From 3a1601d61437b339c47a015dab7a830998b182f9 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:07:50 -1000 Subject: [PATCH] Fix to_numeric not preserving Series index and name (#14718) closes #14717 Authors: - Matthew Roeschke (https://github.com/mroeschke) Approvers: - Bradley Dice (https://github.com/bdice) URL: https://github.com/rapidsai/cudf/pull/14718 --- python/cudf/cudf/core/tools/numeric.py | 4 ++-- python/cudf/cudf/tests/test_numerical.py | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/python/cudf/cudf/core/tools/numeric.py b/python/cudf/cudf/core/tools/numeric.py index b1bcf4b98c5..a28c679b8be 100644 --- a/python/cudf/cudf/core/tools/numeric.py +++ b/python/cudf/cudf/core/tools/numeric.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018-2023, NVIDIA CORPORATION. +# Copyright (c) 2018-2024, NVIDIA CORPORATION. import warnings @@ -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. diff --git a/python/cudf/cudf/tests/test_numerical.py b/python/cudf/cudf/tests/test_numerical.py index 5bb55c164fe..fee5cc0ad21 100644 --- a/python/cudf/cudf/tests/test_numerical.py +++ b/python/cudf/cudf/tests/test_numerical.py @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023, NVIDIA CORPORATION. +# Copyright (c) 2021-2024, NVIDIA CORPORATION. import numpy as np import pandas as pd @@ -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)