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:
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.