Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting year-month to next year-quarter

I have below date expressed as yearmon ‘202112'

I want to convert this to yearqtr and report the next quarter. Therefore from above string I want to get 2022Q1

I unsuccessfully tried below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import pandas as pd
pd.PeriodIndex(pd.to_datetime('202112') ,freq='Q')

Could you please help how to obtain the expected quarter. Any pointer will be veru helpful

>Solution :

I think one issue you’re running into is that '202112' is not a valid date format. You’ll want to use '2021-12'. Then you can do something like this:

pd.to_datetime('2021-12').to_period('Q') + 1

You can convert your date to this new format by simply inserting a - at index 4 of your string like so: date[:4] + '-' + date[4:]

This will take your date, convert it to quarters, and add 1 quarter.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading