I\'ve written this function to get the last Thursday of the month
def last_thurs_date(date):
month=date.dt.month
year=date.dt.year
cal = cale
freq='W-FRI'pandas.DataFrame using pandas.date_range and specifying freq='W-FRI.
W-FRI is Weekly Fridayspd.date_range(df.Date.min(), df.Date.max(), freq='W-FRI')
df.groupby on year and month, and select .last(), to get the last Friday of every month for every year in the date range..last() for each month, there's not an issue with trying to figure out which week of the month has the last Friday.Date column of the dataframe that are in last_fridays_in_daterange.
.isin method to determine containment.import pandas as pd
# test data: given a dataframe with a datetime column
df = pd.DataFrame({'Date': pd.date_range(start=pd.to_datetime('2014-01-01'), end=pd.to_datetime('2020-08-31'), freq='D')})
# create a dateframe with all Fridays in the daterange for min and max of df.Date
fridays = pd.DataFrame({'datetime': pd.date_range(df.Date.min(), df.Date.max(), freq='W-FRI')})
# use groubpy and last, to get the last Friday of each month into a list
last_fridays_in_daterange = fridays.groupby([fridays.datetime.dt.year, fridays.datetime.dt.month]).last()['datetime'].tolist()
# find the data for the last Friday of the month
df[df.Date.isin(last_fridays_in_daterange)]