diff --git a/asv_bench/benchmarks/reshape.py b/asv_bench/benchmarks/reshape.py index 171e4feb290cf..9c043e8db9389 100644 --- a/asv_bench/benchmarks/reshape.py +++ b/asv_bench/benchmarks/reshape.py @@ -112,9 +112,7 @@ def setup(self, dtype): values = np.take(list(string.ascii_letters), indices) values = [pd.Categorical(v) for v in values.T] - self.df = DataFrame( - {i: cat for i, cat in enumerate(values)}, index, columns - ) + self.df = DataFrame(dict(enumerate(values)), index, columns) self.df2 = self.df.iloc[:-1] diff --git a/doc/source/conf.py b/doc/source/conf.py index af93a979789c1..24482d7c7461a 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -374,7 +374,7 @@ html_context = { - "redirects": {old: new for old, new in moved_api_pages}, + "redirects": dict(moved_api_pages), "header": header, } diff --git a/doc/sphinxext/announce.py b/doc/sphinxext/announce.py index 94cbc852a4a38..1c45c03f36d64 100755 --- a/doc/sphinxext/announce.py +++ b/doc/sphinxext/announce.py @@ -88,7 +88,7 @@ def get_authors(revision_range): pre.discard("Homu") # Append '+' to new authors. - authors = [s + " +" for s in cur - pre] + [s for s in cur & pre] + authors = [s + " +" for s in cur - pre] + list(cur & pre) authors.sort() return authors diff --git a/pandas/core/computation/expr.py b/pandas/core/computation/expr.py index 0578d9965df0c..5118b5d0478e8 100644 --- a/pandas/core/computation/expr.py +++ b/pandas/core/computation/expr.py @@ -382,7 +382,7 @@ class BaseExprVisitor(ast.NodeVisitor): unary_ops = UNARY_OPS_SYMS unary_op_nodes = "UAdd", "USub", "Invert", "Not" - unary_op_nodes_map = {k: v for k, v in zip(unary_ops, unary_op_nodes)} + unary_op_nodes_map = dict(zip(unary_ops, unary_op_nodes)) rewrite_map = { ast.Eq: ast.In, diff --git a/pandas/io/xml.py b/pandas/io/xml.py index c44972e9b1a4a..b5973257b8ee2 100644 --- a/pandas/io/xml.py +++ b/pandas/io/xml.py @@ -283,7 +283,7 @@ def _parse_nodes(self, elems: list[Any]) -> list[dict[str, str | None]]: dicts = [{k: d[k] if k in d.keys() else None for k in keys} for d in dicts] if self.names: - dicts = [{nm: v for nm, v in zip(self.names, d.values())} for d in dicts] + dicts = [dict(zip(self.names, d.values())) for d in dicts] return dicts @@ -380,7 +380,7 @@ def _iterparse_nodes(self, iterparse: Callable) -> list[dict[str, str | None]]: dicts = [{k: d[k] if k in d.keys() else None for k in keys} for d in dicts] if self.names: - dicts = [{nm: v for nm, v in zip(self.names, d.values())} for d in dicts] + dicts = [dict(zip(self.names, d.values())) for d in dicts] return dicts diff --git a/pandas/tests/frame/constructors/test_from_records.py b/pandas/tests/frame/constructors/test_from_records.py index 7c4ed68dfd0ef..d4427dd789b16 100644 --- a/pandas/tests/frame/constructors/test_from_records.py +++ b/pandas/tests/frame/constructors/test_from_records.py @@ -151,7 +151,7 @@ def test_from_records_dictlike(self): for b in blocks.values(): columns.extend(b.columns) - asdict = {x: y for x, y in df.items()} + asdict = dict(df.items()) asdict2 = {x: y.values for x, y in df.items()} # dict of series & dict of ndarrays (have dtype info) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 2952db7febea1..033884b9ac57e 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -344,7 +344,7 @@ def test_constructor_mixed_dtypes(self, typ, ad): for d, a in zip(dtypes, arrays): assert a.dtype == d - ad.update({d: a for d, a in zip(dtypes, arrays)}) + ad.update(dict(zip(dtypes, arrays))) df = DataFrame(ad) dtypes = MIXED_FLOAT_DTYPES + MIXED_INT_DTYPES diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index fa51a291bf2f7..eb61f8defeaf8 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -930,7 +930,7 @@ def test_apply_index_has_complex_internals(index): (lambda x: set(x.index.to_list()), [{0, 1}, {2, 3}]), (lambda x: tuple(x.index.to_list()), [(0, 1), (2, 3)]), ( - lambda x: {n: i for (n, i) in enumerate(x.index.to_list())}, + lambda x: dict(enumerate(x.index.to_list())), [{0: 0, 1: 1}, {0: 2, 1: 3}], ), ( diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 9659b4aa5f45c..c947ff03fd09d 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -946,6 +946,9 @@ def test_multi_iter_frame(self, three_group): df["k1"] = np.array(["b", "b", "b", "a", "a", "a"]) df["k2"] = np.array(["1", "1", "1", "2", "2", "2"]) grouped = df.groupby(["k1", "k2"]) + # 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} assert len(groups) == 2 diff --git a/pandas/tests/indexes/categorical/test_map.py b/pandas/tests/indexes/categorical/test_map.py index 71ee82981721d..261ee8daf5dec 100644 --- a/pandas/tests/indexes/categorical/test_map.py +++ b/pandas/tests/indexes/categorical/test_map.py @@ -109,7 +109,7 @@ def test_map_with_dict_or_series(self): # Order of categories in result can be different tm.assert_index_equal(result, expected) - mapper = {o: n for o, n in zip(orig_values[:-1], new_values[:-1])} + mapper = dict(zip(orig_values[:-1], new_values[:-1])) result = cur_index.map(mapper) # Order of categories in result can be different tm.assert_index_equal(result, expected) diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index f89a06636a0c0..f321ecc2f65ff 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -152,7 +152,7 @@ def create_and_load_iris(conn, iris_file: Path, dialect: str): with iris_file.open(newline=None) as csvfile: reader = csv.reader(csvfile) header = next(reader) - params = [{key: value for key, value in zip(header, row)} for row in reader] + params = [dict(zip(header, row)) for row in reader] stmt = insert(iris).values(params) if isinstance(conn, Engine): with conn.connect() as conn: diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 1f00665efe579..59afe22e40f7a 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -661,7 +661,7 @@ def test_replace_different_int_types(self, any_int_numpy_dtype): labs = pd.Series([1, 1, 1, 0, 0, 2, 2, 2], dtype=any_int_numpy_dtype) maps = pd.Series([0, 2, 1], dtype=any_int_numpy_dtype) - map_dict = {old: new for (old, new) in zip(maps.values, maps.index)} + map_dict = dict(zip(maps.values, maps.index)) result = labs.replace(map_dict) expected = labs.replace({0: 0, 2: 1, 1: 2}) diff --git a/pyproject.toml b/pyproject.toml index b436b29c03c84..dd76ef83cb1b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -120,7 +120,6 @@ disable = [ "too-many-public-methods", "too-many-return-statements", "too-many-statements", - "unnecessary-comprehension", "unnecessary-list-index-lookup", "useless-option-value",