Skip to content

CLN: remove extant uses of built-in filter function #35717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2020
Merged
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
14 changes: 8 additions & 6 deletions pandas/_config/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ def _valid_locales(locales, normalize):
valid_locales : list
A list of valid locales.
"""
if normalize:
normalizer = lambda x: locale.normalize(x.strip())
else:
normalizer = lambda x: x.strip()

return list(filter(can_set_locale, map(normalizer, locales)))
return [
loc
for loc in (
locale.normalize(loc.strip()) if normalize else loc.strip()
for loc in locales
)
if can_set_locale(loc)
]


def _default_locale_getter():
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ def _is_type(t):

# partition all AST nodes
_all_nodes = frozenset(
filter(
lambda x: isinstance(x, type) and issubclass(x, ast.AST),
(getattr(ast, node) for node in dir(ast)),
)
node
for node in (getattr(ast, name) for name in dir(ast))
if isinstance(node, type) and issubclass(node, ast.AST)
)


Expand Down
7 changes: 5 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2012,8 +2012,11 @@ def _sort_labels(uniques: np.ndarray, left, right):
def _get_join_keys(llab, rlab, shape, sort: bool):

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

# get keys for the first `nlev` levels
stride = np.prod(shape[1:nlev], dtype="i8")
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,9 @@ def _combine_lines(self, lines) -> str:
"""
Combines a list of JSON objects into one JSON object.
"""
lines = filter(None, map(lambda x: x.strip(), lines))
return "[" + ",".join(lines) + "]"
return (
f'[{",".join((line for line in (line.strip() for line in lines) if line))}]'
)

def read(self):
"""
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2161,9 +2161,7 @@ def read(self, nrows=None):
if self.usecols is not None:
columns = self._filter_usecols(columns)

col_dict = dict(
filter(lambda item: item[0] in columns, col_dict.items())
)
col_dict = {k: v for k, v in col_dict.items() if k in columns}

return index, columns, col_dict

Expand Down
20 changes: 9 additions & 11 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,20 @@ def _ensure_str(name):

def _ensure_term(where, scope_level: int):
"""
ensure that the where is a Term or a list of Term
this makes sure that we are capturing the scope of variables
that are passed
create the terms here with a frame_level=2 (we are 2 levels down)
Ensure that the where is a Term or a list of Term.

This makes sure that we are capturing the scope of variables that are
passed create the terms here with a frame_level=2 (we are 2 levels down)
"""
# only consider list/tuple here as an ndarray is automatically a coordinate
# list
level = scope_level + 1
if isinstance(where, (list, tuple)):
wlist = []
for w in filter(lambda x: x is not None, where):
if not maybe_expression(w):
wlist.append(w)
else:
wlist.append(Term(w, scope_level=level))
where = wlist
where = [
Term(term, scope_level=level + 1) if maybe_expression(term) else term
for term in where
if term is not None
]
elif maybe_expression(where):
where = Term(where, scope_level=level)
return where if where is None or len(where) else None
Expand Down
26 changes: 15 additions & 11 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def setup_ops(self):
def setup_method(self, method):
self.setup_ops()
self.setup_data()
self.current_engines = filter(lambda x: x != self.engine, _engines)
self.current_engines = (engine for engine in _engines if engine != self.engine)

def teardown_method(self, method):
del self.lhses, self.rhses, self.scalar_rhses, self.scalar_lhses
Expand Down Expand Up @@ -774,11 +774,9 @@ def setup_class(cls):
cls.parser = "python"

def setup_ops(self):
self.cmp_ops = list(
filter(lambda x: x not in ("in", "not in"), expr._cmp_ops_syms)
)
self.cmp_ops = [op for op in expr._cmp_ops_syms if op not in ("in", "not in")]
self.cmp2_ops = self.cmp_ops[::-1]
self.bin_ops = [s for s in expr._bool_ops_syms if s not in ("and", "or")]
self.bin_ops = [op for op in expr._bool_ops_syms if op not in ("and", "or")]
self.special_case_ops = _special_case_arith_ops_syms
self.arith_ops = _good_arith_ops
self.unary_ops = "+", "-", "~"
Expand Down Expand Up @@ -1150,9 +1148,9 @@ def eval(self, *args, **kwargs):
return pd.eval(*args, **kwargs)

def test_simple_arith_ops(self):
ops = self.arith_ops
ops = (op for op in self.arith_ops if op != "//")

for op in filter(lambda x: x != "//", ops):
for op in ops:
ex = f"1 {op} 1"
ex2 = f"x {op} 1"
ex3 = f"1 {op} (x + 1)"
Expand Down Expand Up @@ -1637,8 +1635,11 @@ def setup_class(cls):
super().setup_class()
cls.engine = "numexpr"
cls.parser = "python"
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
cls.arith_ops = filter(lambda x: x not in ("in", "not in"), cls.arith_ops)
cls.arith_ops = [
op
for op in expr._arith_ops_syms + expr._cmp_ops_syms
if op not in ("in", "not in")
]

def test_check_many_exprs(self):
a = 1 # noqa
Expand Down Expand Up @@ -1726,8 +1727,11 @@ class TestOperationsPythonPython(TestOperationsNumExprPython):
def setup_class(cls):
super().setup_class()
cls.engine = cls.parser = "python"
cls.arith_ops = expr._arith_ops_syms + expr._cmp_ops_syms
cls.arith_ops = filter(lambda x: x not in ("in", "not in"), cls.arith_ops)
cls.arith_ops = [
op
for op in expr._arith_ops_syms + expr._cmp_ops_syms
if op not in ("in", "not in")
]


class TestOperationsPythonPandas(TestOperationsNumExprPandas):
Expand Down