I am trying to pass in the timeframe='month'
parameter into my function. I tried applying with lambda function but it doesn’t seem to work.
JavaScript
x
4
1
def a(query_date=pd.DatetimeIndex(['2016-12-31']), timeframe = 'month'):
2
a = query_date.to_series().apply(lambda x:x.timeframe)
3
print(a)
4
Any advice on how to apply my timeframe inside? I want to able to extract the day, month or year with a function.
Advertisement
Answer
you can do that by using the dunder method __getattribute__
like so:
JavaScript
1
5
1
def fn(query_date=pd.DatetimeIndex(['2016-12-31']), timeframe = 'month'):
2
a = query_date.to_series().apply(lambda x: x.__getattribute__(timeframe))
3
return a
4
print(fn())
5