[英]How to increment Index of type Date
I am trying to increment the index by 1 for the following array: 我正在尝试将以下数组的索引增加1:
Date High
2017-02-01 253.20
2017-02-02 252.42
2017-02-03 252.18
2017-02-06 257.82
2017-02-07 260.00
I know that we can increment date by using timedelta(days=1)
but this will generate a date which my array may not have. 我知道我们可以使用timedelta(days=1)
来增加日期,但这会生成一个我的数组可能没有的日期。 So is there any way to increment the index? 那么有什么办法可以增加索引呢?
and after incrementing, how can i get the value for that incremented index? 在递增后,如何获得该递增索引的值?
Use to_timedelta
: 使用to_timedelta
:
df.index = df.index + pd.to_timedelta(1, unit='D')
print (df)
High
Date
2017-02-02 253.20
2017-02-03 252.42
2017-02-04 252.18
2017-02-07 257.82
2017-02-08 260.00
Or is possible use shift
: 或者可以使用shift
:
df = df.shift(1, freq='D')
print (df)
High
Date
2017-02-02 253.20
2017-02-03 252.42
2017-02-04 252.18
2017-02-07 257.82
2017-02-08 260.00
EDIT: 编辑:
For replace DatetimeIndex
is possible use rename
by dict
: 对于替换DatetimeIndex
是可能的,请使用dict
rename
:
d1 = pd.to_datetime('2017-02-03')
d2 = d1 + pd.to_timedelta(5, unit='D')
df = df.rename(index={d1:d2})
print (df)
High
Date
2017-02-01 253.20
2017-02-02 252.42
2017-02-08 252.18
2017-02-06 257.82
2017-02-07 260.00
For select 3. index value: 对于选择3.索引值:
d1 = df.index[2]
d2 = d1 + pd.to_timedelta(5, unit='D')
df = df.rename(index={d1:d2})
print (df)
High
Date
2017-02-01 253.20
2017-02-02 252.42
2017-02-08 252.18
2017-02-06 257.82
2017-02-07 260.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.