Filter rows by date index in dataframe

I am looking forward to filtering the following df by date, expecting to filter only on 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 continues ..

Here's what I've 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 result is the same df returning only strings from the outside.

+3


source to share


2 answers


He sees that 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

      

+5


source


Not the most elegant solution and probably far from pythonic. But this will be a trick. (put all data in pandas framework before starting)



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

      

+1


source







All Articles