Skip to content

Commit 98b9049

Browse files
committed
REF: avoid IndexError check
1 parent f5df4c0 commit 98b9049

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

pandas/io/pytables.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@
1010
import os
1111
import re
1212
from textwrap import dedent
13-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
13+
from typing import (
14+
TYPE_CHECKING,
15+
Any,
16+
Dict,
17+
List,
18+
Optional,
19+
Sequence,
20+
Tuple,
21+
Type,
22+
Union,
23+
)
1424
import warnings
1525

1626
import numpy as np
@@ -4998,7 +5008,7 @@ def _need_convert(kind: str) -> bool:
49985008
return False
49995009

50005010

5001-
def _maybe_adjust_name(name: str, version) -> str:
5011+
def _maybe_adjust_name(name: str, version: Sequence[int]) -> str:
50025012
"""
50035013
Prior to 0.10.1, we named values blocks like: values_block_0 an the
50045014
name values_0, adjust the given name if necessary.
@@ -5012,12 +5022,14 @@ def _maybe_adjust_name(name: str, version) -> str:
50125022
-------
50135023
str
50145024
"""
5015-
with suppress(IndexError):
5016-
if version[0] == 0 and version[1] <= 10 and version[2] == 0:
5017-
m = re.search(r"values_block_(\d+)", name)
5018-
if m:
5019-
grp = m.groups()[0]
5020-
name = f"values_{grp}"
5025+
if isinstance(version, str) or len(version) < 3:
5026+
raise ValueError("Version is incorrect, expected sequence of 3 integers.")
5027+
5028+
if version[0] == 0 and version[1] <= 10 and version[2] == 0:
5029+
m = re.search(r"values_block_(\d+)", name)
5030+
if m:
5031+
grp = m.groups()[0]
5032+
name = f"values_{grp}"
50215033
return name
50225034

50235035

pandas/tests/io/pytables/test_store.py

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
)
4646

4747
from pandas.io.pytables import (
48+
_maybe_adjust_name,
4849
ClosedFileError,
4950
HDFStore,
5051
PossibleDataLossError,
@@ -4921,3 +4922,10 @@ def test_unsuppored_hdf_file_error(self, datapath):
49214922

49224923
with pytest.raises(ValueError, match=message):
49234924
pd.read_hdf(data_path)
4925+
4926+
4927+
@pytest.mark.parametrize("bad_version", [(1, 2), (1,), [], '12', '123'])
4928+
def test_maybe_adjust_name_bad_version_raises(bad_version):
4929+
msg = "Version is incorrect, expected sequence of 3 integers"
4930+
with pytest.raises(ValueError, match=msg):
4931+
_maybe_adjust_name("values_block_0", version=bad_version)

0 commit comments

Comments
 (0)