I have a simple dataframe containing a datetimeindex and a column for holiday names. It is structured as follows:
index = ['2016-12-25','2017-04-07','2017-12-26','2018-04-07']
data = {'holiday_name': ['xmas','independence','xmas','independence']}
df = pd.DataFrame(data=data, index=index)
df.index = pd.to_datetime(df.index)
|index |holiday name
|2016-12-25 |christmas
|2017-07-04 |independence day
|2017-12-26 |christmas <<<<<<<<<<<<<<<
|2018-07-04 |independence day
As you can see there is an error in the datetimeindex of the dataframe (Christmas at the 26th of Dec.). I could take the index as a new column and change using loc/at but it surely i can do this directly on the index?
Thanks,
The answer has already been given and works perfectly. For reference this is what i was using to get around it...
df.reset_index(inplace=True)
df.rename(columns={'index':'date'}, inplace=True)
df.at[2,'date'] = '2017-12-25 00:00:00'
df.set_index('date', inplace=True)
You can use .rename()
.
Before rename:
holiday_name
2016-12-25 xmas
2017-04-07 independence
2017-12-26 xmas
2018-04-07 independence
After rename:
df.rename(
index={pd.Timestamp("2017-12-26"): pd.Timestamp("2017-12-24")},
inplace=True,
)
print(df)
Prints:
holiday_name
2016-12-25 xmas
2017-04-07 independence
2017-12-24 xmas
2018-04-07 independence