If our goal is to extract relevant dates from the DataFrame without modification to the contents of DataFrame, we can do it this way:
Case 1: 'Date' column is already in string format, use:
df[df['Date'].str.endswith('12-31')]
Case 2: 'Date' column is already in datetime format, use:
df[df.assign(Date=df['Date'].astype(str))['Date'].str.endswith('12-31')]
Both give you the desired extraction output according to what's the current data type it is, without modification to the DataFrame.
Edit
If you want to automate the extraction of entries falling in business year ends, you can try the following code. This is for your reference and you may want to further fine-tune it to clean up some intermediate columns.
df['Date1'] = pd.to_datetime(df['Date'])
df['BYearEnd'] = pd.to_datetime(df['Date1'].dt.year.astype(str) + '-12-01') + pd.offsets.BMonthEnd(1)
Here, we created temporary columns Date1
and BYearEnd
with values corresponding to the column Date
Column BYearEnd
contains the business year end dates for the respective dates in column Date
.
Then we can extract the relevant dates with the following code:
df[df['Date1'] == df['BYearEnd']]