Get startedGet started for free

Parsing and manipulating dates

1. Parsing and manipulating dates

Welcome back! In this chapter, we'll learn how to work with time-series data.

2. Time series data

Data that records events that happen over time are called time series. Examples of time series include things like market prices

3. Time series data

Or the sales of a company

4. Time series data

Or the number of streams of a hit song

5. Time series data

Or the number of polar bears in Canada

6. Electricity prices dataset

We'll analyze electricity prices from the British market in summer 2025. The dataset has three columns: time with hourly values, price in pounds per megawatt hour, and solar measuring incoming solar power in watts per square meter. Values above 400 indicate a warm summer's day.

7. Price and solar relationship

Here we see electricity prices over 24 hours - prices are near zero at midday and higher in the morning and afternoon.

8. Price and solar relationship

Prices drop at midday because there is cheap solar power available.

9. Datetime dtypes in Polars

Polars has four main dtypes for working with data that captures times: Date for calendar dates. Datetime for date and time combined. Time for time of day only. And Duration for time spans like "3 hours".

10. Automatic datetime parsing

Polars can automatically recognize common datetime patterns when reading CSV files.

11. Automatic datetime parsing

To enable this, we pass try_parse_dates=True. This works for patterns using year-month-day order, such as this date, or this datetime with the time included.

12. Our data needs manual parsing

However, our CSV uses a different format-day/month/year - so Polars won't auto-detect it. We need to parse it manually.

13. Parsing with strptime

To parse the time column into a datetime, we use strptime from the string namespace. We first pass the target dtype

14. Parsing with strptime

pl.Datetime in this case.

15. Parsing with strptime

Then we add the format string matching our data. Percent-d is day, percent-m is month, and percent-Y is four-digit year.

16. Parsing with strptime

Percent-H is hour, and percent-M is minute.

17. Parsing with strptime

And now our time column is a proper Datetime type.

18. Extracting datetime components

Next, let's look at extracting datetime components. For filtering or grouping, we often need to extract part

19. Extracting datetime components

of the datetime, like the date

20. Extracting datetime components

or the time of day for comparing values across different dates

21. Extracting datetime components

or just the hour for grouping by time of day.

22. Extracting the date

To extract the date, we use the dt.date expression.

23. Extracting the date

We use an alias to create a new column called date.

24. Extracting the date

Giving us a separate date column with the Date dtype.

25. Extracting the time

Similarly, we can extract just the time using the dt.time expression.

26. Extracting the time

Useful for comparing values at the same time across days.

27. Extracting the hour

We can also extract individual components like the hour using dt.hour.

28. Extracting the hour

This gives us integers from 0 to 23 - useful for grouping or filtering by hour of day.

29. Other datetime components

Beyond hour, we can also extract the year, month, day, minute, and many more components.

30. Adjusting datetimes

Finally, let's look at adjusting datetimes. The offset_by method does this using string codes. For example, 1h shifts forward one hour, and -2d shifts backward two days.

31. Adjusting datetimes

The full set of offset codes is available in the Polars documentation.

32. Adding a time window end

Let's use this to add a column showing when each hourly window ends. We offset the time by one hour.

33. Adding a time window end

And call it time_end.

34. Adding a time window end

This is useful for analysis where you need explicit window boundaries.

35. Let's practice!

Now you know how to parse dates, extract components, and adjust datetimes. Time for practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.