diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 50156d4565bbd..b4502912b72e5 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -667,6 +667,7 @@ I/O - Bug in :func:`read_csv` used second row to guess implicit index if ``header`` was set to ``None`` for ``engine="python"`` (:issue:`22144`) - Bug in :func:`read_csv` not recognizing bad lines when ``names`` were given for ``engine="c"`` (:issue:`22144`) - Bug in :func:`read_csv` with :code:`float_precision="round_trip"` which did not skip initial/trailing whitespace (:issue:`43713`) +- 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`) - Bug in :func:`read_csv` not applying dtype for ``index_col`` (:issue:`9435`) - Bug in dumping/loading a :class:`DataFrame` with ``yaml.dump(frame)`` (:issue:`42748`) - Bug in :func:`read_csv` raising ``ValueError`` when names was longer than header but equal to data rows for ``engine="python"`` (:issue:`38453`) diff --git a/pandas/_testing/_io.py b/pandas/_testing/_io.py index 437e75be0e55b..2c8e1b0daaeaa 100644 --- a/pandas/_testing/_io.py +++ b/pandas/_testing/_io.py @@ -14,10 +14,7 @@ FilePath, ReadPickleBuffer, ) -from pandas.compat import ( - get_lzma_file, - import_lzma, -) +from pandas.compat import get_lzma_file import pandas as pd from pandas._testing._random import rands @@ -33,8 +30,6 @@ _RAISE_NETWORK_ERROR_DEFAULT = False -lzma = import_lzma() - # skip tests on exceptions with these messages _network_error_messages = ( # 'urlopen error timed out', @@ -397,7 +392,7 @@ def write_to_compressed(compression, path, data, dest="test"): elif compression == "bz2": compress_method = bz2.BZ2File elif compression == "xz": - compress_method = get_lzma_file(lzma) + compress_method = get_lzma_file() else: raise ValueError(f"Unrecognized compression type: {compression}") diff --git a/pandas/compat/__init__.py b/pandas/compat/__init__.py index 57b13fef9ad8a..de6b4c9fc6e4a 100644 --- a/pandas/compat/__init__.py +++ b/pandas/compat/__init__.py @@ -9,7 +9,6 @@ """ import platform import sys -import warnings from pandas._typing import F from pandas.compat.numpy import ( @@ -104,27 +103,7 @@ def is_platform_arm() -> bool: ) -def import_lzma(): - """ - Importing the `lzma` module. - - Warns - ----- - When the `lzma` module is not available. - """ - try: - import lzma - - return lzma - except ImportError: - msg = ( - "Could not import the lzma module. Your installed Python is incomplete. " - "Attempting to use lzma compression will result in a RuntimeError." - ) - warnings.warn(msg) - - -def get_lzma_file(lzma): +def get_lzma_file(): """ Importing the `LZMAFile` class from the `lzma` module. @@ -138,7 +117,9 @@ def get_lzma_file(lzma): RuntimeError If the `lzma` module was not imported correctly, or didn't exist. """ - if lzma is None: + try: + import lzma + except ImportError: raise RuntimeError( "lzma module not available. " "A Python re-install with the proper dependencies, " diff --git a/pandas/io/common.py b/pandas/io/common.py index 2102e67f06d36..844304396a23f 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -49,17 +49,12 @@ StorageOptions, WriteBuffer, ) -from pandas.compat import ( - get_lzma_file, - import_lzma, -) +from pandas.compat import get_lzma_file from pandas.compat._optional import import_optional_dependency from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import is_file_like -lzma = import_lzma() - _VALID_URLS = set(uses_relative + uses_netloc + uses_params) _VALID_URLS.discard("") @@ -754,7 +749,7 @@ def get_handle( # XZ Compression elif compression == "xz": - handle = get_lzma_file(lzma)(handle, ioargs.mode) + handle = get_lzma_file()(handle, ioargs.mode) # Unrecognized Compression else: diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py index d656c56b0ee10..aa80df1bcbd38 100644 --- a/pandas/tests/io/test_pickle.py +++ b/pandas/tests/io/test_pickle.py @@ -33,7 +33,6 @@ from pandas.compat import ( get_lzma_file, - import_lzma, is_platform_little_endian, ) import pandas.util._test_decorators as td @@ -51,9 +50,6 @@ MonthEnd, ) -lzma = import_lzma() - - pytestmark = pytest.mark.filterwarnings( "ignore:Timestamp.freq is deprecated:FutureWarning" ) @@ -311,7 +307,7 @@ def compress_file(self, src_path, dest_path, compression): with zipfile.ZipFile(dest_path, "w", compression=zipfile.ZIP_DEFLATED) as f: f.write(src_path, os.path.basename(src_path)) elif compression == "xz": - f = get_lzma_file(lzma)(dest_path, "w") + f = get_lzma_file()(dest_path, "w") else: msg = f"Unrecognized compression type: {compression}" raise ValueError(msg) diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index fa2305d11f901..eb457d74c6a01 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -3,7 +3,6 @@ from datetime import datetime import gzip import io -import lzma import os import struct import warnings @@ -1904,6 +1903,7 @@ def test_compression(compression, version, use_dict, infer): with bz2.open(path, "rb") as comp: fp = io.BytesIO(comp.read()) elif compression == "xz": + lzma = pytest.importorskip("lzma") with lzma.open(path, "rb") as comp: fp = io.BytesIO(comp.read()) elif compression is None: diff --git a/pandas/tests/io/xml/test_xml.py b/pandas/tests/io/xml/test_xml.py index a99f66336bf22..70a75bd34be71 100644 --- a/pandas/tests/io/xml/test_xml.py +++ b/pandas/tests/io/xml/test_xml.py @@ -1044,12 +1044,14 @@ def test_wrong_compression_gz(parser, comp): @pytest.mark.parametrize("comp", ["bz2", "gzip", "zip"]) def test_wrong_compression_xz(parser, comp): - from lzma import LZMAError + lzma = pytest.importorskip("lzma") with tm.ensure_clean() as path: geom_df.to_xml(path, parser=parser, compression=comp) - with pytest.raises(LZMAError, match="Input format not supported by decoder"): + with pytest.raises( + lzma.LZMAError, match="Input format not supported by decoder" + ): read_xml(path, parser=parser, compression="xz")