Skip to content

Commit 3288d8f

Browse files
authored
DEPR: Remove SparseDataFrame/Series pickle compat (#49316)
* DEPR: Remove SparseDataFrame/Series pickle compat * Add to whatsnew
1 parent 6ee0acb commit 3288d8f

File tree

7 files changed

+2
-93
lines changed

7 files changed

+2
-93
lines changed

doc/source/whatsnew/v2.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Removal of prior version deprecations/changes
228228
- Removed ``pandas.util.testing`` in favor of ``pandas.testing`` (:issue:`30745`)
229229
- Removed :meth:`Series.str.__iter__` (:issue:`28277`)
230230
- Removed ``pandas.SparseArray`` in favor of :class:`arrays.SparseArray` (:issue:`30642`)
231-
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame`` (:issue:`30642`)
231+
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame``, including pickle support. (:issue:`30642`)
232232
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
233233
- Enforced disallowing a tuple of column labels into :meth:`.DataFrameGroupBy.__getitem__` (:issue:`30546`)
234234
- Removed setting Categorical._codes directly (:issue:`41429`)

pandas/compat/pickle_compat.py

+1-70
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
import copy
88
import io
99
import pickle as pkl
10-
from typing import (
11-
TYPE_CHECKING,
12-
Generator,
13-
)
14-
import warnings
10+
from typing import Generator
1511

1612
import numpy as np
1713

@@ -26,12 +22,6 @@
2622
)
2723
from pandas.core.internals import BlockManager
2824

29-
if TYPE_CHECKING:
30-
from pandas import (
31-
DataFrame,
32-
Series,
33-
)
34-
3525

3626
def load_reduce(self):
3727
stack = self.stack
@@ -68,49 +58,6 @@ def load_reduce(self):
6858
raise
6959

7060

71-
_sparse_msg = """\
72-
73-
Loading a saved '{cls}' as a {new} with sparse values.
74-
'{cls}' is now removed. You should re-save this dataset in its new format.
75-
"""
76-
77-
78-
class _LoadSparseSeries:
79-
# To load a SparseSeries as a Series[Sparse]
80-
81-
# https://github.com/python/mypy/issues/1020
82-
# error: Incompatible return type for "__new__" (returns "Series", but must return
83-
# a subtype of "_LoadSparseSeries")
84-
def __new__(cls) -> Series: # type: ignore[misc]
85-
from pandas import Series
86-
87-
warnings.warn(
88-
_sparse_msg.format(cls="SparseSeries", new="Series"),
89-
FutureWarning,
90-
stacklevel=6,
91-
)
92-
93-
return Series(dtype=object)
94-
95-
96-
class _LoadSparseFrame:
97-
# To load a SparseDataFrame as a DataFrame[Sparse]
98-
99-
# https://github.com/python/mypy/issues/1020
100-
# error: Incompatible return type for "__new__" (returns "DataFrame", but must
101-
# return a subtype of "_LoadSparseFrame")
102-
def __new__(cls) -> DataFrame: # type: ignore[misc]
103-
from pandas import DataFrame
104-
105-
warnings.warn(
106-
_sparse_msg.format(cls="SparseDataFrame", new="DataFrame"),
107-
FutureWarning,
108-
stacklevel=6,
109-
)
110-
111-
return DataFrame()
112-
113-
11461
# If classes are moved, provide compat here.
11562
_class_locations_map = {
11663
("pandas.core.sparse.array", "SparseArray"): ("pandas.core.arrays", "SparseArray"),
@@ -144,14 +91,6 @@ def __new__(cls) -> DataFrame: # type: ignore[misc]
14491
"pandas.core.arrays.sparse",
14592
"SparseArray",
14693
),
147-
("pandas.sparse.series", "SparseSeries"): (
148-
"pandas.compat.pickle_compat",
149-
"_LoadSparseSeries",
150-
),
151-
("pandas.sparse.frame", "SparseDataFrame"): (
152-
"pandas.core.sparse.frame",
153-
"_LoadSparseFrame",
154-
),
15594
("pandas.indexes.base", "_new_Index"): ("pandas.core.indexes.base", "_new_Index"),
15695
("pandas.indexes.base", "Index"): ("pandas.core.indexes.base", "Index"),
15796
("pandas.indexes.numeric", "Int64Index"): (
@@ -183,14 +122,6 @@ def __new__(cls) -> DataFrame: # type: ignore[misc]
183122
"pandas.core.indexes.numeric",
184123
"Float64Index",
185124
),
186-
("pandas.core.sparse.series", "SparseSeries"): (
187-
"pandas.compat.pickle_compat",
188-
"_LoadSparseSeries",
189-
),
190-
("pandas.core.sparse.frame", "SparseDataFrame"): (
191-
"pandas.compat.pickle_compat",
192-
"_LoadSparseFrame",
193-
),
194125
}
195126

196127

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

pandas/tests/io/test_pickle.py

-22
Original file line numberDiff line numberDiff line change
@@ -254,28 +254,6 @@ def test_pickle_path_localpath():
254254
tm.assert_frame_equal(df, result)
255255

256256

257-
@pytest.mark.parametrize("typ", ["sparseseries", "sparseframe"])
258-
def test_legacy_sparse_warning(datapath, typ):
259-
"""
260-
261-
Generated with
262-
263-
>>> df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [0, 0, 1, 1]}).to_sparse()
264-
>>> df.to_pickle("pandas/tests/io/data/pickle/sparseframe-0.20.3.pickle.gz",
265-
... compression="gzip")
266-
267-
>>> s = df['B']
268-
>>> s.to_pickle("pandas/tests/io/data/pickle/sparseseries-0.20.3.pickle.gz",
269-
... compression="gzip")
270-
"""
271-
with tm.assert_produces_warning(FutureWarning):
272-
simplefilter("ignore", DeprecationWarning) # from boto
273-
pd.read_pickle(
274-
datapath("io", "data", "pickle", f"{typ}-0.20.3.pickle.gz"),
275-
compression="gzip",
276-
)
277-
278-
279257
# ---------------------
280258
# test pickle compression
281259
# ---------------------

0 commit comments

Comments
 (0)