0

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?

2 Answers 2

3

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>
2

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')
2
  • Does relativedelta works the same as pd.offsets.DateOffset?
    – Lopez
    Commented Sep 14, 2021 at 19:19
  • @Lopez the docs suggest it's exactly the same
    – ALollz
    Commented Sep 14, 2021 at 19:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.