I have a dataframe in pandas which has an error in the index: each entry between 23:00:00 and 23:59:59 has a wrong date. I would need to subtract one day (i.e. 24 hours) to each entry between those two times.
I know that I can obtain the entries between those two times as df[df.hour == 23]
, where df
is my dataframe. However, can I modify the day only for those specific entries of the dataframe index?
Resetting would take me more time, since my dataframe index is not evenly spaced as you can see from the figure below (the step between two consecutive entries is once 15 minutes and once 30 minutes). Note also from the figure the wrong date in the last three entries: it should be 2018-02-05 and not 2018-02-06.
I tried to do this
df[df.index.hour == 23].index.day = df[df.index.hour == 23].index.day - 1
but I get AttributeError: can't set attribute
Sample data:
2018-02-05 22:00:00 271.8000
2018-02-05 22:30:00 271.5600
2018-02-05 22:45:00 271.4400
2018-02-06 23:15:00 271.3750
2018-02-06 23:30:00 271.3425
2018-02-06 00:00:00 271.2700
2018-02-06 00:15:00 271.2300
2018-02-06 00:45:00 271.1500
2018-02-06 01:00:00 271.1475
2018-02-06 01:30:00 271.1425
2018-02-06 01:45:00 271.1400
Expected output:
2018-02-05 22:00:00 271.8000
2018-02-05 22:30:00 271.5600
2018-02-05 22:45:00 271.4400
2018-02-05 23:15:00 271.3750
2018-02-05 23:30:00 271.3425
2018-02-06 00:00:00 271.2700
2018-02-06 00:15:00 271.2300
2018-02-06 00:45:00 271.1500
2018-02-06 01:00:00 271.1475
2018-02-06 01:30:00 271.1425
2018-02-06 01:45:00 271.1400