Skip to content

Change groupby value_counts (from fall through behaviour) #6540

Closed
@hayd

Description

@hayd

The fall through value_counts (for Series) is a bit strange, I think better result would be (with the standard options):

Can put together if people think it's good.

In [132]: df = pd.DataFrame([['a_link', 'dofollow'], ['a_link', 'dofollow'], ['a_link', 'nofollow'], ['b_link', 'javascript']], columns=['link', 'type'])

In [133]: g = df.groupby(['link', 'type'])

In [134]: g.value_counts()
AttributeError: 'DataFrameGroupBy' object has no attribute 'value_counts'

In [135]: g.link.value_counts()   # redundant level
Out[135]: 
link    type              
a_link  dofollow    a_link    2
        nofollow    a_link    1
b_link  javascript  b_link    1
dtype: int64


Following would make sense for DataFrameGroupby to:
In [136]: pd.Series([len(g.groups[i]) for i in g.grouper.result_index], g.grouper.result_index)
Out[136]: 
link    type      
a_link  dofollow      2
        nofollow      1
b_link  javascript    1
dtype: int64

Note: as_index doesn't make sense here so would be ignored.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions