Skip to content

Filter warning in sparse repr #26669

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 1 commit into from
Jun 5, 2019
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
5 changes: 5 additions & 0 deletions pandas/core/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ def _init_spmatrix(self, data, index, columns, dtype=None,
def to_coo(self):
return SparseFrameAccessor(self).to_coo()

def __repr__(self):
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Sparse")
return super().__repr__()

def __getstate__(self):
# pickling
return dict(_typ=self._typ, _subtyp=self._subtyp, _data=self._data,
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,12 @@ def as_sparse_array(self, kind=None, fill_value=None, copy=False):
fill_value=fill_value, kind=kind, copy=copy)

def __repr__(self):
series_rep = Series.__repr__(self)
rep = '{series}\n{index!r}'.format(series=series_rep,
index=self.sp_index)
return rep
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Sparse")
series_rep = Series.__repr__(self)
rep = '{series}\n{index!r}'.format(series=series_rep,
index=self.sp_index)
return rep

def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
filter_type=None, **kwds):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/sparse/test_format.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np
import pytest

Expand Down Expand Up @@ -133,3 +135,14 @@ def test_sparse_repr_after_set(self):

repr(sdf)
tm.assert_sp_frame_equal(sdf, res)


def test_repr_no_warning():
with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
df = pd.SparseDataFrame({"A": [1, 2]})
s = df['A']

with tm.assert_produces_warning(None):
repr(df)
repr(s)