Skip to content

Commit faa6e36

Browse files
CLN: remove extant uses of built-in filter function (#35717)
1 parent 6ea8474 commit faa6e36

File tree

7 files changed

+44
-39
lines changed

7 files changed

+44
-39
lines changed

pandas/_config/localization.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ def _valid_locales(locales, normalize):
8888
valid_locales : list
8989
A list of valid locales.
9090
"""
91-
if normalize:
92-
normalizer = lambda x: locale.normalize(x.strip())
93-
else:
94-
normalizer = lambda x: x.strip()
95-
96-
return list(filter(can_set_locale, map(normalizer, locales)))
91+
return [
92+
loc
93+
for loc in (
94+
locale.normalize(loc.strip()) if normalize else loc.strip()
95+
for loc in locales
96+
)
97+
if can_set_locale(loc)
98+
]
9799

98100

99101
def _default_locale_getter():

pandas/core/computation/expr.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ def _is_type(t):
167167

168168
# partition all AST nodes
169169
_all_nodes = frozenset(
170-
filter(
171-
lambda x: isinstance(x, type) and issubclass(x, ast.AST),
172-
(getattr(ast, node) for node in dir(ast)),
173-
)
170+
node
171+
for node in (getattr(ast, name) for name in dir(ast))
172+
if isinstance(node, type) and issubclass(node, ast.AST)
174173
)
175174

176175

pandas/core/reshape/merge.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2012,8 +2012,11 @@ def _sort_labels(uniques: np.ndarray, left, right):
20122012
def _get_join_keys(llab, rlab, shape, sort: bool):
20132013

20142014
# how many levels can be done without overflow
2015-
pred = lambda i: not is_int64_overflow_possible(shape[:i])
2016-
nlev = next(filter(pred, range(len(shape), 0, -1)))
2015+
nlev = next(
2016+
lev
2017+
for lev in range(len(shape), 0, -1)
2018+
if not is_int64_overflow_possible(shape[:lev])
2019+
)
20172020

20182021
# get keys for the first `nlev` levels
20192022
stride = np.prod(shape[1:nlev], dtype="i8")

pandas/io/json/_json.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,9 @@ def _combine_lines(self, lines) -> str:
765765
"""
766766
Combines a list of JSON objects into one JSON object.
767767
"""
768-
lines = filter(None, map(lambda x: x.strip(), lines))
769-
return "[" + ",".join(lines) + "]"
768+
return (
769+
f'[{",".join((line for line in (line.strip() for line in lines) if line))}]'
770+
)
770771

771772
def read(self):
772773
"""

pandas/io/parsers.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2161,9 +2161,7 @@ def read(self, nrows=None):
21612161
if self.usecols is not None:
21622162
columns = self._filter_usecols(columns)
21632163

2164-
col_dict = dict(
2165-
filter(lambda item: item[0] in columns, col_dict.items())
2166-
)
2164+
col_dict = {k: v for k, v in col_dict.items() if k in columns}
21672165

21682166
return index, columns, col_dict
21692167

pandas/io/pytables.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,20 @@ def _ensure_str(name):
9999

100100
def _ensure_term(where, scope_level: int):
101101
"""
102-
ensure that the where is a Term or a list of Term
103-
this makes sure that we are capturing the scope of variables
104-
that are passed
105-
create the terms here with a frame_level=2 (we are 2 levels down)
102+
Ensure that the where is a Term or a list of Term.
103+
104+
This makes sure that we are capturing the scope of variables that are
105+
passed create the terms here with a frame_level=2 (we are 2 levels down)
106106
"""
107107
# only consider list/tuple here as an ndarray is automatically a coordinate
108108
# list
109109
level = scope_level + 1
110110
if isinstance(where, (list, tuple)):
111-
wlist = []
112-
for w in filter(lambda x: x is not None, where):
113-
if not maybe_expression(w):
114-
wlist.append(w)
115-
else:
116-
wlist.append(Term(w, scope_level=level))
117-
where = wlist
111+
where = [
112+
Term(term, scope_level=level + 1) if maybe_expression(term) else term
113+
for term in where
114+
if term is not None
115+
]
118116
elif maybe_expression(where):
119117
where = Term(where, scope_level=level)
120118
return where if where is None or len(where) else None

pandas/tests/computation/test_eval.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def setup_ops(self):
168168
def setup_method(self, method):
169169
self.setup_ops()
170170
self.setup_data()
171-
self.current_engines = filter(lambda x: x != self.engine, _engines)
171+
self.current_engines = (engine for engine in _engines if engine != self.engine)
172172

173173
def teardown_method(self, method):
174174
del self.lhses, self.rhses, self.scalar_rhses, self.scalar_lhses
@@ -774,11 +774,9 @@ def setup_class(cls):
774774
cls.parser = "python"
775775

776776
def setup_ops(self):
777-
self.cmp_ops = list(
778-
filter(lambda x: x not in ("in", "not in"), expr._cmp_ops_syms)
779-
)
777+
self.cmp_ops = [op for op in expr._cmp_ops_syms if op not in ("in", "not in")]
780778
self.cmp2_ops = self.cmp_ops[::-1]
781-
self.bin_ops = [s for s in expr._bool_ops_syms if s not in ("and", "or")]
779+
self.bin_ops = [op for op in expr._bool_ops_syms if op not in ("and", "or")]
782780
self.special_case_ops = _special_case_arith_ops_syms
783781
self.arith_ops = _good_arith_ops
784782
self.unary_ops = "+", "-", "~"
@@ -1150,9 +1148,9 @@ def eval(self, *args, **kwargs):
11501148
return pd.eval(*args, **kwargs)
11511149

11521150
def test_simple_arith_ops(self):
1153-
ops = self.arith_ops
1151+
ops = (op for op in self.arith_ops if op != "//")
11541152

1155-
for op in filter(lambda x: x != "//", ops):
1153+
for op in ops:
11561154
ex = f"1 {op} 1"
11571155
ex2 = f"x {op} 1"
11581156
ex3 = f"1 {op} (x + 1)"
@@ -1637,8 +1635,11 @@ def setup_class(cls):
16371635
super().setup_class()
16381636
cls.engine = "numexpr"
16391637
cls.parser = "python"
1640-
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
1641-
cls.arith_ops = filter(lambda x: x not in ("in", "not in"), cls.arith_ops)
1638+
cls.arith_ops = [
1639+
op
1640+
for op in expr._arith_ops_syms + expr._cmp_ops_syms
1641+
if op not in ("in", "not in")
1642+
]
16421643

16431644
def test_check_many_exprs(self):
16441645
a = 1 # noqa
@@ -1726,8 +1727,11 @@ class TestOperationsPythonPython(TestOperationsNumExprPython):
17261727
def setup_class(cls):
17271728
super().setup_class()
17281729
cls.engine = cls.parser = "python"
1729-
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
1730-
cls.arith_ops = filter(lambda x: x not in ("in", "not in"), cls.arith_ops)
1730+
cls.arith_ops = [
1731+
op
1732+
for op in expr._arith_ops_syms + expr._cmp_ops_syms
1733+
if op not in ("in", "not in")
1734+
]
17311735

17321736

17331737
class TestOperationsPythonPandas(TestOperationsNumExprPandas):

0 commit comments

Comments
 (0)