Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 15 additions & 8 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
npt,
)

_operations = None


class PyTablesScope(_scope.Scope):
__slots__ = ("queryables",)
Expand Down Expand Up @@ -408,11 +410,12 @@ def prune(self, klass):
operand = operand.prune(klass)

if operand is not None and (
issubclass(klass, ConditionBinOp)
and operand.condition is not None
or not issubclass(klass, ConditionBinOp)
and issubclass(klass, FilterBinOp)
and operand.filter is not None
(issubclass(klass, ConditionBinOp) and operand.condition is not None)
or (
not issubclass(klass, ConditionBinOp)
and issubclass(klass, FilterBinOp)
and operand.filter is not None
)
):
return operand.invert()
return None
Expand Down Expand Up @@ -657,9 +660,13 @@ def tostring(self, encoding) -> str:

def maybe_expression(s) -> bool:
"""loose checking if s is a pytables-acceptable expression"""
global _operations
if not isinstance(s, str):
return False
operations = PyTablesExprVisitor.binary_ops + PyTablesExprVisitor.unary_ops + ("=",)

# Delay creation of operations tuple until first function call
if _operations is None:
_operations = (
PyTablesExprVisitor.binary_ops + PyTablesExprVisitor.unary_ops + ("=",)
)
# make sure we have an op at least
return any(op in s for op in operations)
return any(op in s for op in _operations)
8 changes: 4 additions & 4 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ def info(self) -> str:

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

Expand Down Expand Up @@ -4505,7 +4505,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 +4625,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 +5091,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