I have the following DataFrame:
P N ID Year Month
TS
2016-06-26 19:30:00 263.600006 5.4 5 2016 6
2016-06-26 20:00:00 404.700012 5.6 5 2016 6
2016-06-26 21:10:00 438.600006 6.0 5 2016 6
2016-06-26 21:20:00 218.600006 5.6 5 2016 6
2016-07-02 16:10:00 285.300049 15.1 5 2016 7
I'm trying to add a new column based on the values of columns Year and Month something like the following
def exp_records(row):
return calendar.monthrange(row['Year'], row['Month'])[1]
df['exp_counts'] = df.apply(exp_records, axis=1)
But I'm getting the following error:
TypeError: ('integer argument expected, got float', 'occurred at index 2016-06-26 19:30:00')
If I however reset_index() to integer then the above .apply() works fine. Is this the expected behavior?
I'm using using pandas 0.19.1 with Python 3.4
Code to recreate the DataFrame:
s = '''
TS,P,N,ID,Year,Month
2016-06-26 19:30:00,263.600006,5.4,5,2016,6
2016-06-26 20:00:00,404.700012,5.6,5,2016,6
2016-06-26 21:10:00,438.600006,6.0,5,2016,6
2016-06-26 21:20:00,218.600006,5.6,5,2016,6
2016-07-02 16:10:00,285.300049,15.1,5,2016,7
'''
df = pd.read_csv(pd.compat.StringIO(s), index_col=0, parse_dates=True)
loc,ilocetc returns floats even when the column type is integer. That's probably a bug. You can changerow['Year']toint(row['Year'])as a workaround (and month too, of course). Or you can easily dodf.index.days_in_month.intdtypes, then it returned the values appropriately. But when one of them was changed tofloat, then it coerced all columns to float type(supplyingreduce=Falsealso didn't help). That's why it was complaining to supplyintas it's inputs. Moreover, this isn't specific todatetime, even integer indices show a similar behavior.