I want to shift the date index of my daily data forward by one month. This is in order to calculate a rolling month-on-month change on the daily data by calculating on two dataframes - the existing one with the correct time stamps and a new one with the time stamps shifted forward one month with all the other data columns the same
The problem is that not all months have the same length, so I don't want to roll it forward by 30 days. I would like for example the 24th of March to match with 24th of February (28 days), and the 24th of April to match the 24th March (31 days).
I tried variations of the following with no joy:
df= pd.read_csv("seven_day.csv", parse_dates=['Date'], index_col=['Date'])
df.shift(periods=1, freq="M")
df.shift(periods=1, freq="MS")
What happens is I get the end or start of the next month for ALL of the days in the current month. Eg all March daily data days go to the end or start of April.
Would massively appreciate any insight.
You can use a
DateOffset:Example:
Output:
Intermediate:
Other way around (note the
-sign):Output:
Intermediate: