Skip to content
Advertisement

How do I pass a function parameter into a lambda function subsequently

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.

def a(query_date=pd.DatetimeIndex(['2016-12-31']), timeframe = 'month'):
  a = query_date.to_series().apply(lambda x:x.timeframe)
  print(a)

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:

def fn(query_date=pd.DatetimeIndex(['2016-12-31']), timeframe = 'month'):
    a = query_date.to_series().apply(lambda x: x.__getattribute__(timeframe))
    return a
print(fn())
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement