Reputation: 975
I have a dataframe, generated with the following code:
time_index = pd.date_range(start=datetime(2013, 1, 1, 3),
end=datetime(2013, 1, 2, 2, 59),
freq='5T')
grid_columns = [u'in_brd', u'in_alt', u'out_brd', u'out_alt']
grid_proto = pd.DataFrame(index=time_index, columns=grid_columns)
I've also added some data to this dataframe.
When I'm trying to navigate through the index with int
offsets in basic dataframe I get everthing correct:
In[152]: grid_proto.index[0] + 1
Out[152]: Timestamp('2013-01-01 03:05:00', tz=None)
But if I'm trying to work with some kind of slice, I get an error:
In[153]: z = grid_proto[pd.notnull(x.in_brd)]
In[154]: z.index[0] + 1
Traceback (most recent call last):
File "<ipython-input-151-3ce8a4e5e2d6>", line 1, in <module>
z.index[0] + 1
File "tslib.pyx", line 664, in pandas.tslib._Timestamp.__add__ (pandas\tslib.c:12372)
ValueError: Cannot add integral value to Timestamp without offset.
I understand that this is because in first case I work with a link to DatetimeIndex
elements instead of scalar. And in second case I get exactly scalar Timestamp
value of first index element. Am I right?
How to deal with this offset correctly? (I need to navigate through such slice)
Upvotes: 1
Views: 6118
Reputation: 139142
The reason is that in the first case you have a regular DatetimeIndex with a frequency of 5 minutes. So the integer 1 will be interpreted as one unit of the frequency (5 mins).
While in the second case, because of the slicing, you don't have a regular timeseries anymore, and the DatetimeIndex has no frequency anymore (z.index.freq
will give None, while grid_proto.index.freq
will give 5 mins).
To solve this, you can just explicitely add 5 mins:
In [22]: import datetime as dt
In [23]: z.index[0] + dt.timedelta(minutes=5)
Out[23]: Timestamp('2013-01-01 03:05:00', tz=None)
or alternatively you can add pd.DateOffset(minutes=5)
(this will give the same result).
Upvotes: 4