Skip to content

Use repr of fill_value in SparseDtype repr #34352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ Sparse
- Creating a :class:`SparseArray` from timezone-aware dtype will issue a warning before dropping timezone information, instead of doing so silently (:issue:`32501`)
- Bug in :meth:`arrays.SparseArray.from_spmatrix` wrongly read scipy sparse matrix (:issue:`31991`)
- Bug in :meth:`Series.sum` with ``SparseArray`` raises ``TypeError`` (:issue:`25777`)
- The repr of :class:`SparseDtype` now includes the repr of its ``fill_value`` attribute. Previously it used ``fill_value``'s string representation (:issue:`34352`)

ExtensionArray
^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def subtype(self):

@property
def name(self):
return f"Sparse[{self.subtype.name}, {self.fill_value}]"
return f"Sparse[{self.subtype.name}, {repr(self.fill_value)}]"

def __repr__(self) -> str:
return self.name
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/arrays/sparse/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,14 @@ def test_update_dtype(original, dtype, expected):
def test_update_dtype_raises(original, dtype, expected_error_msg):
with pytest.raises(ValueError, match=expected_error_msg):
original.update_dtype(dtype)


def test_repr():
# GH-34352
result = str(pd.SparseDtype("int64", fill_value=0))
expected = "Sparse[int64, 0]"
assert result == expected

result = str(pd.SparseDtype(object, fill_value="0"))
expected = "Sparse[object, '0']"
assert result == expected