diff --git a/pandas/compat/pickle_compat.py b/pandas/compat/pickle_compat.py index 87c8b92f4b454..c88d08ec37fcd 100644 --- a/pandas/compat/pickle_compat.py +++ b/pandas/compat/pickle_compat.py @@ -5,10 +5,14 @@ import copy import pickle as pkl import sys -from typing import Any +from typing import TYPE_CHECKING +import warnings from pandas import Index +if TYPE_CHECKING: + from pandas._typing import FrameOrSeries + def load_reduce(self): stack = self.stack @@ -55,19 +59,38 @@ def load_reduce(self): raise +_sparse_msg = """\ + +Loading a saved '{cls}' as a {new} with sparse values. +'{cls}' is now removed. You should re-save this dataset in its new format. +""" + + class _LoadSparseSeries: # To load a SparseSeries as a Series[Sparse] - def __new__(cls) -> Any: + def __new__(cls) -> FrameOrSeries: from pandas import Series + warnings.warn( + _sparse_msg.format(cls="SparseSeries", new="Series"), + FutureWarning, + stacklevel=6, + ) + return Series() class _LoadSparseFrame: # To load a SparseDataFrame as a DataFrame[Sparse] - def __new__(cls) -> Any: + def __new__(cls) -> FrameOrSeries: from pandas import DataFrame + warnings.warn( + _sparse_msg.format(cls="SparseDataFrame", new="DataFrame"), + FutureWarning, + stacklevel=6, + ) + return DataFrame() diff --git a/pandas/tests/io/data/sparseframe-0.20.3.pickle.gz b/pandas/tests/io/data/sparseframe-0.20.3.pickle.gz new file mode 100644 index 0000000000000..f4ff0dbaa1ff9 Binary files /dev/null and b/pandas/tests/io/data/sparseframe-0.20.3.pickle.gz differ diff --git a/pandas/tests/io/data/sparseseries-0.20.3.pickle.gz b/pandas/tests/io/data/sparseseries-0.20.3.pickle.gz new file mode 100644 index 0000000000000..b299e7d85808e Binary files /dev/null and b/pandas/tests/io/data/sparseseries-0.20.3.pickle.gz differ diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py index b5014021def5b..4124c9aff9d34 100644 --- a/pandas/tests/io/test_pickle.py +++ b/pandas/tests/io/test_pickle.py @@ -228,6 +228,32 @@ def test_pickle_path_localpath(): tm.assert_frame_equal(df, result) +def test_legacy_sparse_warning(datapath): + """ + + Generated with + + >>> df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [0, 0, 1, 1]}).to_sparse() + >>> df.to_pickle("pandas/tests/io/data/sparseframe-0.20.3.pickle.gz", + ... compression="gzip") + + >>> s = df['B'] + >>> s.to_pickle("pandas/tests/io/data/sparseseries-0.20.3.pickle.gz", + ... compression="gzip") + """ + with tm.assert_produces_warning(FutureWarning): + simplefilter("ignore", DeprecationWarning) # from boto + pd.read_pickle( + datapath("io", "data", "sparseseries-0.20.3.pickle.gz"), compression="gzip" + ) + + with tm.assert_produces_warning(FutureWarning): + simplefilter("ignore", DeprecationWarning) # from boto + pd.read_pickle( + datapath("io", "data", "sparseframe-0.20.3.pickle.gz"), compression="gzip" + ) + + # --------------------- # test pickle compression # ---------------------