From 76d2917d55b98e2bdd28dae01db2b7eed2a345a1 Mon Sep 17 00:00:00 2001 From: Andrew Uzzell Date: Fri, 13 Jan 2023 16:24:39 -0500 Subject: [PATCH 1/2] Enable pylint undefined-loop-variable check --- pandas/core/util/hashing.py | 4 +++- pandas/io/excel/_base.py | 4 +++- pandas/tests/frame/methods/test_to_dict_of_blocks.py | 10 +++++++--- pandas/tests/io/formats/test_printing.py | 6 ++++-- pyproject.toml | 1 - 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pandas/core/util/hashing.py b/pandas/core/util/hashing.py index e0b18047aa0ec..350914cc50556 100644 --- a/pandas/core/util/hashing.py +++ b/pandas/core/util/hashing.py @@ -71,12 +71,14 @@ def combine_hash_arrays( mult = np.uint64(1000003) out = np.zeros_like(first) + np.uint64(0x345678) + last_i = 0 for i, a in enumerate(arrays): inverse_i = num_items - i out ^= a out *= mult mult += np.uint64(82520 + inverse_i + inverse_i) - assert i + 1 == num_items, "Fed in wrong num_items" + last_i = i + assert last_i + 1 == num_items, "Fed in wrong num_items" out += np.uint64(97531) return out diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 6f706a4554855..3d56fe8ed71b2 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -726,7 +726,9 @@ def parse( output = {} + last_sheetname = None for asheetname in sheets: + last_sheetname = asheetname if verbose: print(f"Reading sheet {asheetname}") @@ -881,7 +883,7 @@ def parse( if ret_dict: return output else: - return output[asheetname] + return output[last_sheetname] @doc(storage_options=_shared_docs["storage_options"]) diff --git a/pandas/tests/frame/methods/test_to_dict_of_blocks.py b/pandas/tests/frame/methods/test_to_dict_of_blocks.py index 9705f24d0286c..cc4860beea491 100644 --- a/pandas/tests/frame/methods/test_to_dict_of_blocks.py +++ b/pandas/tests/frame/methods/test_to_dict_of_blocks.py @@ -20,30 +20,34 @@ def test_copy_blocks(self, float_frame): column = df.columns[0] # use the default copy=True, change a column + _last_df = None blocks = df._to_dict_of_blocks(copy=True) for _df in blocks.values(): + _last_df = _df if column in _df: _df.loc[:, column] = _df[column] + 1 # make sure we did not change the original DataFrame - assert not _df[column].equals(df[column]) + assert _last_df is not None and not _last_df[column].equals(df[column]) def test_no_copy_blocks(self, float_frame, using_copy_on_write): # GH#9607 df = DataFrame(float_frame, copy=True) column = df.columns[0] + _last_df = None # use the copy=False, change a column blocks = df._to_dict_of_blocks(copy=False) for _df in blocks.values(): + _last_df = _df if column in _df: _df.loc[:, column] = _df[column] + 1 if not using_copy_on_write: # make sure we did change the original DataFrame - assert _df[column].equals(df[column]) + assert _last_df is not None and _last_df[column].equals(df[column]) else: - assert not _df[column].equals(df[column]) + assert _last_df is not None and not _last_df[column].equals(df[column]) def test_to_dict_of_blocks_item_cache(request, using_copy_on_write): diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index 4ded7bebc431e..98d05d17bb43c 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -126,14 +126,16 @@ class TestTableSchemaRepr: def test_publishes(self, ip): ipython = ip.instance(config=ip.config) df = pd.DataFrame({"A": [1, 2]}) - objects = [df["A"], df, df] # dataframe / series + objects = [df["A"], df] # dataframe / series expected_keys = [ {"text/plain", "application/vnd.dataresource+json"}, {"text/plain", "text/html", "application/vnd.dataresource+json"}, ] opt = pd.option_context("display.html.table_schema", True) + last_obj = None for obj, expected in zip(objects, expected_keys): + last_obj = obj with opt: formatted = ipython.display_formatter.format(obj) assert set(formatted[0].keys()) == expected @@ -141,7 +143,7 @@ def test_publishes(self, ip): with_latex = pd.option_context("display.latex.repr", True) with opt, with_latex: - formatted = ipython.display_formatter.format(obj) + formatted = ipython.display_formatter.format(last_obj) expected = { "text/plain", diff --git a/pyproject.toml b/pyproject.toml index 0308f86beb0d4..76c03ff17a232 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -287,7 +287,6 @@ disable = [ "too-many-public-methods", "too-many-return-statements", "too-many-statements", - "undefined-loop-variable", "unexpected-keyword-arg", "ungrouped-imports", "unsubscriptable-object", From 9761a84c2ad8dae0bb960c3dc4e180b26bbba688 Mon Sep 17 00:00:00 2001 From: Andrew Uzzell Date: Wed, 25 Jan 2023 11:32:35 -0500 Subject: [PATCH 2/2] Respond to review comments --- pandas/io/excel/_base.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 3d56fe8ed71b2..54ee5e82e6d55 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -880,6 +880,9 @@ def parse( err.args = (f"{err.args[0]} (sheet: {asheetname})", *err.args[1:]) raise err + if last_sheetname is None: + raise ValueError("Sheet name is an empty list") + if ret_dict: return output else: