Skip to content

Commit

Permalink
DEPR: Remove SparseDataFrame/Series pickle compat (#49316)
Browse files Browse the repository at this point in the history
* DEPR: Remove SparseDataFrame/Series pickle compat

* Add to whatsnew
  • Loading branch information
mroeschke authored Oct 26, 2022
1 parent 6ee0acb commit 3288d8f
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 93 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Removal of prior version deprecations/changes
- Removed ``pandas.util.testing`` in favor of ``pandas.testing`` (:issue:`30745`)
- Removed :meth:`Series.str.__iter__` (:issue:`28277`)
- Removed ``pandas.SparseArray`` in favor of :class:`arrays.SparseArray` (:issue:`30642`)
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame`` (:issue:`30642`)
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame``, including pickle support. (:issue:`30642`)
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
- Enforced disallowing a tuple of column labels into :meth:`.DataFrameGroupBy.__getitem__` (:issue:`30546`)
- Removed setting Categorical._codes directly (:issue:`41429`)
Expand Down
71 changes: 1 addition & 70 deletions pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
import copy
import io
import pickle as pkl
from typing import (
TYPE_CHECKING,
Generator,
)
import warnings
from typing import Generator

import numpy as np

Expand All @@ -26,12 +22,6 @@
)
from pandas.core.internals import BlockManager

if TYPE_CHECKING:
from pandas import (
DataFrame,
Series,
)


def load_reduce(self):
stack = self.stack
Expand Down Expand Up @@ -68,49 +58,6 @@ 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]

# https://github.com/python/mypy/issues/1020
# error: Incompatible return type for "__new__" (returns "Series", but must return
# a subtype of "_LoadSparseSeries")
def __new__(cls) -> Series: # type: ignore[misc]
from pandas import Series

warnings.warn(
_sparse_msg.format(cls="SparseSeries", new="Series"),
FutureWarning,
stacklevel=6,
)

return Series(dtype=object)


class _LoadSparseFrame:
# To load a SparseDataFrame as a DataFrame[Sparse]

# https://github.com/python/mypy/issues/1020
# error: Incompatible return type for "__new__" (returns "DataFrame", but must
# return a subtype of "_LoadSparseFrame")
def __new__(cls) -> DataFrame: # type: ignore[misc]
from pandas import DataFrame

warnings.warn(
_sparse_msg.format(cls="SparseDataFrame", new="DataFrame"),
FutureWarning,
stacklevel=6,
)

return DataFrame()


# If classes are moved, provide compat here.
_class_locations_map = {
("pandas.core.sparse.array", "SparseArray"): ("pandas.core.arrays", "SparseArray"),
Expand Down Expand Up @@ -144,14 +91,6 @@ def __new__(cls) -> DataFrame: # type: ignore[misc]
"pandas.core.arrays.sparse",
"SparseArray",
),
("pandas.sparse.series", "SparseSeries"): (
"pandas.compat.pickle_compat",
"_LoadSparseSeries",
),
("pandas.sparse.frame", "SparseDataFrame"): (
"pandas.core.sparse.frame",
"_LoadSparseFrame",
),
("pandas.indexes.base", "_new_Index"): ("pandas.core.indexes.base", "_new_Index"),
("pandas.indexes.base", "Index"): ("pandas.core.indexes.base", "Index"),
("pandas.indexes.numeric", "Int64Index"): (
Expand Down Expand Up @@ -183,14 +122,6 @@ def __new__(cls) -> DataFrame: # type: ignore[misc]
"pandas.core.indexes.numeric",
"Float64Index",
),
("pandas.core.sparse.series", "SparseSeries"): (
"pandas.compat.pickle_compat",
"_LoadSparseSeries",
),
("pandas.core.sparse.frame", "SparseDataFrame"): (
"pandas.compat.pickle_compat",
"_LoadSparseFrame",
),
}


Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
22 changes: 0 additions & 22 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,28 +254,6 @@ def test_pickle_path_localpath():
tm.assert_frame_equal(df, result)


@pytest.mark.parametrize("typ", ["sparseseries", "sparseframe"])
def test_legacy_sparse_warning(datapath, typ):
"""
Generated with
>>> df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [0, 0, 1, 1]}).to_sparse()
>>> df.to_pickle("pandas/tests/io/data/pickle/sparseframe-0.20.3.pickle.gz",
... compression="gzip")
>>> s = df['B']
>>> s.to_pickle("pandas/tests/io/data/pickle/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", "pickle", f"{typ}-0.20.3.pickle.gz"),
compression="gzip",
)


# ---------------------
# test pickle compression
# ---------------------
Expand Down

0 comments on commit 3288d8f

Please sign in to comment.