I have a dataframe like this--

Now, I have to filter this data only for the previous week dates (excluding Sat, Sun)
I wrote this Python code-
today = date.today() - timedelta(weeks = 1)
weekday = today.weekday()
prev_week_start = today - timedelta(days = weekday)
for i in range(0,5):
prev_weekdate = today - timedelta(days = i)
prev_weekend = prev__weekdate timedelta(days = 4)
next_weekstart = prev_weekdate timedelta(days = 14)
next_weekdateend = next_weekstart timedelta(days = 4)
week_dates = pd.DataFrame({"LastWeekStartDate":pd.to_datetime(['prev_weekdate']),
"LastWeekEndDate":pd.to_datetime(['prev_weekend']),
"NextWeekStartDate":pd.to_datetime(['next_weekstart']),
"NextWeekEndDate":pd.to_datetime(['next_weekdateend'])})
week_dates.head()
I am getting the correct dates for all the previous & next week start & end dates with these formulae.
Then I wrote the following code to get my data and filter it for previous week using the above dataframe - week_dates
df = pd.read_csv()
df['Date'] = pd.to_datetime(df.Date)
Now to filter rows of my dataframe - df , I wrote-
df = df[(df['Date'] >= week_dates.LastWeekStartDate & df['Date'] <= week_dates.LastWeekEndDate)][["Date","Actual Call Volume","Forecasted Call Volume"]]
I am getting an error -
TypeError: unsupported operand type(s) for &:'DatetimeArray' and 'DateTimeArray'
Please can somebody tell me where I am going wrong or is there any other way to write this code. Thanks in advance!
CodePudding user response:
Use DataFrame.loc with add )( and replaced ][ to , and also use loc for select first value in prev_week DataFrame:
df = df.loc[(df['Date'] >= prev_week.loc[0, 'Prev_week_start']) &
(df['Date'] <= prev_week.loc[0, 'Prev_week_start']),
["Date","Actual Call Volume","Forecasted Call Volume"]]
For simplier solution use:
Prev_week_start = date.today() - timedelta(weeks = 1)
Prev_week_end = Prev_week_start timedelta(days = 4)
df = df.loc[(df['Date'] >= Prev_week_start) & (df['Date'] <= Prev_week_end),
["Date","Actual Call Volume","Forecasted Call Volume"]]
CodePudding user response:
I found the solution to my problem. The values of column 'Date' in my dataframe were being compared with the entire columns of my function week_range(start) table which is not possible. I needed a scalar value to filter my dataframe.
The simplest way to write would be as follows-
df = df[(df['Date'] >= prev_week.Prev_week_start[0]) & (df['Date'] >= prev_week.Prev_week_end[0])][["Date","Actual Call Volume","Forecasted Call Volume"]]
I simply specified the index for Prev_week_start & Prev_week_end by adding index [0].
