Skip to content

Commit 2f2289c

Browse files
bashtageKevin Sheppard
and
Kevin Sheppard
authored
ENH: Synchronize pickle with upstream (#206)
* ENH: Synchronize pickle with upstream Align APIs Add tests * BUG: Correct ReadPickleBuffer Correct definition Use ensure_clean * MAINT: Clean up to_pickle testing Ensure to_pickle is tested since it appears in pandas/io/api * BUG: Restore read_pickle * CLN: Remove to_pickle from frame Remove extra def Test on series * MAINT: Remove extra def Co-authored-by: Kevin Sheppard <kevin.sheppard@gmail.com>
1 parent ae902f6 commit 2f2289c

File tree

5 files changed

+84
-14
lines changed

5 files changed

+84
-14
lines changed

pandas-stubs/_typing.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class BaseBuffer(Protocol): ...
6767
class ReadBuffer(BaseBuffer, Protocol[AnyStr_cov]): ...
6868
class WriteBuffer(BaseBuffer, Protocol[AnyStr_cov]): ...
6969

70+
class ReadPickleBuffer(ReadBuffer[bytes], Protocol):
71+
def readline(self, size: int | None = ...) -> bytes: ...
72+
7073
class ReadCsvBuffer(ReadBuffer[AnyStr_cov], Protocol[AnyStr_cov]):
7174
def __iter__(self) -> Iterator[AnyStr_cov]: ...
7275
def readline(self) -> AnyStr_cov: ...

pandas-stubs/core/frame.pyi

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,12 +1935,6 @@ class DataFrame(NDFrame, OpsMixin):
19351935
index: _bool = ...,
19361936
indent: int | None = ...,
19371937
) -> _str: ...
1938-
def to_pickle(
1939-
self,
1940-
path: _str,
1941-
compression: CompressionOptions = ...,
1942-
protocol: int = ...,
1943-
) -> None: ...
19441938
@overload
19451939
def to_string(
19461940
self,

pandas-stubs/core/generic.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from pandas._typing import (
4242
SortKind,
4343
StorageOptions,
4444
T,
45+
WriteBuffer,
4546
)
4647

4748
from pandas.io.pytables import HDFStore
@@ -171,9 +172,10 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
171172
) -> int | None: ...
172173
def to_pickle(
173174
self,
174-
path: _str,
175+
path: FilePath | WriteBuffer[bytes],
175176
compression: CompressionOptions = ...,
176177
protocol: int = ...,
178+
storage_options: StorageOptions = ...,
177179
) -> None: ...
178180
def to_clipboard(
179181
self, excel: _bool = ..., sep: _str | None = ..., **kwargs

pandas-stubs/io/pickle.pyi

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
from typing import Any
2+
13
from pandas._typing import (
24
CompressionOptions,
3-
FilePathOrBuffer,
5+
FilePath,
6+
ReadPickleBuffer,
7+
StorageOptions,
8+
WriteBuffer,
49
)
510

611
def to_pickle(
7-
obj,
8-
filepath_or_buffer: FilePathOrBuffer,
9-
compression: str | None = ...,
12+
obj: object,
13+
filepath_or_buffer: FilePath | WriteBuffer[bytes],
14+
compression: CompressionOptions = ...,
1015
protocol: int = ...,
11-
): ...
16+
storage_options: StorageOptions = ...,
17+
) -> None: ...
1218
def read_pickle(
13-
filepath_or_buffer_or_reader: FilePathOrBuffer,
19+
filepath_or_buffer: FilePath | ReadPickleBuffer,
1420
compression: CompressionOptions = ...,
15-
): ...
21+
storage_options: StorageOptions = ...,
22+
) -> Any: ...

tests/test_io.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sqlite3
77
from typing import (
88
TYPE_CHECKING,
9+
Any,
910
Dict,
1011
Generator,
1112
List,
@@ -30,6 +31,7 @@
3031
read_json,
3132
read_orc,
3233
read_parquet,
34+
read_pickle,
3335
read_sas,
3436
read_spss,
3537
read_sql,
@@ -45,6 +47,7 @@
4547

4648
from tests import check
4749

50+
from pandas.io.api import to_pickle
4851
from pandas.io.clipboard import PyperclipException
4952
from pandas.io.common import IOHandles
5053
from pandas.io.json._json import JsonReader
@@ -117,6 +120,67 @@ def test_xml_str():
117120
check(assert_type(read_xml(io.StringIO(out)), DataFrame), DataFrame)
118121

119122

123+
def test_pickle():
124+
with ensure_clean() as path:
125+
check(assert_type(DF.to_pickle(path), None), type(None))
126+
check(assert_type(read_pickle(path), Any), DataFrame)
127+
128+
with ensure_clean() as path:
129+
check(assert_type(to_pickle(DF, path), None), type(None))
130+
check(assert_type(read_pickle(path), Any), DataFrame)
131+
132+
133+
def test_pickle_file_handle():
134+
with ensure_clean() as path:
135+
check(assert_type(DF.to_pickle(path), None), type(None))
136+
file = open(path, "rb")
137+
check(assert_type(read_pickle(file), Any), DataFrame)
138+
file.close()
139+
140+
141+
def test_pickle_path():
142+
with ensure_clean() as path:
143+
check(assert_type(DF.to_pickle(path), None), type(None))
144+
check(assert_type(read_pickle(Path(path)), Any), DataFrame)
145+
146+
147+
def test_pickle_protocol():
148+
with ensure_clean() as path:
149+
DF.to_pickle(path, protocol=3)
150+
check(assert_type(read_pickle(path), Any), DataFrame)
151+
152+
153+
def test_pickle_compression():
154+
with ensure_clean() as path:
155+
DF.to_pickle(path, compression="gzip")
156+
check(
157+
assert_type(read_pickle(path, compression="gzip"), Any),
158+
DataFrame,
159+
)
160+
161+
check(
162+
assert_type(read_pickle(path, compression="gzip"), Any),
163+
DataFrame,
164+
)
165+
166+
167+
def test_pickle_storage_options():
168+
with ensure_clean() as path:
169+
DF.to_pickle(path, storage_options={})
170+
171+
check(
172+
assert_type(read_pickle(path, storage_options={}), Any),
173+
DataFrame,
174+
)
175+
176+
177+
def test_to_pickle_series():
178+
s: Series = DF["a"]
179+
with ensure_clean() as path:
180+
check(assert_type(s.to_pickle(path), None), type(None))
181+
check(assert_type(read_pickle(path), Any), Series)
182+
183+
120184
def test_read_stata_df():
121185
with ensure_clean() as path:
122186
DF.to_stata(path)

0 commit comments

Comments
 (0)