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
25 changes: 19 additions & 6 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@

from pandas.core.internals import Block

_atom_coltype_cache = {}

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

# versioning attribute
_version = "0.15.2"

Expand Down Expand Up @@ -1750,7 +1754,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 +2565,12 @@ 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"""
# Optimization: Use a dedicated cache for (kind -> type[Col]).
cache = _atom_coltype_cache
coltype = cache.get(kind)
if coltype is not None:
return coltype

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 @@ -2731,6 +2743,7 @@ def get_atom_string(cls, shape, itemsize):

@classmethod
def get_atom_data(cls, shape, kind: str) -> Col:
# call to get_atom_coltype is cheap now due to cache
return cls.get_atom_coltype(kind=kind)()

@classmethod
Expand Down Expand Up @@ -4505,7 +4518,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 +4638,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 +5104,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 +5278,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