Chris
Chris

Reputation: 2071

Add one year to period index

I have this data:

df = pd.DataFrame({Period('2017-01-01', 'D'): 319.8333333333333,
 Period('2017-01-02', 'D'): 241.20833333333334,
 Period('2017-01-03', 'D'): 226.75,
 Period('2017-01-04', 'D'): 217.60416666666666,
 Period('2017-01-05', 'D'): 204.0625})

I'm trying to subtract one year from the period index. I have tried these 1,2 solutions:

df.index + pd.offsets.MonthEnd(12)

But none of them worked as it gave me these error:

IncompatibleFrequency: Input has different freq=12M from PeriodArray(freq=D)

IncompatibleFrequency: Input has different freq=<DateOffset: years=1> from PeriodArray(freq=D)

The only solution I have found is to transform the Period format to another time format (timestamp, datetime, etc.) and then add the year. However, this approach decreases significantly the code's readability and I feel it's an inefficient approach (as I would have to convert to Period format again once the year is subtracted). Hopefully, I'm looking for a straight answer like:

df.index - some_functions(year=1)

If that's impossible, could be using an additional column and then dropping it, but I'm trying to avoid using another time format.

PS: Be aware that adding 365 days for a year can't be the solution as I'm working with multiple years (and not every year has 365 days).

Upvotes: 1

Views: 282

Answers (1)

Laurent
Laurent

Reputation: 13518

With the following dataframe (the one you provide is not valid):

import pandas as pd
from pandas import Period

df = pd.DataFrame(
    {
        Period("2017-01-01", "D"): [319.8333333333333],
        Period("2017-01-02", "D"): [241.20833333333334],
        Period("2017-01-03", "D"): [226.75],
        Period("2017-01-04", "D"): [217.60416666666666],
        Period("2017-01-05", "D"): [204.0625],
    }
).T
df.columns = ["value"]

print(df)
# Output
                 value
2017-01-01  319.833333
2017-01-02  241.208333
2017-01-03  226.750000
2017-01-04  217.604167
2017-01-05  204.062500

Here is one way to do it:

df.index = [Period(f"{p.year-1}-{p.month}-{p.day}") for p in df.index]

print(df)
# Output
                 value
2016-01-01  319.833333
2016-01-02  241.208333
2016-01-03  226.750000
2016-01-04  217.604167
2016-01-05  204.062500

Upvotes: 0

Related Questions