I'm facing an 'Series' object has no attribute 'stack' but this is not always happenings on my data set. without identifying the root cause. Sometimes working fine, sometimes facing the issue...
Here is the query : within var_max_num_by_grpby = 50
df1['counterA'] = (df1.groupby(['id_type', 'start_date', 'freq'], as_index=True).apply( lambda x: pd.Series(i % var_max_num_by_grpby + 1 for i in range(len(x)))).stack().values)
I added the .stack attribute as workaround issue when my group by as only 1 group...
I'm expecting a counter increasing from 1 to n in column 'counterA', everytime the group by ['id_type', 'start_date', 'freq'] reach 50 (var_max_num_by_grpby)
Found the issue, hope this can help someone else...
The root cause is that my groupby condition returns sometime only 1 groupby. When dataset returns 1 groupby, the attribut .stack() fix the issue.
Else, means dataset returns more then 1 groupby, the .stack() attribut generate the error : 'Series' object has no attribute 'stack' on a group by (not everytime)
I just did a if / else base on how many groupby my dataset returns, like this :
# number of distinct groupby clause
nb_groupby = len(df1.groupby(['id_type', 'start_date', 'freq']).nunique().reset_index() )
print('number of distinct groupby clause = ', nb_groupby)
if nb_groupby == 1:
df1['cpt_lot_50_max'] = (df1.groupby(['id_type', 'start_date', 'freq'], as_index=True).apply(
lambda x: pd.Series(i % var_max_num_by_grpby + 1 for i in range(len(x)))).stack().values)
else:
df1['cpt_lot_50_max'] = (df1.groupby(['id_type', 'start_date', 'freq'], as_index=True).apply(
lambda x: pd.Series(i % var_max_num_by_grpby + 1 for i in range(len(x)))).values)