Skip to content

Commit 769a495

Browse files
committed
Filter warnings in init_spmatrix
Closes pandas-dev#26777
1 parent 2d2606d commit 769a495

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

pandas/core/sparse/frame.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -211,32 +211,37 @@ def _init_spmatrix(self, data, index, columns, dtype=None,
211211
"""
212212
Init self from scipy.sparse matrix.
213213
"""
214-
index, columns = SparseFrameAccessor._prep_index(data, index, columns)
215-
data = data.tocoo()
216-
N = len(index)
217-
218-
# Construct a dict of SparseSeries
219-
sdict = {}
220-
values = Series(data.data, index=data.row, copy=False)
221-
for col, rowvals in values.groupby(data.col):
222-
# get_blocks expects int32 row indices in sorted order
223-
rowvals = rowvals.sort_index()
224-
rows = rowvals.index.values.astype(np.int32)
225-
blocs, blens = get_blocks(rows)
226-
227-
sdict[columns[col]] = SparseSeries(
228-
rowvals.values, index=index,
229-
fill_value=fill_value,
230-
sparse_index=BlockIndex(N, blocs, blens))
231-
232-
# Add any columns that were empty and thus not grouped on above
233-
sdict.update({column: SparseSeries(index=index,
234-
fill_value=fill_value,
235-
sparse_index=BlockIndex(N, [], []))
236-
for column in columns
237-
if column not in sdict})
238-
239-
return self._init_dict(sdict, index, columns, dtype)
214+
with warnings.catch_warnings():
215+
warnings.filterwarnings('ignore', 'SparseSeries', FutureWarning)
216+
index, columns = SparseFrameAccessor._prep_index(data, index,
217+
columns)
218+
data = data.tocoo()
219+
N = len(index)
220+
221+
# Construct a dict of SparseSeries
222+
sdict = {}
223+
values = Series(data.data, index=data.row, copy=False)
224+
for col, rowvals in values.groupby(data.col):
225+
# get_blocks expects int32 row indices in sorted order
226+
rowvals = rowvals.sort_index()
227+
rows = rowvals.index.values.astype(np.int32)
228+
blocs, blens = get_blocks(rows)
229+
230+
sdict[columns[col]] = SparseSeries(
231+
rowvals.values, index=index,
232+
fill_value=fill_value,
233+
sparse_index=BlockIndex(N, blocs, blens))
234+
235+
# Add any columns that were empty and thus not grouped on above
236+
sdict.update({column: SparseSeries(index=index,
237+
fill_value=fill_value,
238+
sparse_index=BlockIndex(N,
239+
[],
240+
[]))
241+
for column in columns
242+
if column not in sdict})
243+
244+
return self._init_dict(sdict, index, columns, dtype)
240245

241246
@Appender(SparseFrameAccessor.to_coo.__doc__)
242247
def to_coo(self):

pandas/tests/sparse/frame/test_frame.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pandas._libs.sparse import BlockIndex, IntIndex
88
from pandas.errors import PerformanceWarning
9+
import pandas.util._test_decorators as td
910

1011
import pandas as pd
1112
from pandas import DataFrame, Series, bdate_range, compat
@@ -24,6 +25,18 @@ def test_deprecated():
2425
pd.SparseDataFrame({"A": [1, 2]})
2526

2627

28+
@td.skip_if_no_scipy
29+
def test_single_warning_from_spmatrix():
30+
import scipy.sparse
31+
32+
arr = scipy.sparse.rand(10, 10, 0.5)
33+
with tm.assert_produces_warning(FutureWarning,
34+
check_stacklevel=False) as w:
35+
pd.SparseDataFrame(arr)
36+
37+
assert len(w) == 1
38+
39+
2740
@pytest.mark.filterwarnings("ignore:Sparse:FutureWarning")
2841
class TestSparseDataFrame(SharedWithSparse):
2942
klass = SparseDataFrame

0 commit comments

Comments
 (0)