Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@

from pandas.core.internals import Block

_VALUES_BLOCK_RE = re.compile(r"values_block_(\d+)")

# versioning attribute
_version = "0.15.2"

Expand Down Expand Up @@ -1750,7 +1752,7 @@ def info(self) -> str:

if self.is_open:
lkeys = sorted(self.keys())
if len(lkeys):
if lkeys:
keys = []
values = []

Expand Down Expand Up @@ -2561,6 +2563,14 @@ def get_atom_string(cls, shape, itemsize):
@classmethod
def get_atom_coltype(cls, kind: str) -> type[Col]:
"""return the PyTables column class for this column"""
cache = getattr(cls, "_coltype_cache", None)
if cache is None:
cache = {}
setattr(cls, "_coltype_cache", cache)

if kind in cache:
return cache[kind]

if kind.startswith("uint"):
k4 = kind[4:]
col_name = f"UInt{k4}Col"
Expand All @@ -2571,7 +2581,9 @@ def get_atom_coltype(cls, kind: str) -> type[Col]:
kcap = kind.capitalize()
col_name = f"{kcap}Col"

return getattr(_tables(), col_name)
coltype = getattr(_tables(), col_name)
cache[kind] = coltype
return coltype

@classmethod
def get_atom_data(cls, shape, kind: str) -> Col:
Expand Down Expand Up @@ -4505,7 +4517,7 @@ def write_data(self, chunksize: int | None, dropna: bool = False) -> None:
masks.append(mask.astype("u1", copy=False))

# consolidate masks
if len(masks):
if masks:
mask = masks[0]
for m in masks[1:]:
mask = mask & m
Expand Down Expand Up @@ -4625,7 +4637,7 @@ def delete(
groups = list(diff[diff > 1].index)

# 1 group
if not len(groups):
if not groups:
groups = [0]

# final element
Expand Down Expand Up @@ -5091,7 +5103,7 @@ def _maybe_convert_for_string_atom(
if bvalues.dtype != object:
return bvalues

bvalues = cast(np.ndarray, bvalues)
bvalues = cast("np.ndarray", bvalues)

dtype_name = bvalues.dtype.name
inferred_type = lib.infer_dtype(bvalues, skipna=False)
Expand Down Expand Up @@ -5265,7 +5277,7 @@ def _maybe_adjust_name(name: str, version: Sequence[int]) -> str:
raise ValueError("Version is incorrect, expected sequence of 3 integers.")

if version[0] == 0 and version[1] <= 10 and version[2] == 0:
m = re.search(r"values_block_(\d+)", name)
m = _VALUES_BLOCK_RE.search(name)
if m:
grp = m.groups()[0]
name = f"values_{grp}"
Expand Down