简体   繁体   English

提前一天设置熊猫日期时间索引

[英]Set pandas datetime index one day forward

Is it possible to set one day forward a datetime index in pandas? 是否可以将大熊猫的日期时间索引设置为一天?

I've got this index 我有这个索引

mydata.data.index
Out[7]: <class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-27 22:00:00, ..., 2014-12-16 22:00:00]
Length: 500, Freq: None, Timezone: None

and its wrong. 和它的错。 The correct date that the data begin is the day after. 数据开始的正确日期是第二天。 That is 2014-12-17 and they end at 2013-01-28 那是2014-12-17,它们结束于2013-01-28

Is there a way to fix this? 有没有办法解决这个问题?

Some different options: 一些不同的选项:

Use the shift method: 使用shift方法:

In [10]: dti = pd.date_range('2012-01-01', periods=2)

In [11]: dti
Out[11]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-01, 2012-01-02]
Length: 2, Freq: D, Timezone: None

In [13]: dti.shift(1, freq='D')
Out[13]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-02, 2012-01-03]
Length: 2, Freq: D, Timezone: None

But maybe more easy (as @Evert notices), you can simply add a timedelta of one day: 但是也许更简单(如@Evert所示),您只需添加一天的时间增量:

In [14]: dti + pd.Timedelta('1 day')
Out[14]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-01-02, 2012-01-03]
Length: 2, Freq: None, Timezone: None

For older pandas versions, you can also just use datetime.timedelta(days=1) ( pd.Timedelta is only avaible starting from 0.15). 对于较早的熊猫版本,您也可以只使用datetime.timedelta(days=1)pd.Timedelta仅从0.15开始可用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM