1

I have a Python time series modeling program where the user sets conditions, or variables, they want to use. One of the variables they set is 'cadence', which takes the values of 'days', 'weeks', 'months', etc. I want to then use this variable in Pandas' DateOffset function to offset dates accordingly. How can I do this?

Below is an example of what I want, though this example does not work. In the example new_d would take the value of '2019-01-15'.

import pandas as pd
cadence = 'weeks'
d = pd.Timestamp('2019-01-01')
new_d = d + pd.DateOffset(cadence=2)
Gaurav Bansal
  • 5,221
  • 14
  • 45
  • 91

1 Answers1

2

Try this

import pandas as pd
cadence = 'weeks'
d = pd.Timestamp('2019-01-01')
new_d = d + pd.DateOffset(**{cadence:2})
new_d
# Timestamp('2019-01-15 00:00:00')
Reza
  • 1,945
  • 1
  • 9
  • 17