Skip to content

Commit

Permalink
Fix to_numeric not preserving Series index and name (#14718)
Browse files Browse the repository at this point in the history
closes #14717

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

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

URL: #14718
  • Loading branch information
mroeschke authored Jan 9, 2024
1 parent d81ca78 commit 3a1601d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions python/cudf/cudf/core/tools/numeric.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2023, NVIDIA CORPORATION.
# Copyright (c) 2018-2024, NVIDIA CORPORATION.

import warnings

Expand Down Expand Up @@ -161,7 +161,7 @@ def to_numeric(arg, errors="raise", downcast=None):
break

if isinstance(arg, (cudf.Series, pd.Series)):
return cudf.Series(col)
return cudf.Series(col, index=arg.index, name=arg.name)
else:
if col.has_nulls():
# To match pandas, always return a floating type filled with nan.
Expand Down
10 changes: 9 additions & 1 deletion python/cudf/cudf/tests/test_numerical.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2021-2023, NVIDIA CORPORATION.
# Copyright (c) 2021-2024, NVIDIA CORPORATION.

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -425,3 +425,11 @@ def test_series_to_numeric_bool(data, downcast):
got = cudf.to_numeric(gs, downcast=downcast)

assert_eq(expect, got)


@pytest.mark.parametrize("klass", [cudf.Series, pd.Series])
def test_series_to_numeric_preserve_index_name(klass):
ser = klass(["1"] * 8, index=range(2, 10), name="name")
result = cudf.to_numeric(ser)
expected = cudf.Series([1] * 8, index=range(2, 10), name="name")
assert_eq(result, expected)

0 comments on commit 3a1601d

Please sign in to comment.