Skip to content

Commit

Permalink
Fix reductions when DataFrame has MulitIndex columns (#15097)
Browse files Browse the repository at this point in the history
closes #15085

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)
  - Bradley Dice (https://github.com/bdice)

URL: #15097
  • Loading branch information
mroeschke authored Feb 21, 2024
1 parent 63e9040 commit 4ce99af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3955,7 +3955,6 @@ def transpose(self):
Not supporting *copy* because default and only behavior is
copy=True
"""

index = self._data.to_pandas_index()
columns = self.index.copy(deep=False)
if self._num_columns == 0 or self._num_rows == 0:
Expand Down Expand Up @@ -6202,9 +6201,13 @@ def _reduce(
"Columns must all have the same dtype to "
f"perform {op=} with {axis=}"
)
return Series._from_data(
{None: as_column(result)}, as_index(source._data.names)
)
if source._data.multiindex:
idx = MultiIndex.from_tuples(
source._data.names, names=source._data.level_names
)
else:
idx = as_index(source._data.names)
return Series._from_data({None: as_column(result)}, idx)
elif axis == 1:
return source._apply_cupy_method_axis_1(op, **kwargs)
else:
Expand Down
10 changes: 10 additions & 0 deletions python/cudf/cudf/tests/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,13 @@ def test_reductions_axis_none_warning(op):
):
expected = getattr(pdf, op)(axis=None)
assert_eq(expected, actual, check_dtype=False)


def test_reduction_column_multiindex():
idx = cudf.MultiIndex.from_tuples(
[("a", 1), ("a", 2)], names=["foo", "bar"]
)
df = cudf.DataFrame(np.array([[1, 3], [2, 4]]), columns=idx)
result = df.mean()
expected = df.to_pandas().mean()
assert_eq(result, expected)

0 comments on commit 4ce99af

Please sign in to comment.