-1

I have a function below to find how many days in a month

import calendar
calendar.monthrange(2012,2)[1]

This return 29

My question is, now I have a dataframe that contains 100+ (Year,Month)

2012,2

2012,3

2013,1

2016,7

2015,4

...

I have defined the dataframe as df['Year'] and df['Month'], and when I insert those two into the function

import calendar
calendar.monthrange(df['Year'],df['Month'])[1]

It tells me that ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How do I use this function with my dataframe or I have to manually type in those inputs in the function?

Thank you

  • you can also use pandas functions directly : `pd.to_datetime(df[['Year','Month']].astype(str).agg('-'.join,1),format="%Y-%m").dt.daysinmonth` – anky Apr 13 '20 at 18:25
  • Thanks, this also works. May I ask what's the difference between "apply" and your method? It just confuses me both dt.daysinmonth and calendar.monthrange are built in function, but work in different format. So how do I know which one to use? – Thug Chicken soimba Apr 13 '20 at 19:02

1 Answers1

2

You have to use apply instead:

df.apply(lambda row: calendar.monthrange(row['Year'],row['Month'])[1], axis=1)
Bruno Mello
  • 4,448
  • 1
  • 9
  • 39
  • Thanks, this works. May I ask, for all function that requires input, if I want to use it on dataframe, I can just follow this by using "apply" ? – Thug Chicken soimba Apr 13 '20 at 19:03
  • Yes, apply basically pass by all rows of the data frame. Can you consider accepting the answer then? @ThugChickensoimba – Bruno Mello Apr 13 '20 at 19:21