Skip to content

Commit e4503dd

Browse files
committed
Review
1 parent b6b5628 commit e4503dd

File tree

6 files changed

+51
-69
lines changed

6 files changed

+51
-69
lines changed

pandas/core/describe.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
from pandas.core.reshape.concat import concat
3737

38+
from pandas.io.formats.format import format_percentiles
39+
3840
if TYPE_CHECKING:
3941
from pandas import (
4042
DataFrame,
@@ -228,8 +230,6 @@ def describe_numeric_1d(series: Series, percentiles: Sequence[float]) -> Series:
228230
"""
229231
from pandas import Series
230232

231-
from pandas.io.formats.format import format_percentiles
232-
233233
# error: Argument 1 to "format_percentiles" has incompatible type "Sequence[float]";
234234
# expected "Union[ndarray, List[Union[int, float]], List[float], List[Union[str,
235235
# float]]]"
@@ -337,8 +337,6 @@ def describe_timestamp_1d(data: Series, percentiles: Sequence[float]) -> Series:
337337
# GH-30164
338338
from pandas import Series
339339

340-
from pandas.io.formats.format import format_percentiles
341-
342340
# error: Argument 1 to "format_percentiles" has incompatible type "Sequence[float]";
343341
# expected "Union[ndarray, List[Union[int, float]], List[float], List[Union[str,
344342
# float]]]"

pandas/io/common.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
from pandas.core.dtypes.common import is_file_like
5656

57-
from pandas.core import generic
57+
from pandas.core.shared_docs import _shared_docs
5858

5959
_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
6060
_VALID_URLS.discard("")
@@ -248,10 +248,7 @@ def is_fsspec_url(url: FilePath | BaseBuffer) -> bool:
248248
)
249249

250250

251-
@doc(
252-
compression_options=generic._shared_docs["compression_options"]
253-
% "filepath_or_buffer"
254-
)
251+
@doc(compression_options=_shared_docs["compression_options"] % "filepath_or_buffer")
255252
def _get_filepath_or_buffer(
256253
filepath_or_buffer: FilePath | BaseBuffer,
257254
encoding: str = "utf-8",
@@ -497,10 +494,7 @@ def get_compression_method(
497494
return compression_method, compression_args
498495

499496

500-
@doc(
501-
compression_options=generic._shared_docs["compression_options"]
502-
% "filepath_or_buffer"
503-
)
497+
@doc(compression_options=_shared_docs["compression_options"] % "filepath_or_buffer")
504498
def infer_compression(
505499
filepath_or_buffer: FilePath | BaseBuffer, compression: str | None
506500
) -> str | None:
@@ -604,7 +598,7 @@ def get_handle(
604598
...
605599

606600

607-
@doc(compression_options=generic._shared_docs["compression_options"] % "path_or_buf")
601+
@doc(compression_options=_shared_docs["compression_options"] % "path_or_buf")
608602
def get_handle(
609603
path_or_buf: FilePath | BaseBuffer,
610604
mode: str,
@@ -1136,18 +1130,20 @@ def _is_binary_mode(handle: FilePath | BaseBuffer, mode: str) -> bool:
11361130
)
11371131

11381132

1139-
def _get_binary_io_classes() -> tuple[type, ...]:
1133+
def _get_binary_io_classes(_cache: list[type] = []) -> tuple[type, ...]:
11401134
"""IO classes that that expect bytes"""
1141-
binary_classes: tuple[type, ...] = (BufferedIOBase, RawIOBase)
1142-
1143-
# python-zstandard doesn't use any of the builtin base classes; instead we
1144-
# have to use the `zstd.ZstdDecompressionReader` class for isinstance checks.
1145-
# Unfortunately `zstd.ZstdDecompressionReader` isn't exposed by python-zstandard
1146-
# so we have to get it from a `zstd.ZstdDecompressor` instance.
1147-
# See also https://github.com/indygreg/python-zstandard/pull/165.
1148-
zstd = import_optional_dependency("zstandard", errors="ignore")
1149-
if zstd is not None:
1150-
with zstd.ZstdDecompressor().stream_reader(b"") as reader:
1151-
binary_classes += (type(reader),)
1152-
1153-
return binary_classes
1135+
if not _cache:
1136+
binary_classes: tuple[type, ...] = (BufferedIOBase, RawIOBase)
1137+
1138+
# python-zstandard doesn't use any of the builtin base classes; instead we
1139+
# have to use the `zstd.ZstdDecompressionReader` class for isinstance checks.
1140+
# Unfortunately `zstd.ZstdDecompressionReader` isn't exposed by python-zstandard
1141+
# so we have to get it from a `zstd.ZstdDecompressor` instance.
1142+
# See also https://github.com/indygreg/python-zstandard/pull/165.
1143+
zstd = import_optional_dependency("zstandard", errors="ignore")
1144+
if zstd is not None:
1145+
with zstd.ZstdDecompressor().stream_reader(b"") as reader:
1146+
binary_classes += (type(reader),)
1147+
1148+
_cache[:] = binary_classes
1149+
return tuple(_cache)

pandas/io/formats/xml.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from pandas.core.dtypes.common import is_list_like
2121

22-
from pandas.core import generic
22+
from pandas.core import _shared_docs
2323
from pandas.core.frame import DataFrame
2424

2525
from pandas.io.common import get_handle
@@ -29,7 +29,7 @@
2929
)
3030

3131

32-
@doc(compression_options=generic._shared_docs["compression_options"] % "path_or_buffer")
32+
@doc(compression_options=_shared_docs["compression_options"] % "path_or_buffer")
3333
class BaseXMLFormatter:
3434
"""
3535
Subclass for formatting data in XML.

pandas/tests/io/conftest.py

+27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
is_platform_mac,
1111
is_platform_windows,
1212
)
13+
import pandas.util._test_decorators as td
1314

1415
import pandas._testing as tm
1516

@@ -184,3 +185,29 @@ def add_tips_files(bucket_name):
184185
while cli.list_buckets()["Buckets"] and timeout > 0:
185186
time.sleep(0.1)
186187
timeout -= 0.1
188+
189+
190+
_compression_formats_params = [
191+
(".no_compress", None),
192+
("", None),
193+
(".gz", "gzip"),
194+
(".GZ", "gzip"),
195+
(".bz2", "bz2"),
196+
(".BZ2", "bz2"),
197+
(".zip", "zip"),
198+
(".ZIP", "zip"),
199+
(".xz", "xz"),
200+
(".XZ", "xz"),
201+
pytest.param((".zst", "zstd"), marks=td.skip_if_no("zstandard")),
202+
pytest.param((".ZST", "zstd"), marks=td.skip_if_no("zstandard")),
203+
]
204+
205+
206+
@pytest.fixture(params=_compression_formats_params[1:])
207+
def compression_format(request):
208+
return request.param
209+
210+
211+
@pytest.fixture(params=_compression_formats_params)
212+
def compression_ext(request):
213+
return request.param[0]

pandas/tests/io/test_common.py

-19
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,6 @@ def __fspath__(self):
4848
HERE = os.path.abspath(os.path.dirname(__file__))
4949

5050

51-
@pytest.fixture(
52-
params=[
53-
("", None),
54-
(".gz", "gzip"),
55-
(".GZ", "gzip"),
56-
(".bz2", "bz2"),
57-
(".BZ2", "bz2"),
58-
(".zip", "zip"),
59-
(".ZIP", "zip"),
60-
(".xz", "xz"),
61-
(".XZ", "xz"),
62-
pytest.param((".zst", "zstd"), marks=td.skip_if_no("zstandard")),
63-
pytest.param((".ZST", "zstd"), marks=td.skip_if_no("zstandard")),
64-
]
65-
)
66-
def compression_format(request):
67-
return request.param
68-
69-
7051
# https://github.com/cython/cython/issues/1720
7152
@pytest.mark.filterwarnings("ignore:can't resolve package:ImportWarning")
7253
class TestCommonIOCapabilities:

pandas/tests/io/test_pickle.py

-20
Original file line numberDiff line numberDiff line change
@@ -286,26 +286,6 @@ def get_random_path():
286286
return f"__{tm.rands(10)}__.pickle"
287287

288288

289-
@pytest.fixture(
290-
params=[
291-
"",
292-
".no_compress",
293-
".gz",
294-
".GZ",
295-
".bz2",
296-
".BZ2",
297-
".zip",
298-
".ZIP",
299-
".xz",
300-
".XZ",
301-
pytest.param(".zst", marks=td.skip_if_no("zstandard")),
302-
pytest.param(".ZST", marks=td.skip_if_no("zstandard")),
303-
]
304-
)
305-
def compression_ext(request):
306-
return request.param
307-
308-
309289
class TestCompression:
310290

311291
_extension_to_compression = {

0 commit comments

Comments
 (0)