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
36 changes: 21 additions & 15 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,20 @@ def _ensure_term(where, scope_level: int):
def _tables():
global _table_mod
global _table_file_open_policy_is_strict
if _table_mod is None:
import tables

_table_mod = tables
# Fast-path: return cached module if already imported
if _table_mod is not None:
return _table_mod

# set the file open policy
# return the file open policy; this changes as of pytables 3.1
# depending on the HDF5 version
with suppress(AttributeError):
_table_file_open_policy_is_strict = (
tables.file._FILE_OPEN_POLICY == "strict"
)
import tables

_table_mod = tables

# set the file open policy
# return the file open policy; this changes as of pytables 3.1
# depending on the HDF5 version
with suppress(AttributeError):
_table_file_open_policy_is_strict = tables.file._FILE_OPEN_POLICY == "strict"

return _table_mod

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 @@ -2735,7 +2737,11 @@ def get_atom_data(cls, shape, kind: str) -> Col:

@classmethod
def get_atom_datetime64(cls, shape):
return _tables().Int64Col()
# Avoid repeated attribute lookups by caching Int64Col at the class level.
# This gives a small speedup if get_atom_datetime64 is called repeatedly.
if not hasattr(cls, "_atom_int64col"):
cls._atom_int64col = _tables().Int64Col()
return cls._atom_int64col

@classmethod
def get_atom_timedelta64(cls, shape):
Expand Down Expand Up @@ -4505,7 +4511,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 +4631,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 +5097,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