Search code examples
pythonpandasdatefiltermask

Using mask to filter dataframe by multiple day of week


I am trying to use a mask to filter and show the dates that I am interested in. Here's my current code:

# mask date range 
start_date = '2019-10-01'
end_date = '2019-11-01'
mask1 = (df['StartTime'] >= start_date) & (df['StartTime'] < end_date)

# mask dayofweek
mask2 = (df.StartTime.dt.dayofweek == 0)

mask = mask1 & mask2

#apply mask
df.loc[mask]

The above code shows all dates within the mask1 date range (2019-10-01 to 2019-11-01) for mask2 dayofweek (0 = Monday). However, I am interested in Monday to Thursday, so I need to adjust mask2.

Here are a few things I've tried:

#this doesn't work:
mask2 = (0<=df.StartTime.dt.dayofweek<=3)

#this doesn't work:
mask2 = (df.StartTime.dt.dayofweek == 0) or (df.StartTime.dt.dayofweek == 1) or (df.StartTime.dt.dayofweek == 2) or (df.StartTime.dt.dayofweek == 3)

Could you please show me what's the best way to do this? Thank you in advance.


Solution

  • One approach by extracting dayofweek and then using isin

    Ex:

    df = pd.DataFrame({
        "StartTime" : ['2019-10-01', '2019-10-02', '2019-10-03', '2019-10-04', '2019-10-05', '2019-10-06', '2019-10-07', '2019-10-08']
    })
    df["StartTime"] = pd.to_datetime(df["StartTime"], format="%Y-%m-%d")
    df["dayofweek"] = df["StartTime"].dt.dayofweek
    
    print(df[df["dayofweek"].isin([0,1,2])]['StartTime'])
    

    Output:

    0   2019-10-01
    1   2019-10-02
    6   2019-10-07
    7   2019-10-08
    Name: StartTime, dtype: datetime64[ns]