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 2 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
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
10 changes: 10 additions & 0 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ class TestPrinting(BaseSparseTests, base.BasePrintingTests):
def test_array_repr(self, data, size):
super().test_array_repr(data, size)

def test_fillna_repr(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this test in one of the files in tests/arrays/sparse/.. (eg in test_dtype.py)?

(trying to keep those extension tests only the shared common ones inherted from the base tests, any custom test goes into tests/arrays instead of tests/extension/)

# 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


class TestParsing(BaseSparseTests, base.BaseParsingTests):
@pytest.mark.parametrize("engine", ["c", "python"])
Expand Down