Skip to content

ENH Remove import time warning for missing lzma #43495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 28, 2021
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
9 changes: 2 additions & 7 deletions pandas/_testing/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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}")

Expand Down
27 changes: 4 additions & 23 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""
import platform
import sys
import warnings

from pandas._typing import F
from pandas.compat.numpy import (
Expand Down Expand Up @@ -104,27 +103,7 @@ def is_platform_arm() -> bool:
)


def import_lzma():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is considered to be public API and would need a deprecation cycle?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no its not

"""
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.

Expand All @@ -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, "
Expand Down
9 changes: 2 additions & 7 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

from pandas.compat import (
get_lzma_file,
import_lzma,
is_platform_little_endian,
)
import pandas.util._test_decorators as td
Expand All @@ -51,9 +50,6 @@
MonthEnd,
)

lzma = import_lzma()


pytestmark = pytest.mark.filterwarnings(
"ignore:Timestamp.freq is deprecated:FutureWarning"
)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import datetime
import gzip
import io
import lzma
import os
import struct
import warnings
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/io/xml/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down