diff --git a/pandas/core/strings/object_array.py b/pandas/core/strings/object_array.py index d45cfa8de6f3e..21e7ede3ed386 100644 --- a/pandas/core/strings/object_array.py +++ b/pandas/core/strings/object_array.py @@ -1,6 +1,7 @@ from __future__ import annotations from collections.abc import Callable # noqa: PDF001 +import functools import re import textwrap from typing import ( @@ -380,9 +381,14 @@ def _str_get_dummies(self, sep: str = "|"): dummies = np.empty((len(arr), len(tags2)), dtype=np.int64) + def _isin(test_elements: str, element: str) -> bool: + return element in test_elements + for i, t in enumerate(tags2): pat = sep + t + sep - dummies[:, i] = lib.map_infer(arr.to_numpy(), lambda x: pat in x) + dummies[:, i] = lib.map_infer( + arr.to_numpy(), functools.partial(_isin, element=pat) + ) return dummies, tags2 def _str_upper(self): diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 1f2bb4c5d21b4..9977c78aab89c 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -4106,44 +4106,44 @@ def process_axes(self, obj, selection: Selection, columns=None) -> DataFrame: for axis, labels in self.non_index_axes: obj = _reindex_axis(obj, axis, labels, columns) - # apply the selection filters (but keep in the same order) - if selection.filter is not None: - for field, op, filt in selection.filter.format(): + def process_filter(field, filt, op): - def process_filter(field, filt): + for axis_name in obj._AXIS_ORDERS: + axis_number = obj._get_axis_number(axis_name) + axis_values = obj._get_axis(axis_name) + assert axis_number is not None - for axis_name in obj._AXIS_ORDERS: - axis_number = obj._get_axis_number(axis_name) - axis_values = obj._get_axis(axis_name) - assert axis_number is not None + # see if the field is the name of an axis + if field == axis_name: - # see if the field is the name of an axis - if field == axis_name: + # if we have a multi-index, then need to include + # the levels + if self.is_multi_index: + filt = filt.union(Index(self.levels)) - # if we have a multi-index, then need to include - # the levels - if self.is_multi_index: - filt = filt.union(Index(self.levels)) + takers = op(axis_values, filt) + return obj.loc(axis=axis_number)[takers] - takers = op(axis_values, filt) - return obj.loc(axis=axis_number)[takers] + # this might be the name of a file IN an axis + elif field in axis_values: - # this might be the name of a file IN an axis - elif field in axis_values: + # we need to filter on this dimension + values = ensure_index(getattr(obj, field).values) + filt = ensure_index(filt) - # we need to filter on this dimension - values = ensure_index(getattr(obj, field).values) - filt = ensure_index(filt) + # hack until we support reversed dim flags + if isinstance(obj, DataFrame): + axis_number = 1 - axis_number - # hack until we support reversed dim flags - if isinstance(obj, DataFrame): - axis_number = 1 - axis_number - takers = op(values, filt) - return obj.loc(axis=axis_number)[takers] + takers = op(values, filt) + return obj.loc(axis=axis_number)[takers] - raise ValueError(f"cannot find the field [{field}] for filtering!") + raise ValueError(f"cannot find the field [{field}] for filtering!") - obj = process_filter(field, filt) + # apply the selection filters (but keep in the same order) + if selection.filter is not None: + for field, op, filt in selection.filter.format(): + obj = process_filter(field, filt, op) return obj diff --git a/pandas/tests/io/parser/test_c_parser_only.py b/pandas/tests/io/parser/test_c_parser_only.py index ecc49ea8adb9f..ec08fb0d60648 100644 --- a/pandas/tests/io/parser/test_c_parser_only.py +++ b/pandas/tests/io/parser/test_c_parser_only.py @@ -176,6 +176,9 @@ def test_precise_conversion(c_parser_only): normal_errors = [] precise_errors = [] + def error(val: float, actual_val: Decimal) -> Decimal: + return abs(Decimal(f"{val:.100}") - actual_val) + # test numbers between 1 and 2 for num in np.linspace(1.0, 2.0, num=500): # 25 decimal digits of precision @@ -192,11 +195,8 @@ def test_precise_conversion(c_parser_only): ) actual_val = Decimal(text[2:]) - def error(val): - return abs(Decimal(f"{val:.100}") - actual_val) - - normal_errors.append(error(normal_val)) - precise_errors.append(error(precise_val)) + normal_errors.append(error(normal_val, actual_val)) + precise_errors.append(error(precise_val, actual_val)) # round-trip should match float() assert roundtrip_val == float(text[2:]) diff --git a/pyproject.toml b/pyproject.toml index 991f6eb24a778..eb24039d1647d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -138,7 +138,6 @@ disable = [ "arguments-renamed", "attribute-defined-outside-init", "broad-except", - "cell-var-from-loop", "comparison-with-callable", "confusing-with-statement", "dangerous-default-value",