From 7323b50a1bb570958e8127b59ca488c97c876631 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Mon, 1 Feb 2021 05:45:47 -0800 Subject: [PATCH] Backport PR #39355: BUG: read_excel failing to check older xlrd versions properly --- doc/source/whatsnew/v1.2.2.rst | 2 ++ pandas/compat/_optional.py | 4 ++-- pandas/io/excel/_base.py | 4 ++-- pandas/tests/io/excel/__init__.py | 4 ++-- pandas/util/_print_versions.py | 4 ++-- scripts/validate_unwanted_patterns.py | 1 - 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/doc/source/whatsnew/v1.2.2.rst b/doc/source/whatsnew/v1.2.2.rst index c5825d0881515..51535501f5c1b 100644 --- a/doc/source/whatsnew/v1.2.2.rst +++ b/doc/source/whatsnew/v1.2.2.rst @@ -14,6 +14,8 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ + +- Fixed regression in :func:`read_excel` that caused it to raise ``AttributeError`` when checking version of older xlrd versions (:issue:`38955`) - Fixed regression in :class:`DataFrame` constructor reordering element when construction from datetime ndarray with dtype not ``"datetime64[ns]"`` (:issue:`39422`) - Fixed regression in :meth:`~DataFrame.to_pickle` failing to create bz2/xz compressed pickle files with ``protocol=5`` (:issue:`39002`) - Fixed regression in :func:`pandas.testing.assert_series_equal` and :func:`pandas.testing.assert_frame_equal` always raising ``AssertionError`` when comparing extension dtypes (:issue:`39410`) diff --git a/pandas/compat/_optional.py b/pandas/compat/_optional.py index 533e67acfa2f4..4ed9df2c97fdb 100644 --- a/pandas/compat/_optional.py +++ b/pandas/compat/_optional.py @@ -46,7 +46,7 @@ } -def _get_version(module: types.ModuleType) -> str: +def get_version(module: types.ModuleType) -> str: version = getattr(module, "__version__", None) if version is None: # xlrd uses a capitalized attribute name @@ -112,7 +112,7 @@ def import_optional_dependency( minimum_version = VERSIONS.get(name) if minimum_version: - version = _get_version(module) + version = get_version(module) if distutils.version.LooseVersion(version) < minimum_version: assert on_version in {"warn", "raise", "ignore"} msg = ( diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 5be8dbf152309..b4d7ee418ea1e 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -13,7 +13,7 @@ from pandas._libs.parsers import STR_NA_VALUES from pandas._typing import Buffer, FilePathOrBuffer, StorageOptions -from pandas.compat._optional import import_optional_dependency +from pandas.compat._optional import get_version, import_optional_dependency from pandas.errors import EmptyDataError from pandas.util._decorators import Appender, deprecate_nonkeyword_arguments, doc @@ -1049,7 +1049,7 @@ def __init__( else: import xlrd - xlrd_version = LooseVersion(xlrd.__version__) + xlrd_version = LooseVersion(get_version(xlrd)) if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book): ext = "xls" diff --git a/pandas/tests/io/excel/__init__.py b/pandas/tests/io/excel/__init__.py index b7ceb28573484..9dda54915ab1c 100644 --- a/pandas/tests/io/excel/__init__.py +++ b/pandas/tests/io/excel/__init__.py @@ -2,7 +2,7 @@ import pytest -from pandas.compat._optional import import_optional_dependency +from pandas.compat._optional import get_version, import_optional_dependency pytestmark = [ pytest.mark.filterwarnings( @@ -32,4 +32,4 @@ else: import xlrd - xlrd_version = LooseVersion(xlrd.__version__) + xlrd_version = LooseVersion(get_version(xlrd)) diff --git a/pandas/util/_print_versions.py b/pandas/util/_print_versions.py index 5256cc29d5543..5b951cab1e3dc 100644 --- a/pandas/util/_print_versions.py +++ b/pandas/util/_print_versions.py @@ -8,7 +8,7 @@ from typing import Dict, Optional, Union from pandas._typing import JSONSerializable -from pandas.compat._optional import VERSIONS, _get_version, import_optional_dependency +from pandas.compat._optional import VERSIONS, get_version, import_optional_dependency def _get_commit_hash() -> Optional[str]: @@ -83,7 +83,7 @@ def _get_dependency_info() -> Dict[str, JSONSerializable]: mod = import_optional_dependency( modname, raise_on_missing=False, on_version="ignore" ) - result[modname] = _get_version(mod) if mod else None + result[modname] = get_version(mod) if mod else None return result diff --git a/scripts/validate_unwanted_patterns.py b/scripts/validate_unwanted_patterns.py index 9c58a55cb907e..8f48d518a737b 100755 --- a/scripts/validate_unwanted_patterns.py +++ b/scripts/validate_unwanted_patterns.py @@ -29,7 +29,6 @@ "_doc_template", "_agg_template", "_pipe_template", - "_get_version", "__main__", "_transform_template", "_flex_comp_doc_FRAME",