I have a pandas DataFrame with index column = date
.
Input:
value
date
1986-01-31 22.93
1986-02-28 15.46
I want to floor the date to the first day of that month
Output:
value
date
1986-01-01 22.93
1986-02-01 15.46
What I tried:
df.index.floor('M')
ValueError: <MonthEnd> is a non-fixed frequency
This is potentially because the df is generated by
df = df.resample("M").sum()
(The output of this code is the input at the beginning of the question)
I also tried df = df.resample("M", convention='start').sum()
. However, it does not work.
I know in R, it is easy to just call floor(date, 'M')
.
datetime
objects for this, but this might be way to costly if you are trying to process millions of objects. – Flowerlike