Skip to content

Commit 2251902

Browse files
authored
BUG: SparseDtype requires numpy dtype (#53160)
* BUG: SparseDtype requires numpy dtype * GH ref
1 parent a90fbc8 commit 2251902

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ Reshaping
426426

427427
Sparse
428428
^^^^^^
429+
- Bug in :class:`SparseDtype` constructor failing to raise ``TypeError`` when given an incompatible ``dtype`` for its subtype, which must be a ``numpy`` dtype (:issue:`53160`)
429430
- Bug in :meth:`arrays.SparseArray.map` allowed the fill value to be included in the sparse values (:issue:`52095`)
430431
-
431432

pandas/core/arrays/sparse/dtype.py

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def __init__(self, dtype: Dtype = np.float64, fill_value: Any = None) -> None:
9191
dtype = pandas_dtype(dtype)
9292
if is_string_dtype(dtype):
9393
dtype = np.dtype("object")
94+
if not isinstance(dtype, np.dtype):
95+
# GH#53160
96+
raise TypeError("SparseDtype subtype must be a numpy dtype")
9497

9598
if fill_value is None:
9699
fill_value = na_value_for_dtype(dtype)

pandas/tests/arrays/sparse/test_astype.py

+1-15
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
from pandas._libs.sparse import IntIndex
55

6-
from pandas import (
7-
DataFrame,
8-
Series,
9-
Timestamp,
10-
)
6+
from pandas import Timestamp
117
import pandas._testing as tm
128
from pandas.core.arrays.sparse import (
139
SparseArray,
@@ -135,13 +131,3 @@ def test_astype_dt64_to_int64(self):
135131
arr3 = SparseArray(values, dtype=dtype)
136132
result3 = arr3.astype("int64")
137133
tm.assert_numpy_array_equal(result3, expected)
138-
139-
140-
def test_dtype_sparse_with_fill_value_not_present_in_data():
141-
# GH 49987
142-
df = DataFrame([["a", 0], ["b", 1], ["b", 2]], columns=["A", "B"])
143-
result = df["A"].astype(SparseDtype("category", fill_value="c"))
144-
expected = Series(
145-
["a", "b", "b"], name="A", dtype=SparseDtype("object", fill_value="c")
146-
)
147-
tm.assert_series_equal(result, expected)

pandas/tests/arrays/sparse/test_dtype.py

+7
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,10 @@ def test_repr():
207207
result = str(SparseDtype(object, fill_value="0"))
208208
expected = "Sparse[object, '0']"
209209
assert result == expected
210+
211+
212+
def test_sparse_dtype_subtype_must_be_numpy_dtype():
213+
# GH#53160
214+
msg = "SparseDtype subtype must be a numpy dtype"
215+
with pytest.raises(TypeError, match=msg):
216+
SparseDtype("category", fill_value="c")

0 commit comments

Comments
 (0)