Skip to content

Commit 6c69cf4

Browse files
committed
REGR: groupby.size with axis=1 doesn't return a Series (pandas-dev#48760)
* REGR: groupby.size with axis=1 doesn't return a Series * Remove code block (cherry picked from commit 3622f15)
1 parent a46ae6e commit 6c69cf4

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 in :meth:`DataFrameGroupBy.apply` when user defined function is called on an empty dataframe (:issue:`47985`)
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
@@ -1869,11 +1869,13 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:
18691869
out = algorithms.take_nd(result._values, ids)
18701870
output = obj._constructor(out, index=obj.index, name=obj.name)
18711871
else:
1872+
# `.size()` gives Series output on DataFrame input, need axis 0
1873+
axis = 0 if result.ndim == 1 else self.axis
18721874
# GH#46209
18731875
# Don't convert indices: negative indices need to give rise
18741876
# to null values in the result
1875-
output = result._take(ids, axis=self.axis, convert_indices=False)
1876-
output = output.set_axis(obj._get_axis(self.axis), axis=self.axis)
1877+
output = result._take(ids, axis=axis, convert_indices=False)
1878+
output = output.set_axis(obj._get_axis(self.axis), axis=axis)
18771879
return output
18781880

18791881
# -----------------------------------------------------------------
@@ -2397,13 +2399,6 @@ def size(self) -> DataFrame | Series:
23972399
"""
23982400
result = self.grouper.size()
23992401

2400-
if self.axis == 1:
2401-
return DataFrame(
2402-
data=np.tile(result.values, (self.obj.shape[0], 1)),
2403-
columns=result.index,
2404-
index=self.obj.index,
2405-
)
2406-
24072402
# GH28330 preserve subclassed Series/DataFrames through calls
24082403
if isinstance(self.obj, Series):
24092404
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)