Home > OS >  Adding a year to a period?
Adding a year to a period?

Time:03-04

I have a column which I have converted to dateime:

df['date'] = pd.to_datetime(df['date'], errors='coerce')

date
2021-10-21 00:00:00
2021-10-24 00:00:00
2021-10-25 00:00:00
2021-10-26 00:00:00

And I need to add 1 year to this time based on a conditional:

df.loc[df['quarter'] == "Q4_", 'date']   pd.offsets.DateOffset(years=1)

but it's not working....

date
2021-10-21 00:00:00
2021-10-24 00:00:00
2021-10-25 00:00:00
2021-10-26 00:00:00

I have tried converting it to period since I only need the year to be used in a concatenation later:

df['year'] = df['date'].dt.to_period('Y')

but I cannot add any number to a period.

CodePudding user response:

This appears to be working for me:

import pandas as pd

df = pd.DataFrame({'date':pd.date_range('1/1/2021', periods=50, freq='M')})

print(df.head(24))

Input:

         date
0  2021-01-31
1  2021-02-28
2  2021-03-31
3  2021-04-30
4  2021-05-31
5  2021-06-30
6  2021-07-31
7  2021-08-31
8  2021-09-30
9  2021-10-31
10 2021-11-30
11 2021-12-31
12 2022-01-31
13 2022-02-28
14 2022-03-31
15 2022-04-30
16 2022-05-31
17 2022-06-30
18 2022-07-31
19 2022-08-31
20 2022-09-30
21 2022-10-31
22 2022-11-30
23 2022-12-31

Add, year:

df.loc[df['date'].dt.quarter == 4, 'date']  = pd.offsets.DateOffset(years=1)

print(df.head(24))

Note per your logic, the year increase on October.

Output:

         date
0  2021-01-31
1  2021-02-28
2  2021-03-31
3  2021-04-30
4  2021-05-31
5  2021-06-30
6  2021-07-31
7  2021-08-31
8  2021-09-30
9  2022-10-31
10 2022-11-30
11 2022-12-31
12 2022-01-31
13 2022-02-28
14 2022-03-31
15 2022-04-30
16 2022-05-31
17 2022-06-30
18 2022-07-31
19 2022-08-31
20 2022-09-30
21 2023-10-31
22 2023-11-30
23 2023-12-31
  • Related