Skip to content

Commit

Permalink
Fix DataFrame.reindex removing column name (#14601)
Browse files Browse the repository at this point in the history
Fixes the following behavior

```python
In [1]: import cudf
    
In [2]: import pandas as pd

In [3]: df = cudf.DataFrame([1], columns=cudf.Index([1], name="foo"))

In [4]: df
Out[4]: 
foo  1
0    1

In [5]: df = df.reindex(index=[0, 1])

In [6]: df
Out[6]: 
      1  <-- foo missing
0     1
1  <NA>
```

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

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #14601
  • Loading branch information
mroeschke authored Dec 8, 2023
1 parent 016cf1e commit 8c511d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 7 additions & 3 deletions python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2661,13 +2661,17 @@ def _reindex(
)
for name in names
}
if column_names is None:
level_names = self._data.level_names
elif isinstance(column_names, pd.Index):
level_names = tuple(column_names.names)
else:
level_names = None
result = self.__class__._from_data(
data=cudf.core.column_accessor.ColumnAccessor(
cols,
multiindex=self._data.multiindex,
level_names=tuple(column_names.names)
if isinstance(column_names, pd.Index)
else None,
level_names=level_names,
),
index=index,
)
Expand Down
9 changes: 9 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10754,6 +10754,15 @@ def test_dataframe_series_dot():
assert_eq(expected, actual)


def test_dataframe_reindex_keep_colname():
gdf = cudf.DataFrame([1], columns=cudf.Index([1], name="foo"))
result = gdf.reindex(index=[0, 1])
expected = cudf.DataFrame(
[1, None], columns=cudf.Index([1], name="foo"), index=[0, 1]
)
assert_eq(result, expected)


def test_dataframe_duplicate_index_reindex():
gdf = cudf.DataFrame({"a": [0, 1, 2, 3]}, index=[0, 0, 1, 1])
pdf = gdf.to_pandas()
Expand Down

0 comments on commit 8c511d1

Please sign in to comment.