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. The DataFrame looks like this:

1993-04-08    3387.83
1993-04-12    3420.79

Any help is appreciated.

有帮助吗?

解决方案

try:

df.index = df.index.to_datetime()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top