Skip to content

STY: Enable ruff C4 - comprehensions #53056

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 2 commits into from
May 3, 2023
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
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class Dtypes:
params = _dtypes + list(map(lambda dt: dt.name, _dtypes))
params = _dtypes + [dt.name for dt in _dtypes]
param_names = ["dtype"]

def time_pandas_dtype(self, dtype):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _filter_nodes(superclass, all_nodes=_all_nodes):
return frozenset(node_names)


_all_node_names = frozenset(map(lambda x: x.__name__, _all_nodes))
_all_node_names = frozenset(x.__name__ for x in _all_nodes)
_mod_nodes = _filter_nodes(ast.mod)
_stmt_nodes = _filter_nodes(ast.stmt)
_expr_nodes = _filter_nodes(ast.expr)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4115,9 +4115,9 @@ def _reindex_output(
# reindex `output`, and then reset the in-axis grouper columns.

# Select in-axis groupers
in_axis_grps = list(
in_axis_grps = [
(i, ping.name) for (i, ping) in enumerate(groupings) if ping.in_axis
)
]
if len(in_axis_grps) > 0:
g_nums, g_names = zip(*in_axis_grps)
output = output.drop(labels=list(g_names), axis=1)
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def _format_header(self) -> Iterable[ExcelCell]:
row = [x if x is not None else "" for x in self.df.index.names] + [
""
] * len(self.columns)
if reduce(lambda x, y: x and y, map(lambda x: x != "", row)):
if reduce(lambda x, y: x and y, (x != "" for x in row)):
gen2 = (
ExcelCell(self.rowcounter, colindex, val, self.header_style)
for colindex, val in enumerate(row)
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def write_style(self) -> None:
)
else:
element_props.append(("thead th", "text-align", "right"))
template_mid = "\n\n".join(map(lambda t: template_select % t, element_props))
template_mid = "\n\n".join(template_select % t for t in element_props)
template = dedent("\n".join((template_first, template_mid, template_last)))
self.write(template)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_compound_invert_op(self, op, lhs, rhs, request, engine, parser):
else:
# compound
if is_scalar(lhs) and is_scalar(rhs):
lhs, rhs = map(lambda x: np.array([x]), (lhs, rhs))
lhs, rhs = (np.array([x]) for x in (lhs, rhs))
expected = _eval_single_bin(lhs, op, rhs, engine)
if is_scalar(expected):
expected = not expected
Expand Down Expand Up @@ -746,7 +746,7 @@ def test_binop_typecasting(self, engine, parser, op, dt, left_right):
def should_warn(*args):
not_mono = not any(map(operator.attrgetter("is_monotonic_increasing"), args))
only_one_dt = reduce(
operator.xor, map(lambda x: issubclass(x.dtype.type, np.datetime64), args)
operator.xor, (issubclass(x.dtype.type, np.datetime64) for x in args)
)
return not_mono and only_one_dt

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def test_array_equivalent_nested_mixed_list(strict_nan):
np.array(["c", "d"], dtype=object),
]
left = np.array([subarr, None], dtype=object)
right = np.array([list([[None, "b"], ["c", "d"]]), None], dtype=object)
right = np.array([[[None, "b"], ["c", "d"]], None], dtype=object)
assert array_equivalent(left, right, strict_nan=strict_nan)
assert not array_equivalent(left, right[::-1], strict_nan=strict_nan)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/extension/date/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __init__(
self._day = np.zeros(ldates, dtype=np.uint8) # 255 (1, 12)
# populate them
for i, (y, m, d) in enumerate(
map(lambda date: (date.year, date.month, date.day), dates)
(date.year, date.month, date.day) for date in dates
):
self._year[i] = y
self._month[i] = m
Expand All @@ -94,7 +94,7 @@ def __init__(
if ldates != 3:
raise ValueError("only triples are valid")
# check if all elements have the same type
if any(map(lambda x: not isinstance(x, np.ndarray), dates)):
if any(not isinstance(x, np.ndarray) for x in dates):
raise TypeError("invalid type")
ly, lm, ld = (len(cast(np.ndarray, d)) for d in dates)
if not ly == lm == ld:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ def test_astype_str(self):

expected = DataFrame(
{
"a": list(map(str, map(lambda x: Timestamp(x)._date_repr, a._values))),
"a": list(map(str, (Timestamp(x)._date_repr for x in a._values))),
"b": list(map(str, map(Timestamp, b._values))),
"c": list(map(lambda x: Timedelta(x)._repr_base(), c._values)),
"c": [Timedelta(x)._repr_base() for x in c._values],
"d": list(map(str, d._values)),
"e": list(map(str, e._values)),
}
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ def test_multi_iter_frame(self, three_group):
# calling `dict` on a DataFrameGroupBy leads to a TypeError,
# we need to use a dictionary comprehension here
# pylint: disable-next=unnecessary-comprehension
groups = {key: gp for key, gp in grouped}
groups = {key: gp for key, gp in grouped} # noqa: C416
assert len(groups) == 2

# axis = 1
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/groupby/test_raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_groupby_raises_string_np(
),
}[groupby_func_np]

_call_and_check(klass, msg, how, gb, groupby_func_np, tuple())
_call_and_check(klass, msg, how, gb, groupby_func_np, ())


@pytest.mark.parametrize("how", ["method", "agg", "transform"])
Expand Down Expand Up @@ -333,7 +333,7 @@ def test_groupby_raises_datetime_np(
np.mean: (None, ""),
}[groupby_func_np]

_call_and_check(klass, msg, how, gb, groupby_func_np, tuple())
_call_and_check(klass, msg, how, gb, groupby_func_np, ())


@pytest.mark.parametrize("func", ["prod", "cumprod", "skew", "var"])
Expand Down Expand Up @@ -526,7 +526,7 @@ def test_groupby_raises_category_np(
),
}[groupby_func_np]

_call_and_check(klass, msg, how, gb, groupby_func_np, tuple())
_call_and_check(klass, msg, how, gb, groupby_func_np, ())


@pytest.mark.parametrize("how", ["method", "agg", "transform"])
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ def test_dispatch_transform(tsframe):

def test_transform_fillna_null():
df = DataFrame(
dict(
price=[10, 10, 20, 20, 30, 30],
color=[10, 10, 20, 20, 30, 30],
cost=(100, 200, 300, 400, 500, 600),
)
{
"price": [10, 10, 20, 20, 30, 30],
"color": [10, 10, 20, 20, 30, 30],
"cost": (100, 200, 300, 400, 500, 600),
}
)
with pytest.raises(ValueError, match="Must specify a fill 'value' or 'method'"):
df.groupby(["price"]).transform("fillna")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_indexing_slow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
np.random.choice(list("ZYXWVUTSRQP"), m),
]
keys = list(map(tuple, zip(*keys)))
keys += list(map(lambda t: t[:-1], vals[:: n // m]))
keys += [t[:-1] for t in vals[:: n // m]]


# covers both unique index and non-unique index
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,30 @@ def test_partial_setting_frame(self, using_array_manager):
df.iat[4, 2] = 5.0

# row setting where it exists
expected = DataFrame(dict({"A": [0, 4, 4], "B": [1, 5, 5]}))
expected = DataFrame({"A": [0, 4, 4], "B": [1, 5, 5]})
df = df_orig.copy()
df.iloc[1] = df.iloc[2]
tm.assert_frame_equal(df, expected)

expected = DataFrame(dict({"A": [0, 4, 4], "B": [1, 5, 5]}))
expected = DataFrame({"A": [0, 4, 4], "B": [1, 5, 5]})
df = df_orig.copy()
df.loc[1] = df.loc[2]
tm.assert_frame_equal(df, expected)

# like 2578, partial setting with dtype preservation
expected = DataFrame(dict({"A": [0, 2, 4, 4], "B": [1, 3, 5, 5]}))
expected = DataFrame({"A": [0, 2, 4, 4], "B": [1, 3, 5, 5]})
df = df_orig.copy()
df.loc[3] = df.loc[2]
tm.assert_frame_equal(df, expected)

# single dtype frame, overwrite
expected = DataFrame(dict({"A": [0, 2, 4], "B": [0, 2, 4]}))
expected = DataFrame({"A": [0, 2, 4], "B": [0, 2, 4]})
df = df_orig.copy()
df.loc[:, "B"] = df.loc[:, "A"]
tm.assert_frame_equal(df, expected)

# mixed dtype frame, overwrite
expected = DataFrame(dict({"A": [0, 2, 4], "B": Series([0.0, 2.0, 4.0])}))
expected = DataFrame({"A": [0, 2, 4], "B": Series([0.0, 2.0, 4.0])})
df = df_orig.copy()
df["B"] = df["B"].astype(np.float64)
# as of 2.0, df.loc[:, "B"] = ... attempts (and here succeeds) at
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/style/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ def html_lines(foot_prefix: str):

def test_to_html_na_rep_non_scalar_data(datapath):
# GH47103
df = DataFrame([dict(a=1, b=[1, 2, 3], c=np.nan)])
df = DataFrame([{"a": 1, "b": [1, 2, 3], "c": np.nan}])
result = df.style.format(na_rep="-").to_html(table_uuid="test")
expected = """\
<style type="text/css">
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/style/test_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def test_pass_colormap_instance(df, plot_method):
# https://github.com/pandas-dev/pandas/issues/49374
cmap = mpl.colors.ListedColormap([[1, 1, 1], [0, 0, 0]])
df["c"] = df.A + df.B
kwargs = dict(x="A", y="B", c="c", colormap=cmap)
kwargs = {"x": "A", "y": "B", "c": "c", "colormap": cmap}
if plot_method == "hexbin":
kwargs["C"] = kwargs.pop("c")
getattr(df.plot, plot_method)(**kwargs)
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ def test_to_html_na_rep_and_float_format(na_rep, datapath):

def test_to_html_na_rep_non_scalar_data(datapath):
# GH47103
df = DataFrame([dict(a=1, b=[1, 2, 3])])
df = DataFrame([{"a": 1, "b": [1, 2, 3]}])
result = df.to_html(na_rep="-")
expected = expected_html(datapath, "gh47103_expected_output")
assert result == expected
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,7 @@ def test_date_parser_int_bug(all_parsers):
def test_nat_parse(all_parsers):
# see gh-3062
parser = all_parsers
df = DataFrame(
dict({"A": np.arange(10, dtype="float64"), "B": Timestamp("20010101")})
)
df = DataFrame({"A": np.arange(10, dtype="float64"), "B": Timestamp("20010101")})
df.iloc[3:6, :] = np.nan

with tm.ensure_clean("__nat_parse_.csv") as path:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/libs/test_hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_allocated_khash_memory():
snapshot = snapshot.filter_traces(
(tracemalloc.DomainFilter(True, ht.get_hashtable_trace_domain()),)
)
return sum(map(lambda x: x.size, snapshot.traces))
return sum(x.size for x in snapshot.traces)


@pytest.mark.parametrize(
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,14 +369,14 @@ def test_constructor_generator(self):

def test_constructor_map(self):
# GH8909
m = map(lambda x: x, range(10))
m = (x for x in range(10))

result = Series(m)
exp = Series(range(10))
tm.assert_series_equal(result, exp)

# same but with non-default index
m = map(lambda x: x, range(10))
m = (x for x in range(10))
result = Series(m, index=range(10, 20))
exp.index = range(10, 20)
tm.assert_series_equal(result, exp)
Expand Down Expand Up @@ -647,7 +647,7 @@ def test_constructor_default_index(self):
list(range(3)),
Categorical(["a", "b", "a"]),
(i for i in range(3)),
map(lambda x: x, range(3)),
(x for x in range(3)),
],
)
def test_constructor_index_mismatch(self, input):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/util/test_assert_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def test_assert_almost_equal_iterable_values_mismatch():
# same-length lists
(
np.array([subarr, None], dtype=object),
np.array([list([[None, "b"], ["c", "d"]]), None], dtype=object),
np.array([[[None, "b"], ["c", "d"]], None], dtype=object),
),
# dicts
(
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,13 @@ line-length = 88
update-check = false
target-version = "py310"
fix = true
unfixable = ["E711"]
unfixable = []

select = [
# pyflakes
"F",
# pycodestyle
"E",
"W",
"E", "W",
# flake8-2020
"YTT",
# flake8-bugbear
Expand All @@ -221,6 +220,8 @@ select = [
"ISC",
# type-checking imports
"TCH",
# comprehensions
"C4",
]

ignore = [
Expand Down