Skip to content

Commit 891a419

Browse files
TomAugspurgerjreback
authored andcommitted
filter warning in repr (#26669)
1 parent f8b4c57 commit 891a419

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

pandas/core/sparse/frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ def _init_spmatrix(self, data, index, columns, dtype=None,
242242
def to_coo(self):
243243
return SparseFrameAccessor(self).to_coo()
244244

245+
def __repr__(self):
246+
with warnings.catch_warnings():
247+
warnings.filterwarnings("ignore", "Sparse")
248+
return super().__repr__()
249+
245250
def __getstate__(self):
246251
# pickling
247252
return dict(_typ=self._typ, _subtyp=self._subtyp, _data=self._data,

pandas/core/sparse/series.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,12 @@ def as_sparse_array(self, kind=None, fill_value=None, copy=False):
214214
fill_value=fill_value, kind=kind, copy=copy)
215215

216216
def __repr__(self):
217-
series_rep = Series.__repr__(self)
218-
rep = '{series}\n{index!r}'.format(series=series_rep,
219-
index=self.sp_index)
220-
return rep
217+
with warnings.catch_warnings():
218+
warnings.filterwarnings("ignore", "Sparse")
219+
series_rep = Series.__repr__(self)
220+
rep = '{series}\n{index!r}'.format(series=series_rep,
221+
index=self.sp_index)
222+
return rep
221223

222224
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
223225
filter_type=None, **kwds):

pandas/tests/sparse/test_format.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import numpy as np
24
import pytest
35

@@ -133,3 +135,14 @@ def test_sparse_repr_after_set(self):
133135

134136
repr(sdf)
135137
tm.assert_sp_frame_equal(sdf, res)
138+
139+
140+
def test_repr_no_warning():
141+
with warnings.catch_warnings():
142+
warnings.simplefilter("ignore", FutureWarning)
143+
df = pd.SparseDataFrame({"A": [1, 2]})
144+
s = df['A']
145+
146+
with tm.assert_produces_warning(None):
147+
repr(df)
148+
repr(s)

0 commit comments

Comments
 (0)