JamesHudson81
JamesHudson81

Reputation: 2273

filter rows by date index in a dataframe

I am looking forward to filter the following df by date , looking forward to filter only by Wednesdays in the index value:

begin=2015-05-14
end=2015-05-22

Date
2015-05-14   81.370003  6.11282  39.753  44.950001
2015-05-15   80.419998  6.03380  39.289  44.750000
2015-05-18   80.879997  6.00746  41.249  44.360001
2015-05-19   80.629997  6.10465  41.047  40.980000
2015-05-20   80.550003  6.14370  41.636  42.790001
2015-05-21   80.480003  6.16096  42.137  43.680000
2015-05-22   80.540001  6.13916  42.179  43.490002

and goes on..

This is what I have tried:

df1=df[df.index.dayofweek == 2]

and then tried:

df.index = pd.date_range(begin,end,freq='W')

unsuccessfully in both cases

The desired output is the same df returning only the rows of wednesdays.

Upvotes: 3

Views: 3963

Answers (2)

Peter
Peter

Reputation: 441

Not the most elegant solution and probably far from pythonic. But it will do the trick. (put all the data in a pandas dataframe before running it)

import datetime
import pandas as pd
import time
import calendar
b=0
for date in a:
    x = time.strptime(date, "%Y-%m-%d") #strips date in to its components
    year=x.tm_year #get year this is necessary for the way datetime.date works (to my best understanding)
    month=x.tm_mon #get month
    day=x.tm_mday #get day
    dayofweek=datetime.date(year,month,day).weekday() #use above to determine day of the week
    if dayofweek is 2:    
        df.set_value(b, 'col6', True) #create extra column that is True if day of week is Wednesday
    b=b+1
df=df.loc[df['col6'] == True] #drop everything in df that is not a Wednesday observation

Upvotes: 1

jezrael
jezrael

Reputation: 862481

It seeems you can filter first:

df = df.loc[begin:end]
df1=df[df.index.dayofweek == 2]
print (df1)
                    a       b       c          d
2015-05-20  80.550003  6.1437  41.636  42.790001

Upvotes: 6

Related Questions