user2719480
user2719480

Reputation: 43

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

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

Any help is appreciated.

Upvotes: 4

Views: 1056

Answers (1)

behzad.nouri
behzad.nouri

Reputation: 77951

try:

df.index = df.index.to_datetime()

Upvotes: 3

Related Questions