Skip to content
Advertisement

Quarter end from today’s date

I’m trying to get a quarter end date based on today’s date, but I wanted to strip the time first. If I do that, the below code throws me an error on the last line AttributeError: ‘datetime.datetime’ object has no attribute ‘to_period’

import pandas as pd
import datetime

today_datetime = datetime.date.today().strftime('%Y-%m-%d')
today_date = datetime.datetime.strptime(today_datetime, '%Y-%m-%d')
df['Qend'] = today_date.to_period("Q").end_time

Could you advise me, how to improve it to get the Quarter end of today’s date without time, please?

Advertisement

Answer

Considering @Ezer-k comment, below is code, which insert quarter end date based on current date with stripped time.

today_datetime = pd.Timestamp.now()
# get month end stripped time
df['Qend'] = today_datetime.to_period('M').end_time
df['Qend'] = df['Qend'].dt.strftime('%Y-%m-%d')

I’m sure there might be better way also, but just to return with correct answer.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement