Skip to content

Add test for named period index, doing group by index #33101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ Other
- Fixed bug in :func:`pandas.testing.assert_series_equal` where dtypes were checked for ``Interval`` and ``ExtensionArray`` operands when ``check_dtype`` was ``False`` (:issue:`32747`)
- Bug in :meth:`Series.map` not raising on invalid ``na_action`` (:issue:`32815`)
- Bug in :meth:`DataFrame.__dir__` caused a segfault when using unicode surrogates in a column name (:issue:`25509`)
- Added test for named period index, doing group by on index.(:issue:`32108`)

.. ---------------------------------------------------------------------------

Expand Down
23 changes: 22 additions & 1 deletion pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
from pandas.errors import PerformanceWarning

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range, read_csv
from pandas import (
DataFrame,
Index,
MultiIndex,
Series,
Timestamp,
date_range,
period_range,
read_csv,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError
import pandas.core.common as com
Expand Down Expand Up @@ -2057,3 +2066,15 @@ def test_groups_repr_truncates(max_seq_items, expected):

result = df.groupby(np.array(df.a)).groups.__repr__()
assert result == expected


def test_groupby_period_index():
periods = 2
index = period_range(start="2018-01", periods=periods, freq="M")
period_series = Series(range(periods), index=index)
period_series.index.name = "Month"
result = period_series.groupby(period_series.index.month).sum()

expected = pd.Series(range(0, periods), index=range(1, periods + 1))
expected.index.name = period_series.index.name
tm.assert_series_equal(result, expected)