'How to change the parameter dynamically in pd.DateOffset in python code?
date_ranges_values = request.POST['range']
ft = [df.index[-1] + DateOffset(date_ranges_values = lambda x:x) for x in range(0, 24))]
Suppose I get value in (request.post['range'])
in days, I need to set that particular value in Dateoffset parameter dynamically, But I got an invalid keyword argument for DateOffset Methods, How do I solve this problem?
sample input
date_ranges_values = request.POST['range'] #(days,months,years)
df contains
timestamp state
2016-09-01 07:00:00 423
2016-09-01 07:01:00 298
2016-09-01 07:06:00 251
2016-09-01 07:07:00 466
2016-09-01 07:37:00 415
... ...
2016-09-09 04:12:00 284
2016-09-09 04:22:00 322
2016-09-09 04:52:00 287
2016-09-09 04:53:00 441
2016-09-09 05:23:00 365
Solution 1:[1]
Use keyword args dictionary expansion (**kwargs) to dynamically create the arguments for function calls in Python.
from pandas.tseries.offsets import DateOffset
date_ranges_values = 'days'
x = 6
DateOffset(**{date_ranges_values:x})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gerald Gibson |