我试图在每日股票价格收盘的数据框中选择每个星期五。我在this link中阅读并尝试了这个建议,具体来说是:
Fridays = df[df.index.weekday == 4] #Fridays
但是我收到以下错误:
AttributeError: 'Index' object has no attribute 'weekday'
<class 'pandas.core.frame.DataFrame'>
我认为问题在于Python不会将索引中的字符串识别为日期,但我无法弄清楚原因。 DataFrame看起来像这样:
1993-04-08 3387.83
1993-04-12 3420.79
感谢任何帮助。
答案 0 :(得分:3)
尝试:
df.index = df.index.to_datetime()