Skip to content

Commit 5ff0eb0

Browse files
committed
CLN: Create get_bz2_file to match get_lzma_file
1 parent 2bdf91e commit 5ff0eb0

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

pandas/_testing/_io.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import bz2
43
from functools import wraps
54
import gzip
65
import io
@@ -13,7 +12,10 @@
1312
)
1413
import zipfile
1514

16-
from pandas.compat import get_lzma_file
15+
from pandas.compat import (
16+
get_bz2_file,
17+
get_lzma_file,
18+
)
1719
from pandas.compat._optional import import_optional_dependency
1820

1921
import pandas as pd
@@ -410,7 +412,7 @@ def write_to_compressed(compression, path, data, dest: str = "test"):
410412
elif compression == "gzip":
411413
compress_method = gzip.GzipFile
412414
elif compression == "bz2":
413-
compress_method = bz2.BZ2File
415+
compress_method = get_bz2_file()
414416
elif compression == "zstd":
415417
compress_method = import_optional_dependency("zstandard").open
416418
elif compression == "xz":

pandas/compat/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ def get_lzma_file() -> type[pandas.compat.compressors.LZMAFile]:
154154
return pandas.compat.compressors.LZMAFile
155155

156156

157+
def get_bz2_file() -> type[pandas.compat.compressors.BZ2File]:
158+
"""
159+
Importing the `BZ2File` class from the `bz2` module.
160+
161+
Returns
162+
-------
163+
class
164+
The `BZ2File` class from the `bz2` module.
165+
166+
Raises
167+
------
168+
RuntimeError
169+
If the `bz2` module was not imported correctly, or didn't exist.
170+
"""
171+
if not pandas.compat.compressors.has_bz2:
172+
raise RuntimeError(
173+
"bz2 module not available. "
174+
"A Python re-install with the proper dependencies, "
175+
"might be required to solve this issue."
176+
)
177+
return pandas.compat.compressors.BZ2File
178+
179+
157180
__all__ = [
158181
"is_numpy_dev",
159182
"pa_version_under7p0",

pandas/io/common.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@
5757
StorageOptions,
5858
WriteBuffer,
5959
)
60-
from pandas.compat import get_lzma_file
60+
from pandas.compat import (
61+
get_bz2_file,
62+
get_lzma_file,
63+
)
6164
from pandas.compat._optional import import_optional_dependency
6265
from pandas.util._decorators import doc
6366
from pandas.util._exceptions import find_stack_level
@@ -72,11 +75,6 @@
7275
from pandas.core.indexes.api import MultiIndex
7376
from pandas.core.shared_docs import _shared_docs
7477

75-
try:
76-
from pandas.compat.compressors import BZ2File as _BZ2File
77-
except ImportError:
78-
_BZ2File = None
79-
8078
_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
8179
_VALID_URLS.discard("")
8280
_RFC_3986_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9+\-+.]*://")
@@ -770,11 +768,7 @@ def get_handle(
770768
elif compression == "bz2":
771769
# Overload of "BZ2File" to handle pickle protocol 5
772770
# "Union[str, BaseBuffer]", "str", "Dict[str, Any]"
773-
if _BZ2File is None:
774-
raise ImportError(
775-
"bz2 compression requires the bz2 module to be installed"
776-
)
777-
handle = _BZ2File( # type: ignore[call-overload]
771+
handle = get_bz2_file()( # type: ignore[call-overload]
778772
handle,
779773
mode=ioargs.mode,
780774
**compression_args,

0 commit comments

Comments
 (0)