diff --git a/pandas/tests/arrays/sparse/test_astype.py b/pandas/tests/arrays/sparse/test_astype.py index d729a31668ade..86d69610059b3 100644 --- a/pandas/tests/arrays/sparse/test_astype.py +++ b/pandas/tests/arrays/sparse/test_astype.py @@ -3,7 +3,11 @@ 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, @@ -131,3 +135,13 @@ 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_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")) + expected = Series( + ["a", "b", "b"], name="A", dtype=SparseDtype("object", fill_value="c") + ) + tm.assert_series_equal(result, expected)