The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Date Range – pd.date_range()

Create date ranges in pandas using pd.date_range

Think of Pandas Date Range (pd.date_range) like a β€œdate ruler.” You have a start time, end time, and interval frequency you’d like to split your dates by.

Don’t get intimidated by the β€œdate” part, you can just as easily create time ranges as well. The frequency (how you’ll split up your pd.date_range) is set by the offset options. Get familiar with these!

You’ll use pd.date_range when you need to have a clean series of dates to reindex your DataFrame.

pandas.date_range(start=your_start_time,
                  end=your_end_time,
                 freq=your_time_intervals)

Pseudo Code: Create a range of timestamps at a specified start and end.

Pandas Date Range

PD.Date_Range Parameters

  • start – The timestamp that you’d like to start your date range
  • end – The timestamp you’d like to end your date range
  • periods (Optional) – Say instead of splitting your start/end times by 5 minute intervals, you just wanted to have 3 cuts. You can specify periods=3 and pandas will automatically cut your time for you.
  • freq – The sub periods of time that you will cut your date range into. In the above image, we chose 6 hours (β€˜6H’) to split our time by. This resulted in 4 buckets (24 hours in a day). See all the offset aliases here.
  • Normalized (Optional) – Selecting Normalized will automatically set your start and end dates to midnight. This is useful when your start/end time has an hour specified, but you only care about the β€˜day’ information.
  • Other Parameters – For a list of other lesser used parameters. Check out the official documentation.

Now the fun part, let’s take a look at a code sample

Link to code

Official Documentation

On this page