Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get quarter beginning date in python

I want to get date beginning of quarter of the date

x="2018-02-07"
x=pd.to_datetime(x)
x=x-pd.offsets.QuarterBegin()
print(x)
2017-12-01 00:00:00

Which is wrong and should be "2018-01-01 00:00:00"

Can somebody help me where I am going wrong?

like image 274
Sam Avatar asked Nov 07 '18 10:11

Sam


People also ask

How do you create a quarter column in Python?

The quarter can simply be obtained through (month - 1) // 3 + 1 .


1 Answers

You can convert to a period and then to a timestamp:

x = pd.to_datetime('2018-02-07')

res = x.to_period('Q').to_timestamp()

print(res)

Timestamp('2018-01-01 00:00:00')
like image 106
jpp Avatar answered Oct 06 '22 19:10

jpp