numpytypestypeerrorseries

TypeError: unhashable type 'series'/'numpy.ndarray'


I'm trying to prepare data for visualization with seaborn. So I need to get number of different type of sessions for a multiple line chart.

With
session_cnt = df.groupby(df['EVENT_DATETIME'].dt.date, df['CUSTOMER_ID']).agg(session_count=('SESSION_ID', 'nunique'), app_session_cnt=('APP_SESSION_ID', 'nunique')).reset_index()

I got

TypeError: unhashable type 'series'

So I did that:

session_cnt = df.groupby(df['EVENT_DATETIME'].dt.date, df['CUSTOMER_ID'].values).agg(session_count=('SESSION_ID', 'nunique'), app_session_cnt=('APP_SESSION_ID', 'nunique')).reset_index()

But got

TypeError: unhashable type 'numpy.ndarray'

I'd like to understand which column to check when using groupby and getting TypeError, because now I'm only guessing. Maybe I need to read a good article on that error.


Solution

  • Groupby takes a "mapping, function, label, pd.Grouper or list of such" :

    session_cnt = (df.groupby([df['EVENT_DATETIME'].dt.date, df['CUSTOMER_ID']]) 
                     .agg(session_count=('SESSION_ID', 'nunique'), 
                          app_session_cnt=('APP_SESSION_ID', 'nunique'))
                     .reset_index()
    )