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

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 1:[1]

You don't need to use for-loops if you use pandas package and datetimemethods:

import pandas as pd

data = {'date_time': ['2013-04-04 08:32:15','2013-04-04 08:32:15','2013-06-30 19:55:18','2013-06-30 19:55:18'],
        'srch_id': [1,1,332785,332785]}
example = pd.DataFrame(data)

# Convert to datetime to use its methods
example['date_time'] = pd.to_datetime(example['date_time'])

# Add year as string
example['Y'] = example['date_time'].dt.year.astype(str)
# Add month as string
example['M'] = example['date_time'].dt.month.astype(str)

# Add year and month as string
example['YM'] = example['Y'] + '-' + example['M']

# Add year and quarter as string
example['YQ'] = example['date_time'].dt.to_period('Q').astype(str)

# Add year, month and quarter? Every month is already related to a quarter
example['YMQ'] = example['Y'] + '-' + example['M'] + '-' + example['YQ'].str.slice(-2)

# If you want date_Time column as string type:
example['date_time'] = example['date_time'].astype(str)

Output:

Out[53]: 
             date_time  srch_id     Y  M      YM      YQ        YMQ
0  2013-04-04 08:32:15        1  2013  4  2013-4  2013Q2  2013-4-Q2
1  2013-04-04 08:32:15        1  2013  4  2013-4  2013Q2  2013-4-Q2
2  2013-06-30 19:55:18   332785  2013  6  2013-6  2013Q2  2013-6-Q2
3  2013-06-30 19:55:18   332785  2013  6  2013-6  2013Q2  2013-6-Q2
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RobertoT