From 8e86026396999b1fbc9ded1d4476d5c2c1ecf888 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 8 Jan 2024 09:50:26 -0800 Subject: [PATCH 1/2] Fix to_numeric not preserving Series index and name --- 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..c39d91b94b1 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 = klass([1] * 8, index=range(2, 10), name="name") + assert_eq(result, expected) From 2fcc86d2b7566cd0fbe6dd570971374cdf646ed9 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 8 Jan 2024 09:51:46 -0800 Subject: [PATCH 2/2] expected is cudf.Series --- python/cudf/cudf/tests/test_numerical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/cudf/cudf/tests/test_numerical.py b/python/cudf/cudf/tests/test_numerical.py index c39d91b94b1..fee5cc0ad21 100644 --- a/python/cudf/cudf/tests/test_numerical.py +++ b/python/cudf/cudf/tests/test_numerical.py @@ -431,5 +431,5 @@ def test_series_to_numeric_bool(data, downcast): 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 = klass([1] * 8, index=range(2, 10), name="name") + expected = cudf.Series([1] * 8, index=range(2, 10), name="name") assert_eq(result, expected)