如何在计算中直接使用Pandas日期时间索引? [英] How to directly use Pandas Date-time index in calculations?
本文介绍了如何在计算中直接使用Pandas日期时间索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下有效的代码:
table['CALC_DOM']=table.index
table['CALC_DOM']=table['END_DATE']-['CALC_DOM']
没有更好的方法可以直接从table.index转换吗?像:
Shouldn't there be a better way to directly convert from table.index? like:
table['CALC_DOM']=table.index
table['CALC_DOM']=table['END_DATE']-(table.index())
我尝试使用
table.index.get_values
和
table.index.date
I have tried using
table.index.get_values
and
table.index.date
...但是我得到的是:
TypeError: incompatible type [object] for a datetime/timedelta operation
.
...but all I get is:
TypeError: incompatible type [object] for a datetime/timedelta operation
.
推荐答案
非常接近!
In [1]: df = DataFrame(randn(5,2),index=date_range('20130101',periods=5))
In [3]: df['date'] = Timestamp('20130102')
In [4]: df
Out[4]:
0 1 date
2013-01-01 2.406932 -0.313473 2013-01-02 00:00:00
2013-01-02 0.034162 -0.708450 2013-01-02 00:00:00
2013-01-03 -1.585031 0.587243 2013-01-02 00:00:00
2013-01-04 1.454818 1.089208 2013-01-02 00:00:00
2013-01-05 -0.778016 -0.994242 2013-01-02 00:00:00
In [5]: df['td'] = df['date']-df.index.to_series()
In [6]: df
Out[6]:
0 1 date td
2013-01-01 2.406932 -0.313473 2013-01-02 00:00:00 1 days, 00:00:00
2013-01-02 0.034162 -0.708450 2013-01-02 00:00:00 00:00:00
2013-01-03 -1.585031 0.587243 2013-01-02 00:00:00 -1 days, 00:00:00
2013-01-04 1.454818 1.089208 2013-01-02 00:00:00 -2 days, 00:00:00
2013-01-05 -0.778016 -0.994242 2013-01-02 00:00:00 -3 days, 00:00:00
这篇关于如何在计算中直接使用Pandas日期时间索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文