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

How to get Year Mont and Quarter from time series in python

I have this dataset:

date_time srch_id
2013-04-04 08:32:15 1
2013-04-04 08:32:15 1
..
2013-06-30 19:55:18 332785
2013-06-30 19:55:18 332785

And I want to separate date_time into: YM (Year_Month),YMQ(Year_Month_Quarter),Y and M:

date_time srch_id YMQ YM Y M
2013-04-04 08:32:15 1 2013-04-2 2013-04 2013 4
2013-04-04 08:32:15 1 2013-04-2 2013-04 2013 4
..
2013-06-30 19:55:18 332785 2013-06-2 2013-04 2013 6
2013-06-30 19:55:18 332785 2013-06-2 2013-04 2013 6

I already succeeded with separating it with YM,Y and M with this code:

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

list_YM = [i.split(" ")[0][:-3] for i in  list(train_dataset['date_time'])]
list_Year = [i.split(" ")[0][0:4] for i in  list(train_dataset['date_time'])]
list_Month = [i.split(" ")[0][5:7] for i in  list(train_dataset['date_time'])]

train_dataset['YM'] = list_YM
train_dataset['Year'] = list_Year
train_dataset['Month'] = list_Month

But how do I get YMQ and Q?

>Solution :

If you already have the months listed in list_Month, then you can use a simple floored integer division to get the corresponding quarter of each month:

list_quarter = [(((i - 1) // 3) + 1) for i in list_Month]

This works because the result of this integer division will be zero for months 1, 2 and 3; 1 for months 4, 5, and 6; 2 for months 7, 8, and 9; and 3 for months 10, 11 and 12.

For YMQ you simply concatenate Y, M and Q, which you already have.

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