Skip to content

Commit b6f9117

Browse files
roadswitcherWilliam BlumMarcoGorelli
authored andcommitted
STYLE: enable pylint warnings for cell-var-from-loop (pandas-dev#49445)
* STYLE: Refactored one loop, suppressed three false positives. * Oooh, didn't think of that. * I need to get better at spotting this stuff. * Yeah, let's not suppress those after all. * Accidentally removed a newline. * Helps to revert to correct version of file. * OK, backing slowly away from the git client now. * OK, backing slowly away from the git client now. * Resolve autotyper find * Added type hints per reviewer note. * Update pandas/core/strings/object_array.py Co-authored-by: Marco Edward Gorelli <[email protected]> * Update pandas/tests/io/parser/test_c_parser_only.py Co-authored-by: Marco Edward Gorelli <[email protected]> Co-authored-by: William Blum <[email protected]> Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent ba800e5 commit b6f9117

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

pandas/core/strings/object_array.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from collections.abc import Callable # noqa: PDF001
4+
import functools
45
import re
56
import textwrap
67
from typing import (
@@ -380,9 +381,14 @@ def _str_get_dummies(self, sep: str = "|"):
380381

381382
dummies = np.empty((len(arr), len(tags2)), dtype=np.int64)
382383

384+
def _isin(test_elements: str, element: str) -> bool:
385+
return element in test_elements
386+
383387
for i, t in enumerate(tags2):
384388
pat = sep + t + sep
385-
dummies[:, i] = lib.map_infer(arr.to_numpy(), lambda x: pat in x)
389+
dummies[:, i] = lib.map_infer(
390+
arr.to_numpy(), functools.partial(_isin, element=pat)
391+
)
386392
return dummies, tags2
387393

388394
def _str_upper(self):

pandas/io/pytables.py

+28-28
Original file line numberDiff line numberDiff line change
@@ -4094,44 +4094,44 @@ def process_axes(self, obj, selection: Selection, columns=None) -> DataFrame:
40944094
for axis, labels in self.non_index_axes:
40954095
obj = _reindex_axis(obj, axis, labels, columns)
40964096

4097-
# apply the selection filters (but keep in the same order)
4098-
if selection.filter is not None:
4099-
for field, op, filt in selection.filter.format():
4097+
def process_filter(field, filt, op):
41004098

4101-
def process_filter(field, filt):
4099+
for axis_name in obj._AXIS_ORDERS:
4100+
axis_number = obj._get_axis_number(axis_name)
4101+
axis_values = obj._get_axis(axis_name)
4102+
assert axis_number is not None
41024103

4103-
for axis_name in obj._AXIS_ORDERS:
4104-
axis_number = obj._get_axis_number(axis_name)
4105-
axis_values = obj._get_axis(axis_name)
4106-
assert axis_number is not None
4104+
# see if the field is the name of an axis
4105+
if field == axis_name:
41074106

4108-
# see if the field is the name of an axis
4109-
if field == axis_name:
4107+
# if we have a multi-index, then need to include
4108+
# the levels
4109+
if self.is_multi_index:
4110+
filt = filt.union(Index(self.levels))
41104111

4111-
# if we have a multi-index, then need to include
4112-
# the levels
4113-
if self.is_multi_index:
4114-
filt = filt.union(Index(self.levels))
4112+
takers = op(axis_values, filt)
4113+
return obj.loc(axis=axis_number)[takers]
41154114

4116-
takers = op(axis_values, filt)
4117-
return obj.loc(axis=axis_number)[takers]
4115+
# this might be the name of a file IN an axis
4116+
elif field in axis_values:
41184117

4119-
# this might be the name of a file IN an axis
4120-
elif field in axis_values:
4118+
# we need to filter on this dimension
4119+
values = ensure_index(getattr(obj, field).values)
4120+
filt = ensure_index(filt)
41214121

4122-
# we need to filter on this dimension
4123-
values = ensure_index(getattr(obj, field).values)
4124-
filt = ensure_index(filt)
4122+
# hack until we support reversed dim flags
4123+
if isinstance(obj, DataFrame):
4124+
axis_number = 1 - axis_number
41254125

4126-
# hack until we support reversed dim flags
4127-
if isinstance(obj, DataFrame):
4128-
axis_number = 1 - axis_number
4129-
takers = op(values, filt)
4130-
return obj.loc(axis=axis_number)[takers]
4126+
takers = op(values, filt)
4127+
return obj.loc(axis=axis_number)[takers]
41314128

4132-
raise ValueError(f"cannot find the field [{field}] for filtering!")
4129+
raise ValueError(f"cannot find the field [{field}] for filtering!")
41334130

4134-
obj = process_filter(field, filt)
4131+
# apply the selection filters (but keep in the same order)
4132+
if selection.filter is not None:
4133+
for field, op, filt in selection.filter.format():
4134+
obj = process_filter(field, filt, op)
41354135

41364136
return obj
41374137

pandas/tests/io/parser/test_c_parser_only.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ def test_precise_conversion(c_parser_only):
176176
normal_errors = []
177177
precise_errors = []
178178

179+
def error(val: float, actual_val: Decimal) -> Decimal:
180+
return abs(Decimal(f"{val:.100}") - actual_val)
181+
179182
# test numbers between 1 and 2
180183
for num in np.linspace(1.0, 2.0, num=500):
181184
# 25 decimal digits of precision
@@ -192,11 +195,8 @@ def test_precise_conversion(c_parser_only):
192195
)
193196
actual_val = Decimal(text[2:])
194197

195-
def error(val):
196-
return abs(Decimal(f"{val:.100}") - actual_val)
197-
198-
normal_errors.append(error(normal_val))
199-
precise_errors.append(error(precise_val))
198+
normal_errors.append(error(normal_val, actual_val))
199+
precise_errors.append(error(precise_val, actual_val))
200200

201201
# round-trip should match float()
202202
assert roundtrip_val == float(text[2:])

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ disable = [
137137
"arguments-renamed",
138138
"attribute-defined-outside-init",
139139
"broad-except",
140-
"cell-var-from-loop",
141140
"comparison-with-callable",
142141
"confusing-with-statement",
143142
"dangerous-default-value",

0 commit comments

Comments
 (0)