Skip to content

Commit 3622f15

Browse files
authored
REGR: groupby.size with axis=1 doesn't return a Series (#48760)
* REGR: groupby.size with axis=1 doesn't return a Series * Remove code block
1 parent 71c94c3 commit 3622f15

File tree

4 files changed

+11
-19
lines changed

4 files changed

+11
-19
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Fixed regressions
7676
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)
7777
- Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)
7878
- Fixed regression causing an ``AttributeError`` during warning emitted if the provided table name in :meth:`DataFrame.to_sql` and the table name actually used in the database do not match (:issue:`48733`)
79+
- Fixed :meth:`.DataFrameGroupBy.size` not returning a Series when ``axis=1`` (:issue:`48738`)
7980

8081
.. ---------------------------------------------------------------------------
8182

pandas/core/groupby/groupby.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -1865,11 +1865,13 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:
18651865
out = algorithms.take_nd(result._values, ids)
18661866
output = obj._constructor(out, index=obj.index, name=obj.name)
18671867
else:
1868+
# `.size()` gives Series output on DataFrame input, need axis 0
1869+
axis = 0 if result.ndim == 1 else self.axis
18681870
# GH#46209
18691871
# Don't convert indices: negative indices need to give rise
18701872
# to null values in the result
1871-
output = result._take(ids, axis=self.axis, convert_indices=False)
1872-
output = output.set_axis(obj._get_axis(self.axis), axis=self.axis)
1873+
output = result._take(ids, axis=axis, convert_indices=False)
1874+
output = output.set_axis(obj._get_axis(self.axis), axis=axis)
18731875
return output
18741876

18751877
# -----------------------------------------------------------------
@@ -2393,13 +2395,6 @@ def size(self) -> DataFrame | Series:
23932395
"""
23942396
result = self.grouper.size()
23952397

2396-
if self.axis == 1:
2397-
return DataFrame(
2398-
data=np.tile(result.values, (self.obj.shape[0], 1)),
2399-
columns=result.index,
2400-
index=self.obj.index,
2401-
)
2402-
24032398
# GH28330 preserve subclassed Series/DataFrames through calls
24042399
if isinstance(self.obj, Series):
24052400
result = self._obj_1d_constructor(result, name=self.obj.name)

pandas/tests/groupby/test_size.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def test_size_axis_1(df, axis_1, by, sort, dropna):
3333
counts = {key: sum(value == key for value in by) for key in dict.fromkeys(by)}
3434
if dropna:
3535
counts = {key: value for key, value in counts.items() if key is not None}
36-
expected = DataFrame(counts, index=df.index)
36+
expected = Series(counts)
3737
if sort:
38-
expected = expected.sort_index(axis=1)
38+
expected = expected.sort_index()
3939
grouped = df.groupby(by=by, axis=axis_1, sort=sort, dropna=dropna)
4040
result = grouped.size()
41-
tm.assert_frame_equal(result, expected)
41+
tm.assert_series_equal(result, expected)
4242

4343

4444
@pytest.mark.parametrize("by", ["A", "B", ["A", "B"]])

pandas/tests/groupby/transform/test_transform.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,9 @@ def test_transform_axis_1_reducer(request, reduction_func):
213213
df = DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]}, index=["x", "y"])
214214
with tm.assert_produces_warning(warn, match=msg):
215215
result = df.groupby([0, 0, 1], axis=1).transform(reduction_func)
216-
if reduction_func == "size":
217-
# size doesn't behave in the same manner; hardcode expected result
218-
expected = DataFrame(2 * [[2, 2, 1]], index=df.index, columns=df.columns)
219-
else:
220-
warn = FutureWarning if reduction_func == "mad" else None
221-
with tm.assert_produces_warning(warn, match="The 'mad' method is deprecated"):
222-
expected = df.T.groupby([0, 0, 1]).transform(reduction_func).T
216+
warn = FutureWarning if reduction_func == "mad" else None
217+
with tm.assert_produces_warning(warn, match="The 'mad' method is deprecated"):
218+
expected = df.T.groupby([0, 0, 1]).transform(reduction_func).T
223219
tm.assert_equal(result, expected)
224220

225221

0 commit comments

Comments
 (0)