From c4d1baee21008c9c775f7b4efd5b7047668ffb3d Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Sat, 14 Jan 2023 22:14:51 +0800 Subject: [PATCH 1/3] test dtype sparseDtype with specificd fill value --- pandas/tests/arrays/sparse/test_astype.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/arrays/sparse/test_astype.py b/pandas/tests/arrays/sparse/test_astype.py index d729a31668ade..bc372cc75da35 100644 --- a/pandas/tests/arrays/sparse/test_astype.py +++ b/pandas/tests/arrays/sparse/test_astype.py @@ -9,6 +9,8 @@ SparseArray, SparseDtype, ) +from pandas.core.frame import DataFrame +from pandas.core.series import Series class TestAstype: @@ -131,3 +133,14 @@ def test_astype_dt64_to_int64(self): arr3 = SparseArray(values, dtype=dtype) result3 = arr3.astype("int64") tm.assert_numpy_array_equal(result3, expected) + + +def test_dtype_sparse_with_specified_fill_value(): + # GH 49987 + # specify fill_value which not present in data + df = DataFrame([["a", 0], ["b", 1], ["b", 2]], columns=["A", "B"]) + result = df["A"].astype(SparseDtype("category", fill_value="c")) + expected = Series( + ["a", "b", "b"], name="A", dtype=SparseDtype("object", fill_value="c") + ) + tm.assert_series_equal(result, expected) From 436a56961f30bbcc7bddade100cb357ebf7215c1 Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Sun, 15 Jan 2023 11:14:48 +0800 Subject: [PATCH 2/3] rename and remove comment --- pandas/tests/arrays/sparse/test_astype.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/arrays/sparse/test_astype.py b/pandas/tests/arrays/sparse/test_astype.py index bc372cc75da35..aaa12339f482c 100644 --- a/pandas/tests/arrays/sparse/test_astype.py +++ b/pandas/tests/arrays/sparse/test_astype.py @@ -135,9 +135,8 @@ def test_astype_dt64_to_int64(self): tm.assert_numpy_array_equal(result3, expected) -def test_dtype_sparse_with_specified_fill_value(): +def test_dtype_sparse_with_fill_value_not_prsent_in_data(): # GH 49987 - # specify fill_value which not present in data df = DataFrame([["a", 0], ["b", 1], ["b", 2]], columns=["A", "B"]) result = df["A"].astype(SparseDtype("category", fill_value="c")) expected = Series( From 613ce4fe36f67b6efb3dd009792ba393b0a7745b Mon Sep 17 00:00:00 2001 From: luke <2736230899@qq.com> Date: Sun, 15 Jan 2023 19:04:58 +0800 Subject: [PATCH 3/3] fix typo and import --- pandas/tests/arrays/sparse/test_astype.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pandas/tests/arrays/sparse/test_astype.py b/pandas/tests/arrays/sparse/test_astype.py index aaa12339f482c..86d69610059b3 100644 --- a/pandas/tests/arrays/sparse/test_astype.py +++ b/pandas/tests/arrays/sparse/test_astype.py @@ -3,14 +3,16 @@ from pandas._libs.sparse import IntIndex -from pandas import Timestamp +from pandas import ( + DataFrame, + Series, + Timestamp, +) import pandas._testing as tm from pandas.core.arrays.sparse import ( SparseArray, SparseDtype, ) -from pandas.core.frame import DataFrame -from pandas.core.series import Series class TestAstype: @@ -135,7 +137,7 @@ def test_astype_dt64_to_int64(self): tm.assert_numpy_array_equal(result3, expected) -def test_dtype_sparse_with_fill_value_not_prsent_in_data(): +def test_dtype_sparse_with_fill_value_not_present_in_data(): # GH 49987 df = DataFrame([["a", 0], ["b", 1], ["b", 2]], columns=["A", "B"]) result = df["A"].astype(SparseDtype("category", fill_value="c"))