Reputation: 1493
I have the following 15 minute data as a dataframe for 3 years. With the first two columns being the index.
2014-01-01 00:15:00 1269.6
2014-01-01 00:30:00 1161.6
2014-01-01 00:45:00 1466.4
2014-01-01 01:00:00 1365.6
2014-01-01 01:15:00 1362.6
2014-01-01 01:30:00 1064.0
2014-01-01 01:45:00 1171.2
2014-01-01 02:00:00 1171.0
2014-01-01 02:15:00 1330.4
2014-01-01 02:30:00 1309.6
2014-01-01 02:45:00 1308.4
2014-01-01 03:00:00 1494.0
I would like to offset/shift the data into the previous year so that 2014-01-01 00:15:00 1269.6
will be converted into 2013-01-01 00:15:00 1269.6
.
I have used df = df.shift(-1, freq='15min') to shift the dataframe 15 mins into the past but would not like to offset/shift by the number of 15min intervals as this might cause errors in leap years and with clock changes.
Does anyone have a smooth solution for this?
Upvotes: 5
Views: 9810
Reputation: 128948
In [13]: df = DataFrame(randn(10,1),index=date_range('20140101 00:15:00',freq='15T',periods=10))
In [14]: df
Out[14]:
0
2014-01-01 00:15:00 -0.176117
2014-01-01 00:30:00 0.517030
2014-01-01 00:45:00 1.033585
2014-01-01 01:00:00 -0.284402
2014-01-01 01:15:00 0.476984
2014-01-01 01:30:00 0.356078
2014-01-01 01:45:00 -0.285609
2014-01-01 02:00:00 0.423048
2014-01-01 02:15:00 0.095823
2014-01-01 02:30:00 -1.123258
In [15]: df.index = df.index-pd.offsets.Day(365)
In [16]: df
Out[16]:
0
2013-01-01 00:15:00 -0.176117
2013-01-01 00:30:00 0.517030
2013-01-01 00:45:00 1.033585
2013-01-01 01:00:00 -0.284402
2013-01-01 01:15:00 0.476984
2013-01-01 01:30:00 0.356078
2013-01-01 01:45:00 -0.285609
2013-01-01 02:00:00 0.423048
2013-01-01 02:15:00 0.095823
2013-01-01 02:30:00 -1.123258
Upvotes: 5