I noticed a strange behaviour of the pandas package, that leads to an unexpected failure to add time offsets in some cases.
Suppose I have the following dataframe:
df = pd.DataFrame({'time': ['2022-01-24', '2022-02-24', '2022-03-24'],
'value': [10, 20, 30]})
I can successfully add a time offset to it using this syntax:
df.set_index(['time'], inplace=True)
df.index = pd.to_datetime(df.index, format='%Y-%m-%d')
df.index = df.index + pd.offsets.DateOffset(years=100)
But there is a fail, when I want to add the offset only to a subset of the dataframe, e.g. only to dates after 2022-02-25
, see below:
df.set_index(['time'], inplace=True)
df.index = pd.to_datetime(df.index, format='%Y-%m-%d')
df[df.index>pd.to_datetime('2022-02-25')].index = df[df.index>pd.to_datetime('2022-02-25')].index + pd.offsets.DateOffset(years=100)
The second code slip leads to no change in the column time
of df
. Why nothing changes when I perform the adding only to a slice? And how do I successfully do it? Tnx