[英]Dataframe Row Selection by Weekday of Index in Python
I am trying to select every Friday in a dataframe of daily stock price closes. 我试图在每日股票价格收盘的数据框中选择每个星期五。 I read and tried the suggestion in this link , specifically:
我在这个链接中阅读并尝试了这个建议,具体来说:
Fridays = df[df.index.weekday == 4] #Fridays
but I get the following error: 但是我收到以下错误:
AttributeError: 'Index' object has no attribute 'weekday'
<class 'pandas.core.frame.DataFrame'>
I believe the issue is that Python does not recognize the strings in the index as dates, but I can't figure out why. 我认为问题是Python不能将索引中的字符串识别为日期,但我无法弄清楚原因。 The DataFrame looks like this:
DataFrame看起来像这样:
1993-04-08 3387.83
1993-04-12 3420.79
Any help is appreciated. 任何帮助表示赞赏。
尝试:
df.index = df.index.to_datetime()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.