Skip to content

Commit 839ed72

Browse files
authored
BUG: Groupby not keeping object dtype when infer string is set (pandas-dev#55620)
1 parent 3a90faa commit 839ed72

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Fixed regressions
2424

2525
Bug fixes
2626
~~~~~~~~~
27+
- Fixed bug in :class:`.DataFrameGroupBy` reductions not preserving object dtype when ``infer_string`` is set (:issue:`55620`)
2728
- Fixed bug in :meth:`Categorical.equals` if other has arrow backed string dtype (:issue:`55364`)
2829
- Fixed bug in :meth:`DataFrame.__setitem__` not inferring string dtype for zero-dimensional array with ``infer_string=True`` (:issue:`55366`)
2930
- Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`)

pandas/core/groupby/groupby.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1904,8 +1904,7 @@ def _agg_py_fallback(
19041904
ser = Series(values, copy=False)
19051905
else:
19061906
# We only get here with values.dtype == object
1907-
# TODO: special case not needed with ArrayManager
1908-
df = DataFrame(values.T)
1907+
df = DataFrame(values.T, dtype=values.dtype)
19091908
# bc we split object blocks in grouped_reduce, we have only 1 col
19101909
# otherwise we'd have to worry about block-splitting GH#39329
19111910
assert df.shape[1] == 1

pandas/tests/groupby/test_groupby.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
import pytest
77

8+
from pandas.compat import pa_version_under7p0
89
from pandas.errors import (
910
PerformanceWarning,
1011
SpecificationError,
@@ -2537,13 +2538,24 @@ def test_groupby_column_index_name_lost(func):
25372538
tm.assert_index_equal(result, expected)
25382539

25392540

2540-
def test_groupby_duplicate_columns():
2541+
@pytest.mark.parametrize(
2542+
"infer_string",
2543+
[
2544+
False,
2545+
pytest.param(
2546+
True,
2547+
marks=pytest.mark.skipif(pa_version_under7p0, reason="arrow not installed"),
2548+
),
2549+
],
2550+
)
2551+
def test_groupby_duplicate_columns(infer_string):
25412552
# GH: 31735
25422553
df = DataFrame(
25432554
{"A": ["f", "e", "g", "h"], "B": ["a", "b", "c", "d"], "C": [1, 2, 3, 4]}
25442555
).astype(object)
25452556
df.columns = ["A", "B", "B"]
2546-
result = df.groupby([0, 0, 0, 0]).min()
2557+
with pd.option_context("future.infer_string", infer_string):
2558+
result = df.groupby([0, 0, 0, 0]).min()
25472559
expected = DataFrame(
25482560
[["e", "a", 1]], index=np.array([0]), columns=["A", "B", "B"], dtype=object
25492561
)

0 commit comments

Comments
 (0)