Skip to content

ENH: implement SeriesGroupBy.corrwith #32293

Open
@fujiaxiang

Description

@fujiaxiang

Sample data

>>> import pandas as pd

>>> pd.__version__  # on current master
'1.1.0.dev0+613.g97c0ce962'

>>> df = pd.DataFrame({'key': list('aaabbb'), 'value': [1, 2, 3, 3, 2, 1]})
>>> df
  key  value
0   a      1
1   a      2
2   a      3
3   b      3
4   b      2
5   b      1

Issue

>>> df.groupby('key')
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000001C0D32FC400>

# DataFrameGroupBy.corrwith has no issues
>>> df.groupby('key').corrwith(pd.Series([1,2,3,1,2,3]))
     value
key
a      1.0
b     -1.0

>>> df.groupby('key')['value']
<pandas.core.groupby.generic.SeriesGroupBy object at 0x000001C0D279BF28>

# SeriesGroupBy.corrwith is not implemented
>>> df.groupby('key')['value'].corrwith(pd.Series([1, 2, 3, 1, 2, 3]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\users\xxx\xxx\pandas\pandas\core\groupby\groupby.py", line 580, in __getattr__
    f"'{type(self).__name__}' object has no attribute '{attr}'"
AttributeError: 'SeriesGroupBy' object has no attribute 'corrwith'

Problem description

As shown above, DataFrameGroupBy.corrwith has no issues and works as expected. However, the corresponding SeriesGroupBy.corrwith is not implemented and reports error.

Expected Output

>>> df.groupby('key')['value'].corrwith(pd.Series([1, 2, 3, 1, 2, 3]))
     value
key
a      1.0
b     -1.0

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