Skip to content

Commit 36df78e

Browse files
authored
ENH Remove import time warning for missing lzma (#43495)
1 parent 3d99081 commit 36df78e

File tree

7 files changed

+15
-45
lines changed

7 files changed

+15
-45
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ I/O
702702
- Bug in :func:`read_csv` used second row to guess implicit index if ``header`` was set to ``None`` for ``engine="python"`` (:issue:`22144`)
703703
- Bug in :func:`read_csv` not recognizing bad lines when ``names`` were given for ``engine="c"`` (:issue:`22144`)
704704
- Bug in :func:`read_csv` with :code:`float_precision="round_trip"` which did not skip initial/trailing whitespace (:issue:`43713`)
705+
- Bug when Python is built without lzma module: a warning was raised at the pandas import time, even if the lzma capability isn't used. (:issue:`43495`)
705706
- Bug in :func:`read_csv` not applying dtype for ``index_col`` (:issue:`9435`)
706707
- Bug in dumping/loading a :class:`DataFrame` with ``yaml.dump(frame)`` (:issue:`42748`)
707708
- Bug in :func:`read_csv` raising ``ValueError`` when names was longer than header but equal to data rows for ``engine="python"`` (:issue:`38453`)

pandas/_testing/_io.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
FilePath,
1515
ReadPickleBuffer,
1616
)
17-
from pandas.compat import (
18-
get_lzma_file,
19-
import_lzma,
20-
)
17+
from pandas.compat import get_lzma_file
2118

2219
import pandas as pd
2320
from pandas._testing._random import rands
@@ -33,8 +30,6 @@
3330

3431
_RAISE_NETWORK_ERROR_DEFAULT = False
3532

36-
lzma = import_lzma()
37-
3833
# skip tests on exceptions with these messages
3934
_network_error_messages = (
4035
# 'urlopen error timed out',
@@ -397,7 +392,7 @@ def write_to_compressed(compression, path, data, dest="test"):
397392
elif compression == "bz2":
398393
compress_method = bz2.BZ2File
399394
elif compression == "xz":
400-
compress_method = get_lzma_file(lzma)
395+
compress_method = get_lzma_file()
401396
else:
402397
raise ValueError(f"Unrecognized compression type: {compression}")
403398

pandas/compat/__init__.py

+4-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"""
1010
import platform
1111
import sys
12-
import warnings
1312

1413
from pandas._typing import F
1514
from pandas.compat.numpy import (
@@ -104,27 +103,7 @@ def is_platform_arm() -> bool:
104103
)
105104

106105

107-
def import_lzma():
108-
"""
109-
Importing the `lzma` module.
110-
111-
Warns
112-
-----
113-
When the `lzma` module is not available.
114-
"""
115-
try:
116-
import lzma
117-
118-
return lzma
119-
except ImportError:
120-
msg = (
121-
"Could not import the lzma module. Your installed Python is incomplete. "
122-
"Attempting to use lzma compression will result in a RuntimeError."
123-
)
124-
warnings.warn(msg)
125-
126-
127-
def get_lzma_file(lzma):
106+
def get_lzma_file():
128107
"""
129108
Importing the `LZMAFile` class from the `lzma` module.
130109
@@ -138,7 +117,9 @@ def get_lzma_file(lzma):
138117
RuntimeError
139118
If the `lzma` module was not imported correctly, or didn't exist.
140119
"""
141-
if lzma is None:
120+
try:
121+
import lzma
122+
except ImportError:
142123
raise RuntimeError(
143124
"lzma module not available. "
144125
"A Python re-install with the proper dependencies, "

pandas/io/common.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,12 @@
4949
StorageOptions,
5050
WriteBuffer,
5151
)
52-
from pandas.compat import (
53-
get_lzma_file,
54-
import_lzma,
55-
)
52+
from pandas.compat import get_lzma_file
5653
from pandas.compat._optional import import_optional_dependency
5754
from pandas.util._exceptions import find_stack_level
5855

5956
from pandas.core.dtypes.common import is_file_like
6057

61-
lzma = import_lzma()
62-
6358
_VALID_URLS = set(uses_relative + uses_netloc + uses_params)
6459
_VALID_URLS.discard("")
6560

@@ -754,7 +749,7 @@ def get_handle(
754749

755750
# XZ Compression
756751
elif compression == "xz":
757-
handle = get_lzma_file(lzma)(handle, ioargs.mode)
752+
handle = get_lzma_file()(handle, ioargs.mode)
758753

759754
# Unrecognized Compression
760755
else:

pandas/tests/io/test_pickle.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
from pandas.compat import (
3535
get_lzma_file,
36-
import_lzma,
3736
is_platform_little_endian,
3837
)
3938
import pandas.util._test_decorators as td
@@ -51,9 +50,6 @@
5150
MonthEnd,
5251
)
5352

54-
lzma = import_lzma()
55-
56-
5753
pytestmark = pytest.mark.filterwarnings(
5854
"ignore:Timestamp.freq is deprecated:FutureWarning"
5955
)
@@ -311,7 +307,7 @@ def compress_file(self, src_path, dest_path, compression):
311307
with zipfile.ZipFile(dest_path, "w", compression=zipfile.ZIP_DEFLATED) as f:
312308
f.write(src_path, os.path.basename(src_path))
313309
elif compression == "xz":
314-
f = get_lzma_file(lzma)(dest_path, "w")
310+
f = get_lzma_file()(dest_path, "w")
315311
else:
316312
msg = f"Unrecognized compression type: {compression}"
317313
raise ValueError(msg)

pandas/tests/io/test_stata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from datetime import datetime
44
import gzip
55
import io
6-
import lzma
76
import os
87
import struct
98
import warnings
@@ -1904,6 +1903,7 @@ def test_compression(compression, version, use_dict, infer):
19041903
with bz2.open(path, "rb") as comp:
19051904
fp = io.BytesIO(comp.read())
19061905
elif compression == "xz":
1906+
lzma = pytest.importorskip("lzma")
19071907
with lzma.open(path, "rb") as comp:
19081908
fp = io.BytesIO(comp.read())
19091909
elif compression is None:

pandas/tests/io/xml/test_xml.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1044,12 +1044,14 @@ def test_wrong_compression_gz(parser, comp):
10441044

10451045
@pytest.mark.parametrize("comp", ["bz2", "gzip", "zip"])
10461046
def test_wrong_compression_xz(parser, comp):
1047-
from lzma import LZMAError
1047+
lzma = pytest.importorskip("lzma")
10481048

10491049
with tm.ensure_clean() as path:
10501050
geom_df.to_xml(path, parser=parser, compression=comp)
10511051

1052-
with pytest.raises(LZMAError, match="Input format not supported by decoder"):
1052+
with pytest.raises(
1053+
lzma.LZMAError, match="Input format not supported by decoder"
1054+
):
10531055
read_xml(path, parser=parser, compression="xz")
10541056

10551057

0 commit comments

Comments
 (0)