Skip to content

Commit a8b0d65

Browse files
committed
warning for sparse
1 parent a4a21ae commit a8b0d65

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

pandas/compat/pickle_compat.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import copy
66
import pickle as pkl
77
import sys
8-
from typing import Any
8+
from typing import TYPE_CHECKING
9+
import warnings
910

1011
from pandas import Index
1112

13+
if TYPE_CHECKING:
14+
from pandas._typing import FrameOrSeries
15+
1216

1317
def load_reduce(self):
1418
stack = self.stack
@@ -55,19 +59,38 @@ def load_reduce(self):
5559
raise
5660

5761

62+
_sparse_msg = """\
63+
64+
Loading a saved '{cls}' as a {new} with sparse values.
65+
'{cls}' is now removed. You should re-save this dataset in its new format.
66+
"""
67+
68+
5869
class _LoadSparseSeries:
5970
# To load a SparseSeries as a Series[Sparse]
60-
def __new__(cls) -> Any:
71+
def __new__(cls) -> FrameOrSeries:
6172
from pandas import Series
6273

74+
warnings.warn(
75+
_sparse_msg.format(cls="SparseSeries", new="Series"),
76+
FutureWarning,
77+
stacklevel=6,
78+
)
79+
6380
return Series()
6481

6582

6683
class _LoadSparseFrame:
6784
# To load a SparseDataFrame as a DataFrame[Sparse]
68-
def __new__(cls) -> Any:
85+
def __new__(cls) -> FrameOrSeries:
6986
from pandas import DataFrame
7087

88+
warnings.warn(
89+
_sparse_msg.format(cls="SparseDataFrame", new="DataFrame"),
90+
FutureWarning,
91+
stacklevel=6,
92+
)
93+
7194
return DataFrame()
7295

7396

625 Bytes
Binary file not shown.
521 Bytes
Binary file not shown.

pandas/tests/io/test_pickle.py

+26
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,32 @@ def test_pickle_path_localpath():
228228
tm.assert_frame_equal(df, result)
229229

230230

231+
def test_legacy_sparse_warning(datapath):
232+
"""
233+
234+
Generated with
235+
236+
>>> df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [0, 0, 1, 1]}).to_sparse()
237+
>>> df.to_pickle("pandas/tests/io/data/sparseframe-0.20.3.pickle.gz",
238+
... compression="gzip")
239+
240+
>>> s = df['B']
241+
>>> s.to_pickle("pandas/tests/io/data/sparseseries-0.20.3.pickle.gz",
242+
... compression="gzip")
243+
"""
244+
with tm.assert_produces_warning(FutureWarning):
245+
simplefilter("ignore", DeprecationWarning) # from boto
246+
pd.read_pickle(
247+
datapath("io", "data", "sparseseries-0.20.3.pickle.gz"), compression="gzip"
248+
)
249+
250+
with tm.assert_produces_warning(FutureWarning):
251+
simplefilter("ignore", DeprecationWarning) # from boto
252+
pd.read_pickle(
253+
datapath("io", "data", "sparseframe-0.20.3.pickle.gz"), compression="gzip"
254+
)
255+
256+
231257
# ---------------------
232258
# test pickle compression
233259
# ---------------------

0 commit comments

Comments
 (0)