From 2dc145c9ccc46ab5335baf53ffc5950b92a47a17 Mon Sep 17 00:00:00 2001 From: Kernc Date: Thu, 23 Feb 2017 19:37:12 +0100 Subject: [PATCH] BUG: repr SparseDataFrame after setting a value --- doc/source/whatsnew/v0.20.0.txt | 2 +- pandas/formats/format.py | 3 --- pandas/tests/sparse/test_format.py | 12 ++++++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index c94429b469641..3c161413c08c0 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -564,7 +564,7 @@ Bug Fixes - Bug in ``to_sql`` when writing a DataFrame with numeric index names (:issue:`15404`). - Bug in ``Series.iloc`` where a ``Categorical`` object for list-like indexes input was returned, where a ``Series`` was expected. (:issue:`14580`) - +- Bug in repr-formatting a ``SparseDataFrame`` after a value was set on (a copy of) one of its series (:issue:`15488`) - Bug in groupby operations with timedelta64 when passing ``numeric_only=False`` (:issue:`5724`) diff --git a/pandas/formats/format.py b/pandas/formats/format.py index 4c081770e0125..caf8ae7d02a46 100644 --- a/pandas/formats/format.py +++ b/pandas/formats/format.py @@ -712,9 +712,6 @@ def to_html(self, classes=None, notebook=False, border=None): def _get_formatted_column_labels(self, frame): from pandas.core.index import _sparsify - def is_numeric_dtype(dtype): - return issubclass(dtype.type, np.number) - columns = frame.columns if isinstance(columns, MultiIndex): diff --git a/pandas/tests/sparse/test_format.py b/pandas/tests/sparse/test_format.py index 0c0e773d19bb9..ba870a2c33801 100644 --- a/pandas/tests/sparse/test_format.py +++ b/pandas/tests/sparse/test_format.py @@ -116,3 +116,15 @@ def test_sparse_frame(self): with option_context("display.max_rows", 3): self.assertEqual(repr(sparse), repr(df)) + + def test_sparse_repr_after_set(self): + # GH 15488 + sdf = pd.SparseDataFrame([[np.nan, 1], [2, np.nan]]) + res = sdf.copy() + + # Ignore the warning + with pd.option_context('mode.chained_assignment', None): + sdf[0][1] = 2 # This line triggers the bug + + repr(sdf) + tm.assert_sp_frame_equal(sdf, res)