pandas 日期时间索引偏移量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23339993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:58:45  来源:igfitidea点击:

DatetimeIndex offset with

pythonpandas

提问by Mikhail Elizarev

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 intoffsets in basic dataframe I get everthing correct:

当我尝试int在基本数据帧中使用偏移量浏览索引时,我得到了正确的结果:

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 DatetimeIndexelements instead of scalar. And in second case I get exactly scalar Timestampvalue of first index element. Am I right?

我知道这是因为在第一种情况下,我使用指向DatetimeIndex元素的链接而不是标量。在第二种情况下,我得到Timestamp第一个索引元素的准确标量值。我对吗?

How to deal with this offset correctly? (I need to navigate through such slice)

如何正确处理这个偏移?(我需要浏览这样的切片)

回答by joris

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.freqwill give None, while grid_proto.index.freqwill give 5 mins).

原因是在第一种情况下,您有一个频率为 5 分钟的常规 DatetimeIndex。因此整数 1 将被解释为频率的一个单位(5 分钟)。
而在第二种情况下,由于切片,您不再有常规时间序列,并且 DatetimeIndex 不再有频率(z.index.freq将给出 None,而grid_proto.index.freq将给出 5 分钟)。

To solve this, you can just explicitely add 5 mins:

为了解决这个问题,你可以明确地添加 5 分钟:

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).

或者,您可以添加pd.DateOffset(minutes=5)(这将产生相同的结果)。