Getting month and day names from datetime with polars
technical
polars
Author
Joram Mutenge
Published
July 16, 2024
When you have timeseries data, you may want to extract the day and month names from the date. Polars makes it easy to do that but the default way doesn’t get you the actual names like “Wednesday” or “January” instead it gives you numbers; 3 for Wednesday and 1 for January.
Here’s my dataset with YouTube comments and the datetime stamp when they were posted.
Here’s the default way to get the day and month data from datetime. As I pointed out, it won’t get us the names but it will get us the numbers that match either the day or month.
Why would you want to use short names? For one, they are good for visualizations. Here is a bar chart with long names. You’ll agree that it doesn’t look nice, especially on the right side, because the day names are too close.
Now, here is the same bar chart made with short day names. It’s easy on the eyes because the bar labels are nicely spaced.
(df .with_columns(Day_Short=pl.col('Datetime').dt.strftime('%a'), Day_Long=pl.col('Datetime').dt.strftime('%A'), Month_Short=pl.col('Datetime').dt.strftime('%b'), Month_Long=pl.col('Datetime').dt.strftime('%B')) .group_by('Day_Short').len() .to_pandas() .plot.bar(x='Day_Short', y='len', rot=0, width=.85, legend=False, color='#dc8add', xlabel='', title='Total number of comments for each week day', figsize=(8,4)) );
If you can’t bring yourself to write the complicated code with percentage signs and letters just to extract day or month names, you can use a library called polars_xdt. Here’s how easy it is to get both day and month names.
What’s more, with polars_xdt you can get the day names in other languages. The code below shows how to get the French and Ukrainian day names.
import polars_xdt as xdt(df .with_columns(Weekday=xdt.day_name('Datetime'), Month=xdt.month_name('Datetime')) .head() )
shape: (5, 4)
text
Datetime
Weekday
Month
str
datetime[μs]
str
str
"The legislator who said he did…
2024-01-29 04:01:02
"Monday"
"January"
"This commentator is no doubt a…
2024-01-27 16:01:02
"Saturday"
"January"
"Satanists run this world."
2024-01-27 16:01:02
"Saturday"
"January"
"All abortions are a sacrifice …
2024-01-27 16:01:02
"Saturday"
"January"
"I just had a guy say that givi…
2024-01-25 16:01:02
"Thursday"
"January"
However, I couldn’t find a way to get the names in short form using polars_xdt. But that’s not too much of a problem because the slice method in polars can help us do that.