From 7359e36299b09742cbbdb479266a6c9a88fb1aef Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 24 May 2020 11:42:48 +0100 Subject: [PATCH 1/4] use repr of fill_value in sparse dtype repr --- pandas/core/arrays/sparse/dtype.py | 2 +- pandas/tests/extension/test_sparse.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/sparse/dtype.py b/pandas/core/arrays/sparse/dtype.py index 156a90f6ce600..a9090570e64a9 100644 --- a/pandas/core/arrays/sparse/dtype.py +++ b/pandas/core/arrays/sparse/dtype.py @@ -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 diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index e59b3f0600867..aed43a0be42c6 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -402,6 +402,14 @@ class TestPrinting(BaseSparseTests, base.BasePrintingTests): def test_array_repr(self, data, size): super().test_array_repr(data, size) + def test_fillna_repr(self): + 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"]) From 8c6546ed5a845b816a25ea5d3a112402083a6772 Mon Sep 17 00:00:00 2001 From: tp Date: Sun, 24 May 2020 11:52:41 +0100 Subject: [PATCH 2/4] add gh-number --- pandas/tests/extension/test_sparse.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index aed43a0be42c6..07a0cb73b80da 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -403,6 +403,7 @@ def test_array_repr(self, data, size): super().test_array_repr(data, size) def test_fillna_repr(self): + # GH-34352 result = str(pd.SparseDtype("int64", fill_value=0)) expected = "Sparse[int64, 0]" assert result == expected @@ -411,6 +412,7 @@ def test_fillna_repr(self): expected = "Sparse[object, '0']" assert result == expected + class TestParsing(BaseSparseTests, base.BaseParsingTests): @pytest.mark.parametrize("engine", ["c", "python"]) def test_EA_types(self, engine, data): From 81aff87fab5f340a4fb4853af62facafd8412f12 Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 25 May 2020 12:47:15 +0100 Subject: [PATCH 3/4] move tests to arrays/sparse/test_dtype.py --- pandas/tests/arrays/sparse/test_dtype.py | 11 +++++++++++ pandas/tests/extension/test_sparse.py | 10 ---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pandas/tests/arrays/sparse/test_dtype.py b/pandas/tests/arrays/sparse/test_dtype.py index 5e9e2d854f577..16b4dd5c95932 100644 --- a/pandas/tests/arrays/sparse/test_dtype.py +++ b/pandas/tests/arrays/sparse/test_dtype.py @@ -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 diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index 07a0cb73b80da..e59b3f0600867 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -402,16 +402,6 @@ class TestPrinting(BaseSparseTests, base.BasePrintingTests): def test_array_repr(self, data, size): super().test_array_repr(data, size) - def test_fillna_repr(self): - # 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"]) From e495b359804cf6988a38de418e77ecfeb1eff85d Mon Sep 17 00:00:00 2001 From: tp Date: Mon, 25 May 2020 19:10:19 +0100 Subject: [PATCH 4/4] whatsnew entry --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index bfe2dcee40d5e..c4ac2e779cbfc 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -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 ^^^^^^^^^^^^^^