Skip to content

Commit f5ab5a8

Browse files
authored
Use repr of fill_value in SparseDtype repr (pandas-dev#34352)
* use repr of fill_value in sparse dtype repr * add gh-number * move tests to arrays/sparse/test_dtype.py * whatsnew entry
1 parent 014d8ea commit f5ab5a8

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ Sparse
865865
- Creating a :class:`SparseArray` from timezone-aware dtype will issue a warning before dropping timezone information, instead of doing so silently (:issue:`32501`)
866866
- Bug in :meth:`arrays.SparseArray.from_spmatrix` wrongly read scipy sparse matrix (:issue:`31991`)
867867
- Bug in :meth:`Series.sum` with ``SparseArray`` raises ``TypeError`` (:issue:`25777`)
868+
- The repr of :class:`SparseDtype` now includes the repr of its ``fill_value`` attribute. Previously it used ``fill_value``'s string representation (:issue:`34352`)
868869

869870
ExtensionArray
870871
^^^^^^^^^^^^^^

pandas/core/arrays/sparse/dtype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def subtype(self):
166166

167167
@property
168168
def name(self):
169-
return f"Sparse[{self.subtype.name}, {self.fill_value}]"
169+
return f"Sparse[{self.subtype.name}, {repr(self.fill_value)}]"
170170

171171
def __repr__(self) -> str:
172172
return self.name

pandas/tests/arrays/sparse/test_dtype.py

+11
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,14 @@ def test_update_dtype(original, dtype, expected):
196196
def test_update_dtype_raises(original, dtype, expected_error_msg):
197197
with pytest.raises(ValueError, match=expected_error_msg):
198198
original.update_dtype(dtype)
199+
200+
201+
def test_repr():
202+
# GH-34352
203+
result = str(pd.SparseDtype("int64", fill_value=0))
204+
expected = "Sparse[int64, 0]"
205+
assert result == expected
206+
207+
result = str(pd.SparseDtype(object, fill_value="0"))
208+
expected = "Sparse[object, '0']"
209+
assert result == expected

0 commit comments

Comments
 (0)