How to directly use Pandas Date-time index in calculations?

前端 未结 1 569
囚心锁ツ
囚心锁ツ 2021-01-13 20:56

I have the following code that works:

table[\'CALC_DOM\']=table.index
table[\'CALC_DOM\']=table[\'END_DATE\']-[\'CALC_DOM\']

Shouldn\'t the

相关标签:
1条回答
  • 2021-01-13 21:25

    Very close!

    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
    
    0 讨论(0)
提交回复
热议问题