Skip to content

Commit 82abcd9

Browse files
authored
REGR: Fix regression when grouping over a Series (#57323)
1 parent 2e0ef79 commit 82abcd9

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, :meth:`.SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
2222
- Fixed regression in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, :meth:`.SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
2323
- Fixed regression in :meth:`CategoricalIndex.difference` raising ``KeyError`` when other contains null values other than NaN (:issue:`57318`)
24+
- Fixed regression in :meth:`DataFrame.groupby` raising ``ValueError`` when grouping by a :class:`Series` in some cases (:issue:`57276`)
2425
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
2526
- Fixed regression in :meth:`DataFrame.merge` raising ``ValueError`` for certain types of 3rd-party extension arrays (:issue:`57316`)
2627
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)

pandas/core/internals/managers.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
final,
1616
)
1717
import warnings
18-
import weakref
1918

2019
import numpy as np
2120

@@ -337,8 +336,8 @@ def references_same_values(self, mgr: BaseBlockManager, blkno: int) -> bool:
337336
Checks if two blocks from two different block managers reference the
338337
same underlying values.
339338
"""
340-
ref = weakref.ref(self.blocks[blkno])
341-
return ref in mgr.blocks[blkno].refs.referenced_blocks
339+
blk = self.blocks[blkno]
340+
return any(blk is ref() for ref in mgr.blocks[blkno].refs.referenced_blocks)
342341

343342
def get_dtypes(self) -> npt.NDArray[np.object_]:
344343
dtypes = np.array([blk.dtype for blk in self.blocks], dtype=object)

pandas/tests/copy_view/test_methods.py

+11
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ def test_reset_index_series_drop(using_copy_on_write, index):
273273
tm.assert_series_equal(ser, ser_orig)
274274

275275

276+
def test_groupby_column_index_in_references():
277+
df = DataFrame(
278+
{"A": ["a", "b", "c", "d"], "B": [1, 2, 3, 4], "C": ["a", "a", "b", "b"]}
279+
)
280+
df = df.set_index("A")
281+
key = df["C"]
282+
result = df.groupby(key, observed=True).sum()
283+
expected = df.groupby("C", observed=True).sum()
284+
tm.assert_frame_equal(result, expected)
285+
286+
276287
def test_rename_columns(using_copy_on_write):
277288
# Case: renaming columns returns a new dataframe
278289
# + afterwards modifying the result

0 commit comments

Comments
 (0)