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 shift a column by 1 year in Python

With the python shift function, you are able to offset values by the number of rows. I’m looking to offset values by a specified time, which is 1 year in this case.

Here is my sample data frame. The value_py column is what I’m trying to return with a shift function. This is an over simplified example of my problem. How do I specify date as the offset parameter and not use rows?

import pandas as pd
import numpy as np

test_df = pd.DataFrame({'dt':['2020-01-01', '2020-08-01', '2021-01-01', '2022-01-01'],
                        'value':[10,13,15,14]})

test_df['dt'] = pd.to_datetime(test_df['dt'])

test_df['value_py'] = [np.nan, np.nan, 10, 15]

I have tried this but I’m seeing the index value get shifted by 1 year and not the value column

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

test_df.set_index('dt')['value'].shift(12, freq='MS')

>Solution :

This should solve your problem:

test_df['new_val'] = test_df['dt'].map(test_df.set_index('dt')['value'].shift(12, freq='MS'))
test_df


        dt   value  value_py new_val
0   2020-01-01  10  NaN     NaN
1   2020-08-01  13  NaN     NaN
2   2021-01-01  15  10.0    10.0
3   2022-01-01  14  15.0    15.0

Use .map() to map the values of the shifted dates to original dates.

Also you should use 12 as your shift parameter not -12.

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