Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to_numeric not preserving Series index and name #14718

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)