星期几错误处理

时间:2016-07-13 01:21:10

标签: python datetime pandas

我是大熊猫的新手,并试图从时间戳中获得一周的日子。

我有以下DF

    Date    Open    High
0   2015-07-13  532.880005  547.109985
1   2015-07-14  546.760010  565.848999
2   2015-07-15  560.130005  566.502991
3   2015-07-16  565.119995  580.679993
4   2015-07-17  649.000000  674.468018

我想知道它发生在哪一周,我做了以下事情:

G['weekday'] = G['Date'].dt.dayofweek()

我收到以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-55-b3724388eff0> in <module>()
----> 1 G['weekday'] = G['Date'].dt.dayofweek()

TypeError: 'Series' object is not callable 

提前致谢

1 个答案:

答案 0 :(得分:2)

所以df['Date'].dt.dayofweek会返回一个系列。而是做:

df['weekday'] = df['Date'].dt.dayofweek
>>> df
        Date        Open        High  weekday
0 2015-07-13  532.880005  547.109985        0
1 2015-07-14  546.760010  565.848999        1
2 2015-07-15  560.130005  566.502991        2
3 2015-07-16  565.119995  580.679993        3
4 2015-07-17  649.000000  674.468018        4