Skip to content

Commit

Permalink
warning for sparse
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Sep 17, 2019
1 parent a4a21ae commit a8b0d65
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
29 changes: 26 additions & 3 deletions pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()


Expand Down
Binary file added pandas/tests/io/data/sparseframe-0.20.3.pickle.gz
Binary file not shown.
Binary file added pandas/tests/io/data/sparseseries-0.20.3.pickle.gz
Binary file not shown.
26 changes: 26 additions & 0 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ---------------------
Expand Down

0 comments on commit a8b0d65

Please sign in to comment.