Search⌘ K
AI Features

Create a Pandas Timestamp and date_range Object

Explore creating pandas Timestamp and date_range objects and understand how frequency strings like month-end (M) and minutes (T) affect date calculations. This lesson helps you grasp advanced datetime handling and frequency offset applications in pandas, improving your data manipulation skills.

We'll cover the following...

Try it yourself

Try executing the code below to see the result.

Python 3.8
import pandas as pd
start = pd.Timestamp.fromtimestamp(0).strftime('%Y-%m-%d')
times = pd.date_range(start=start, freq='M', periods=2)
print(times)

Explanation

There are couple of puzzling things here:

  • M is a month frequency.
  • The first date is January 31st and not January 1st.

Let’s start with M being a month frequency. You’ve probably used the infamous strftime or its cousin strptime to convert datetime to or from strings. In those cases, M stands for minute:

In [1]: t =
...