Home > front end >  Pandas Using Frequencies to Offset Date
Pandas Using Frequencies to Offset Date

Time:09-17

I am trying to build a function in Python where if a user provides an offset frequency such as 1D 10M 1Y then I can provide the date using the offset.

Example if user inputs 1M along with a date 2021-08-25

pd.Timestamp('2021-08-25') - pd.tseries.frequencies.to_offset('1M')

The above code outputs Timestamp('2021-07-31 00:00:00') which is not one month prior to the date provided by user. Expected Output Timestamp('2021-07-25 00:00:00')

How can I achieve this?

CodePudding user response:

You need to use pd.DateOffset:

>>> pd.Timestamp("2020-08-25") - pd.DateOffset(months=1)
Timestamp('2020-07-25 00:00:00')

The frequencies.to_offset() returns a <MonthEnd> object, hence why you were getting 07-31:

>>> pd.tseries.frequencies.to_offset("1M")
<MonthEnd>

CodePudding user response:

Since years and months don't have a fixed frequency you can use the pd.offsets.DateOffset method to deal with calendar additions of years and months, similar to the implementation of relativedelta.

Because you'll need to specify both the argument names and values for this to work, you can change your function to pass a dict with the arguments as opposed to just the offset frequency.

import pandas as pd

def offset_date(timestamp, offset):
    """
    offset: dict of {'offset frequency': periods}
    """
    return timestamp   pd.offsets.DateOffset(**offset)


timestamp = pd.Timestamp('2021-08-25')

offset_date(timestamp, {'months': 1})
#Timestamp('2021-09-25 00:00:00')

offset_date(timestamp, {'days': 10})
#Timestamp('2021-09-04 00:00:00')

offset_date(timestamp, {'years': -3})
#Timestamp('2018-08-25 00:00:00')
  • Related