Having problems with multiple "groupby" variable and category (binaries)

df.dtypes

Close       float64
eqId          int64
date         object
IntDate       int64
expiry        int64
delta         int64
ivMid       float64
conf        float64
Skew        float64
psc         float64
vol_B      category
dtype: object

gb = df.groupby([df['vol_B'],df['expiry']])

gb.describe()

      

I am getting long error with the last line

AttributeError: 'Categorical' object has no attribute 'flags'

      

When I execute a groupby

on each of them separately, each one (independently) works fine, I just can't execute multiple groupby

, with one of the variables being "bin".

Also, when I use the other two variables, I can do multiple groupby

- I successfully accomplished this:

gb = df.groupby([df['delta'],df['expiry']])

      

+3


source to share


1 answer


I faced a similar issue similar to the OP and found this question looking for solutions. A simple hack that worked for me after going through the pandas documentation for categorical variables was to change the type of the categorical variable before grouping.

Since vol_B is a categorical variable in your case, you should try the following



#Depending on the content of vol_B you can do astype(int) or astype(float) as well.
gb = df.groupby([df['vol_B'].astype(str), df['expiry']])

      

I didn't go into detail on why this works, and it doesn't, but if I get in it I'll update the answer.

+7


source







All Articles