'(Python) Pivot table- cannot cast array data from dtype('float64) to dtype('<U32') according to safe rule
I am having some troubles with the cast array data error when making a pivot table. I have checked thoroughly and all the components are float64. However the error still persists. My code snippet is reproduced below:
bins_array=np.asarray(bins_list,dtype = np.float64)
Genesis_file['PASS_DATE']=np.round(((date_rp-Genesis_file['VALUE_DATE']).dt.days)/365,1)
temp_file=Genesis_file.pivot_table(values ='ACY_CURR_BALANCE_2021.03.19',index = pd.cut('CM_MIS_Y',bins = bins_array),
columns = pd.cut('PASS_DATE',bins = bins_array),fill_value = 0.0, aggfunc = 'sum',
dropna = False)
>>>TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'
Genesis_file['ACY_CURR_BALANCE_2021.03.19'].dtypes
>>>dtype('float64')
Genesis_file['CM_MIS_Y'].dtypes
>>>dtype('float64')
Genesis_file['PASS_DATE'].dtypes
>>>dtype('float64')
All columns are float64; However the ['ACY_CURR_BALANCE_2021.03.19'] column is not rounded; the ['CM_MIS_Y'] and ['PASS_DATE'] are rounded to 1 decimal (hope the info helps!)
Thank you very much!
Solution 1:[1]
I don't know why, but adding the dataframe's name to the index and columns' Columns seems to do the trick:
temp_file=Genesis_file.pivot_table(values ='ACY_CURR_BALANCE_2021.03.19',index = pd.cut(Genesis_file['CM_MIS_Y'],bins = bins_array),
columns = pd.cut(Genesis_file['PASS_DATE'],bins = bins_array),fill_value = 0, aggfunc = 'sum',
dropna = False)
Truly appreciate if someone can provide a reason why!
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 | Zack Nguyen |