From efd5e0352354c8dacacc265087b4f1501de7483a Mon Sep 17 00:00:00 2001 From: VirosaLi <2EkF8qUgpNkj> Date: Wed, 25 Nov 2020 21:28:19 -0600 Subject: [PATCH 1/5] CLN: fix flake8 C406, C409, and some of C408 --- asv_bench/benchmarks/groupby.py | 2 +- pandas/conftest.py | 4 +- pandas/core/indexers.py | 2 +- pandas/core/indexes/base.py | 2 +- pandas/core/indexing.py | 4 +- pandas/core/internals/managers.py | 2 +- pandas/core/series.py | 2 +- pandas/io/stata.py | 72 ++++++++--------- pandas/tests/arithmetic/test_datetime64.py | 4 +- pandas/tests/base/test_conversion.py | 2 +- pandas/tests/frame/apply/test_frame_apply.py | 52 +++++------- pandas/tests/frame/indexing/test_indexing.py | 23 +++++- pandas/tests/frame/methods/test_dtypes.py | 18 ++--- .../tests/frame/methods/test_select_dtypes.py | 26 +++--- pandas/tests/frame/test_constructors.py | 16 ++-- .../tests/groupby/aggregate/test_aggregate.py | 4 +- pandas/tests/groupby/test_categorical.py | 54 ++++++------- .../tests/indexes/base_class/test_setops.py | 4 +- pandas/tests/indexes/datetimes/test_ops.py | 2 +- pandas/tests/indexes/multi/test_sorting.py | 10 +-- .../indexes/period/test_partial_slicing.py | 2 +- .../tests/indexes/ranges/test_constructors.py | 14 ++-- pandas/tests/indexing/common.py | 2 +- .../tests/indexing/multiindex/test_slice.py | 79 +++++++++++++------ .../indexing/test_chaining_and_caching.py | 18 ++--- pandas/tests/indexing/test_iloc.py | 15 ++-- pandas/tests/indexing/test_indexing.py | 14 ++-- pandas/tests/indexing/test_loc.py | 19 +++-- pandas/tests/indexing/test_partial.py | 6 +- pandas/tests/io/conftest.py | 2 +- pandas/tests/io/test_clipboard.py | 2 +- pandas/tests/io/test_feather.py | 2 +- pandas/tests/io/test_sql.py | 14 ++-- pandas/tests/resample/test_period_index.py | 2 +- pandas/tests/resample/test_resample_api.py | 4 +- pandas/tests/reshape/concat/test_concat.py | 8 +- pandas/tests/reshape/merge/test_merge.py | 4 +- pandas/tests/reshape/test_get_dummies.py | 2 +- .../tests/scalar/timestamp/test_timestamp.py | 33 ++++---- pandas/tests/series/methods/test_replace.py | 2 +- pandas/tests/series/methods/test_to_csv.py | 2 +- pandas/tests/series/methods/test_to_frame.py | 4 +- pandas/tests/series/test_reductions.py | 2 +- pandas/tests/series/test_validate.py | 2 +- pandas/tests/test_sorting.py | 16 +++- pandas/tests/tslibs/test_array_to_datetime.py | 4 +- pandas/tests/tslibs/test_parsing.py | 4 +- pandas/tests/util/test_assert_almost_equal.py | 2 +- pandas/tests/util/test_hashing.py | 17 +++- pandas/tests/window/test_api.py | 2 +- setup.cfg | 2 - 51 files changed, 336 insertions(+), 270 deletions(-) diff --git a/asv_bench/benchmarks/groupby.py b/asv_bench/benchmarks/groupby.py index 22f002e6cb79a..6ce63ff8badca 100644 --- a/asv_bench/benchmarks/groupby.py +++ b/asv_bench/benchmarks/groupby.py @@ -486,7 +486,7 @@ def setup(self): tmp2 = (np.random.random(10000) * 10.0).astype(np.float32) tmp = np.concatenate((tmp1, tmp2)) arr = np.repeat(tmp, 10) - self.df = DataFrame(dict(a=arr, b=arr)) + self.df = DataFrame({"a": arr, "b": arr}) def time_sum(self): self.df.groupby(["a"])["b"].sum() diff --git a/pandas/conftest.py b/pandas/conftest.py index 12682a68fe177..a0ec6f96042fc 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -472,8 +472,8 @@ def index_with_missing(request): if request.param in ["tuples", "mi-with-dt64tz-level", "multi"]: # For setting missing values in the top level of MultiIndex vals = ind.tolist() - vals[0] = tuple([None]) + vals[0][1:] - vals[-1] = tuple([None]) + vals[-1][1:] + vals[0] = (None,) + vals[0][1:] + vals[-1] = (None,) + vals[-1][1:] return MultiIndex.from_tuples(vals) else: vals[0] = None diff --git a/pandas/core/indexers.py b/pandas/core/indexers.py index e48a42599a2a0..b6713bc760c5e 100644 --- a/pandas/core/indexers.py +++ b/pandas/core/indexers.py @@ -105,7 +105,7 @@ def is_empty_indexer(indexer, arr_value: np.ndarray) -> bool: return True if arr_value.ndim == 1: if not isinstance(indexer, tuple): - indexer = tuple([indexer]) + indexer = (indexer,) return any(isinstance(idx, np.ndarray) and len(idx) == 0 for idx in indexer) return False diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index b5900ead246f3..32792ad1796f8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3558,7 +3558,7 @@ def _reindex_non_unique(self, target): cur_labels = self.take(indexer[check]).values cur_indexer = ensure_int64(length[check]) - new_labels = np.empty(tuple([len(indexer)]), dtype=object) + new_labels = np.empty((len(indexer),), dtype=object) new_labels[cur_indexer] = cur_labels new_labels[missing_indexer] = missing_labels diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index f6d14a1c1503c..4874aab04738b 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1934,7 +1934,7 @@ def _align_series(self, indexer, ser: "Series", multiindex_indexer: bool = False to the locations selected by `indexer` """ if isinstance(indexer, (slice, np.ndarray, list, Index)): - indexer = tuple([indexer]) + indexer = (indexer,) if isinstance(indexer, tuple): @@ -2073,7 +2073,7 @@ def __getitem__(self, key): # we could have a convertible item here (e.g. Timestamp) if not is_list_like_indexer(key): - key = tuple([key]) + key = (key,) else: raise ValueError("Invalid call for scalar access (getting)!") diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 760765e3a20e6..57a84ed2bc3a4 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1544,7 +1544,7 @@ def __init__( ) self.axes = [axis] - self.blocks = tuple([block]) + self.blocks = (block,) @classmethod def from_blocks( diff --git a/pandas/core/series.py b/pandas/core/series.py index 1e4c0e07de403..d493ac0a8c051 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -368,7 +368,7 @@ def _init_dict(self, data, index=None, dtype=None): values = na_value_for_dtype(dtype) keys = index else: - keys, values = tuple([]), [] + keys, values = tuple(), [] # Input is now list-like, so rely on "standard" construction: diff --git a/pandas/io/stata.py b/pandas/io/stata.py index e8b61c3c40291..d97ba6183c955 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -873,30 +873,26 @@ def __init__(self): (255, np.dtype(np.float64)), ] ) - self.DTYPE_MAP_XML = dict( - [ - (32768, np.dtype(np.uint8)), # Keys to GSO - (65526, np.dtype(np.float64)), - (65527, np.dtype(np.float32)), - (65528, np.dtype(np.int32)), - (65529, np.dtype(np.int16)), - (65530, np.dtype(np.int8)), - ] - ) + self.DTYPE_MAP_XML = { + 32768: np.dtype(np.uint8), # Keys to GSO + 65526: np.dtype(np.float64), + 65527: np.dtype(np.float32), + 65528: np.dtype(np.int32), + 65529: np.dtype(np.int16), + 65530: np.dtype(np.int8), + } # error: Argument 1 to "list" has incompatible type "str"; # expected "Iterable[int]" [arg-type] self.TYPE_MAP = list(range(251)) + list("bhlfd") # type: ignore[arg-type] - self.TYPE_MAP_XML = dict( - [ - # Not really a Q, unclear how to handle byteswap - (32768, "Q"), - (65526, "d"), - (65527, "f"), - (65528, "l"), - (65529, "h"), - (65530, "b"), - ] - ) + self.TYPE_MAP_XML = { + # Not really a Q, unclear how to handle byteswap + 32768: "Q", + 65526: "d", + 65527: "f", + 65528: "l", + 65529: "h", + 65530: "b", + } # NOTE: technically, some of these are wrong. there are more numbers # that can be represented. it's the 27 ABOVE and BELOW the max listed # numeric data type in [U] 12.2.2 of the 11.2 manual @@ -3138,24 +3134,22 @@ def _write_map(self) -> None: all blocks have been written. """ if not self._map: - self._map = dict( - ( - ("stata_data", 0), - ("map", self.handles.handle.tell()), - ("variable_types", 0), - ("varnames", 0), - ("sortlist", 0), - ("formats", 0), - ("value_label_names", 0), - ("variable_labels", 0), - ("characteristics", 0), - ("data", 0), - ("strls", 0), - ("value_labels", 0), - ("stata_data_close", 0), - ("end-of-file", 0), - ) - ) + self._map = { + "stata_data": 0, + "map": self.handles.handle.tell(), + "variable_types": 0, + "varnames": 0, + "sortlist": 0, + "formats": 0, + "value_label_names": 0, + "variable_labels": 0, + "characteristics": 0, + "data": 0, + "strls": 0, + "value_labels": 0, + "stata_data_close": 0, + "end-of-file": 0, + } # Move to start of map self.handles.handle.seek(self._map["map"]) bio = BytesIO() diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 35ffb0a246e25..a3d30cf0bc3c6 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1288,8 +1288,8 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array): ("seconds", 2), ("microseconds", 5), ] - for i, kwd in enumerate(relative_kwargs): - off = DateOffset(**dict([kwd])) + for i, (unit, value) in enumerate(relative_kwargs): + off = DateOffset(**{unit: value}) expected = DatetimeIndex([x + off for x in vec_items]) expected = tm.box_expected(expected, box_with_array) diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index 63280f5ccf8cd..a6fdb82e48197 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -109,7 +109,7 @@ def test_iterable_map(self, index_or_series, dtype, rdtype): s = typ([1], dtype=dtype) result = s.map(type)[0] if not isinstance(rdtype, tuple): - rdtype = tuple([rdtype]) + rdtype = (rdtype,) assert result in rdtype @pytest.mark.parametrize( diff --git a/pandas/tests/frame/apply/test_frame_apply.py b/pandas/tests/frame/apply/test_frame_apply.py index f080d4b8bcc36..15952f36b0fae 100644 --- a/pandas/tests/frame/apply/test_frame_apply.py +++ b/pandas/tests/frame/apply/test_frame_apply.py @@ -562,11 +562,9 @@ def test_apply_dict(self): # GH 8735 A = DataFrame([["foo", "bar"], ["spam", "eggs"]]) - A_dicts = Series( - [dict([(0, "foo"), (1, "spam")]), dict([(0, "bar"), (1, "eggs")])] - ) + A_dicts = Series([{0: "foo", 1: "spam"}, {0: "bar", 1: "eggs"}]) B = DataFrame([[0, 1], [2, 3]]) - B_dicts = Series([dict([(0, 0), (1, 2)]), dict([(0, 1), (1, 3)])]) + B_dicts = Series([{0: 0, 1: 2}, {0: 1, 1: 3}]) fn = lambda x: x.to_dict() for df, dicts in [(A, A_dicts), (B, B_dicts)]: @@ -1221,7 +1219,7 @@ def test_agg_reduce(self, axis, float_frame): tm.assert_frame_equal(result, expected) # dict input with scalars - func = dict([(name1, "mean"), (name2, "sum")]) + func = {name1: "mean", name2: "sum"} result = float_frame.agg(func, axis=axis) expected = Series( [ @@ -1233,7 +1231,7 @@ def test_agg_reduce(self, axis, float_frame): tm.assert_series_equal(result, expected) # dict input with lists - func = dict([(name1, ["mean"]), (name2, ["sum"])]) + func = {name1: ["mean"], name2: ["sum"]} result = float_frame.agg(func, axis=axis) expected = DataFrame( { @@ -1249,33 +1247,25 @@ def test_agg_reduce(self, axis, float_frame): tm.assert_frame_equal(result, expected) # dict input with lists with multiple - func = dict([(name1, ["mean", "sum"]), (name2, ["sum", "max"])]) + func = {name1: ["mean", "sum"], name2: ["sum", "max"]} result = float_frame.agg(func, axis=axis) expected = pd.concat( - dict( - [ - ( - name1, - Series( - [ - float_frame.loc(other_axis)[name1].mean(), - float_frame.loc(other_axis)[name1].sum(), - ], - index=["mean", "sum"], - ), - ), - ( - name2, - Series( - [ - float_frame.loc(other_axis)[name2].sum(), - float_frame.loc(other_axis)[name2].max(), - ], - index=["sum", "max"], - ), - ), - ] - ), + { + name1: Series( + [ + float_frame.loc(other_axis)[name1].mean(), + float_frame.loc(other_axis)[name1].sum(), + ], + index=["mean", "sum"], + ), + name2: Series( + [ + float_frame.loc(other_axis)[name2].sum(), + float_frame.loc(other_axis)[name2].max(), + ], + index=["sum", "max"], + ), + }, axis=1, ) expected = expected.T if axis in {1, "columns"} else expected diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index ff9646d45c0ac..e33009f4597f0 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1503,10 +1503,29 @@ def test_loc_getitem_index_namedtuple(self): result = df.loc[IndexType("foo", "bar")]["A"] assert result == 1 - @pytest.mark.parametrize("tpl", [tuple([1]), tuple([1, 2])]) + @pytest.mark.parametrize( + "tpl", + [ + (1,), + ( + 1, + 2, + ), + ], + ) def test_loc_getitem_index_single_double_tuples(self, tpl): # GH 20991 - idx = Index([tuple([1]), tuple([1, 2])], name="A", tupleize_cols=False) + idx = Index( + [ + (1,), + ( + 1, + 2, + ), + ], + name="A", + tupleize_cols=False, + ) df = DataFrame(index=idx) result = df.loc[[tpl]] diff --git a/pandas/tests/frame/methods/test_dtypes.py b/pandas/tests/frame/methods/test_dtypes.py index 0105eef435121..840e23604939a 100644 --- a/pandas/tests/frame/methods/test_dtypes.py +++ b/pandas/tests/frame/methods/test_dtypes.py @@ -32,8 +32,8 @@ def test_empty_frame_dtypes(self): norows_int_df.dtypes, Series(np.dtype("int32"), index=list("abc")) ) - df = DataFrame(dict([("a", 1), ("b", True), ("c", 1.0)]), index=[1, 2, 3]) - ex_dtypes = Series(dict([("a", np.int64), ("b", np.bool_), ("c", np.float64)])) + df = DataFrame({"a": 1, "b": True, "c": 1.0}, index=[1, 2, 3]) + ex_dtypes = Series({"a": np.int64, "b": np.bool_, "c": np.float64}) tm.assert_series_equal(df.dtypes, ex_dtypes) # same but for empty slice of df @@ -66,12 +66,12 @@ def test_dtypes_are_correct_after_column_slice(self): df = DataFrame(index=range(5), columns=list("abc"), dtype=np.float_) tm.assert_series_equal( df.dtypes, - Series(dict([("a", np.float_), ("b", np.float_), ("c", np.float_)])), + Series({"a": np.float_, "b": np.float_, "c": np.float_}), ) - tm.assert_series_equal(df.iloc[:, 2:].dtypes, Series(dict([("c", np.float_)]))) + tm.assert_series_equal(df.iloc[:, 2:].dtypes, Series({"c": np.float_})) tm.assert_series_equal( df.dtypes, - Series(dict([("a", np.float_), ("b", np.float_), ("c", np.float_)])), + Series({"a": np.float_, "b": np.float_, "c": np.float_}), ) def test_dtypes_gh8722(self, float_string_frame): @@ -90,10 +90,10 @@ def test_dtypes_gh8722(self, float_string_frame): def test_dtypes_timedeltas(self): df = DataFrame( - dict( - A=Series(date_range("2012-1-1", periods=3, freq="D")), - B=Series([timedelta(days=i) for i in range(3)]), - ) + { + "A": Series(date_range("2012-1-1", periods=3, freq="D")), + "B": Series([timedelta(days=i) for i in range(3)]), + } ) result = df.dtypes expected = Series( diff --git a/pandas/tests/frame/methods/test_select_dtypes.py b/pandas/tests/frame/methods/test_select_dtypes.py index 4599761909c33..f56debc1e4113 100644 --- a/pandas/tests/frame/methods/test_select_dtypes.py +++ b/pandas/tests/frame/methods/test_select_dtypes.py @@ -201,16 +201,14 @@ def test_select_dtypes_include_exclude_mixed_scalars_lists(self): def test_select_dtypes_duplicate_columns(self): # GH20839 df = DataFrame( - dict( - [ - ("a", list("abc")), - ("b", list(range(1, 4))), - ("c", np.arange(3, 6).astype("u1")), - ("d", np.arange(4.0, 7.0, dtype="float64")), - ("e", [True, False, True]), - ("f", pd.date_range("now", periods=3).values), - ] - ) + { + "a": ["abc"], + "b": list(range(1, 4)), + "c": np.arange(3, 6).astype("u1"), + "d": np.arange(4.0, 7.0, dtype="float64"), + "e": [True, False, True], + "f": pd.date_range("now", periods=3).values, + } ) df.columns = ["a", "a", "b", "b", "b", "c"] @@ -268,10 +266,10 @@ def test_select_dtypes_bad_datetime64(self): def test_select_dtypes_datetime_with_tz(self): df2 = DataFrame( - dict( - A=Timestamp("20130102", tz="US/Eastern"), - B=Timestamp("20130603", tz="CET"), - ), + { + "A": Timestamp("20130102", tz="US/Eastern"), + "B": Timestamp("20130603", tz="CET"), + }, index=range(5), ) df3 = pd.concat([df2.A.to_frame(), df2.B.to_frame()], axis=1) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index d32ca454b5fb2..e279c5872da03 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -181,7 +181,7 @@ def _make_mixed_dtypes_df(typ, ad=None): for d, a in zip(dtypes, arrays): assert a.dtype == d if ad is None: - ad = dict() + ad = {} ad.update({d: a for d, a in zip(dtypes, arrays)}) return DataFrame(ad) @@ -197,7 +197,7 @@ def _check_mixed_dtypes(df, dtypes=None): _check_mixed_dtypes(df) # add lots of types - df = _make_mixed_dtypes_df("float", dict(A=1, B="foo", C="bar")) + df = _make_mixed_dtypes_df("float", {"A": 1, "B": "foo", "C": "bar"}) _check_mixed_dtypes(df) # GH 622 @@ -356,8 +356,8 @@ def test_constructor_dict(self): # GH 14381 # Dict with None value - frame_none = DataFrame(dict(a=None), index=[0]) - frame_none_list = DataFrame(dict(a=[None]), index=[0]) + frame_none = DataFrame({"a": None}, index=[0]) + frame_none_list = DataFrame({"a": [None]}, index=[0]) assert frame_none._get_value(0, "a") is None assert frame_none_list._get_value(0, "a") is None tm.assert_frame_equal(frame_none, frame_none_list) @@ -1540,14 +1540,12 @@ def test_from_dict_columns_parameter(self): msg = "cannot use columns parameter with orient='columns'" with pytest.raises(ValueError, match=msg): DataFrame.from_dict( - dict([("A", [1, 2]), ("B", [4, 5])]), + {"A": [1, 2], "B": [4, 5]}, orient="columns", columns=["one", "two"], ) with pytest.raises(ValueError, match=msg): - DataFrame.from_dict( - dict([("A", [1, 2]), ("B", [4, 5])]), columns=["one", "two"] - ) + DataFrame.from_dict({"A": [1, 2], "B": [4, 5]}, columns=["one", "two"]) @pytest.mark.parametrize( "data_dict, keys, orient", @@ -1590,7 +1588,7 @@ def test_constructor_Series_named(self): arr = np.random.randn(10) s = Series(arr, name="x") df = DataFrame(s) - expected = DataFrame(dict(x=s)) + expected = DataFrame({"x": s}) tm.assert_frame_equal(df, expected) s = Series(arr, index=range(3, 13)) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index dba039b66d22d..451e063dd0f95 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -348,7 +348,7 @@ def bar(x): # this uses column selection & renaming msg = r"nested renamer is not supported" with pytest.raises(SpecificationError, match=msg): - d = dict([["C", np.mean], ["D", dict([["foo", np.mean], ["bar", np.std]])]]) + d = {"C": np.mean, "D": {"foo": np.mean, "bar": np.std}} grouped.aggregate(d) # But without renaming, these functions are OK @@ -1047,7 +1047,7 @@ def test_groupby_get_by_index(): # GH 33439 df = DataFrame({"A": ["S", "W", "W"], "B": [1.0, 1.0, 2.0]}) res = df.groupby("A").agg({"B": lambda x: x.get(x.index[-1])}) - expected = DataFrame(dict(A=["S", "W"], B=[1.0, 2.0])).set_index("A") + expected = DataFrame({"A": ["S", "W"], "B": [1.0, 2.0]}).set_index("A") pd.testing.assert_frame_equal(res, expected) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 8271d0c45313d..8cf77ca6335f4 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -33,39 +33,37 @@ def f(a): return result.reindex(index, fill_value=fill_value).sort_index() -_results_for_groupbys_with_missing_categories = dict( +_results_for_groupbys_with_missing_categories = { # This maps the builtin groupby functions to their expected outputs for # missing categories when they are called on a categorical grouper with # observed=False. Some functions are expected to return NaN, some zero. # These expected values can be used across several tests (i.e. they are # the same for SeriesGroupBy and DataFrameGroupBy) but they should only be # hardcoded in one place. - [ - ("all", np.NaN), - ("any", np.NaN), - ("count", 0), - ("corrwith", np.NaN), - ("first", np.NaN), - ("idxmax", np.NaN), - ("idxmin", np.NaN), - ("last", np.NaN), - ("mad", np.NaN), - ("max", np.NaN), - ("mean", np.NaN), - ("median", np.NaN), - ("min", np.NaN), - ("nth", np.NaN), - ("nunique", 0), - ("prod", np.NaN), - ("quantile", np.NaN), - ("sem", np.NaN), - ("size", 0), - ("skew", np.NaN), - ("std", np.NaN), - ("sum", 0), - ("var", np.NaN), - ] -) + "all": np.NaN, + "any": np.NaN, + "count": 0, + "corrwith": np.NaN, + "first": np.NaN, + "idxmax": np.NaN, + "idxmin": np.NaN, + "last": np.NaN, + "mad": np.NaN, + "max": np.NaN, + "mean": np.NaN, + "median": np.NaN, + "min": np.NaN, + "nth": np.NaN, + "nunique": 0, + "prod": np.NaN, + "quantile": np.NaN, + "sem": np.NaN, + "size": 0, + "skew": np.NaN, + "std": np.NaN, + "sum": 0, + "var": np.NaN, +} def test_apply_use_categorical_name(df): @@ -1151,7 +1149,7 @@ def df_cat(df): @pytest.mark.parametrize( - "operation, kwargs", [("agg", dict(dtype="category")), ("apply", dict())] + "operation, kwargs", [("agg", {"dtype": "category"}), ("apply", {})] ) def test_seriesgroupby_observed_true(df_cat, operation, kwargs): # GH 24880 diff --git a/pandas/tests/indexes/base_class/test_setops.py b/pandas/tests/indexes/base_class/test_setops.py index 9a6a892307da8..4d4940d3ab12c 100644 --- a/pandas/tests/indexes/base_class/test_setops.py +++ b/pandas/tests/indexes/base_class/test_setops.py @@ -223,8 +223,8 @@ def test_tuple_union_bug(self, method, expected, sort): expected = Index(expected) tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("first_list", [list("ba"), list()]) - @pytest.mark.parametrize("second_list", [list("ab"), list()]) + @pytest.mark.parametrize("first_list", [["ba"], []]) + @pytest.mark.parametrize("second_list", [["ab"], []]) @pytest.mark.parametrize( "first_name, second_name, expected_name", [("A", "B", None), (None, "B", None), ("A", None, None)], diff --git a/pandas/tests/indexes/datetimes/test_ops.py b/pandas/tests/indexes/datetimes/test_ops.py index cbbe3aca9ccbe..17c072556313d 100644 --- a/pandas/tests/indexes/datetimes/test_ops.py +++ b/pandas/tests/indexes/datetimes/test_ops.py @@ -34,7 +34,7 @@ def test_ops_properties_basic(self, datetime_series): getattr(datetime_series, op) # attribute access should still work! - s = Series(dict(year=2000, month=1, day=10)) + s = Series({"year": 2000, "month": 1, "day": 10}) assert s.year == 2000 assert s.month == 1 assert s.day == 10 diff --git a/pandas/tests/indexes/multi/test_sorting.py b/pandas/tests/indexes/multi/test_sorting.py index cd063a0c3f74b..e5d178581136b 100644 --- a/pandas/tests/indexes/multi/test_sorting.py +++ b/pandas/tests/indexes/multi/test_sorting.py @@ -235,11 +235,11 @@ def test_remove_unused_levels_large(first_type, second_type): size = 1 << 16 df = DataFrame( - dict( - first=rng.randint(0, 1 << 13, size).astype(first_type), - second=rng.randint(0, 1 << 10, size).astype(second_type), - third=rng.rand(size), - ) + { + "first": rng.randint(0, 1 << 13, size).astype(first_type), + "second": rng.randint(0, 1 << 10, size).astype(second_type), + "third": rng.rand(size), + } ) df = df.groupby(["first", "second"]).sum() df = df[df.third < 0.1] diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index 660c32d44a7aa..45e443053410a 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -78,7 +78,7 @@ def test_range_slice_outofbounds(self, make_range): # GH#5407 idx = make_range(start="2013/10/01", freq="D", periods=10) - df = DataFrame(dict(units=[100 + i for i in range(10)]), index=idx) + df = DataFrame({"units": [100 + i for i in range(10)]}, index=idx) empty = DataFrame(index=type(idx)([], freq="D"), columns=["units"]) empty["units"] = empty["units"].astype("int64") diff --git a/pandas/tests/indexes/ranges/test_constructors.py b/pandas/tests/indexes/ranges/test_constructors.py index f573da44e99b3..7dd893bd16720 100644 --- a/pandas/tests/indexes/ranges/test_constructors.py +++ b/pandas/tests/indexes/ranges/test_constructors.py @@ -12,13 +12,13 @@ class TestRangeIndexConstructors: @pytest.mark.parametrize( "args, kwargs, start, stop, step", [ - ((5,), dict(), 0, 5, 1), - ((1, 5), dict(), 1, 5, 1), - ((1, 5, 2), dict(), 1, 5, 2), - ((0,), dict(), 0, 0, 1), - ((0, 0), dict(), 0, 0, 1), - (tuple(), dict(start=0), 0, 0, 1), - (tuple(), dict(stop=0), 0, 0, 1), + ((5,), {}, 0, 5, 1), + ((1, 5), {}, 1, 5, 1), + ((1, 5, 2), {}, 1, 5, 2), + ((0,), {}, 0, 0, 1), + ((0, 0), {}, 0, 0, 1), + ((), {"start": 0}, 0, 0, 1), + ((), {"stop": 0}, 0, 0, 1), ], ) def test_constructor(self, args, kwargs, start, stop, step, name): diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index 656d25bec2a6b..fb6f4da2a482e 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -94,7 +94,7 @@ def setup_method(self, method): # form agglomerates for kind in self._kinds: - d = dict() + d = {} for typ in self._typs: d[typ] = getattr(self, f"{kind}_{typ}") diff --git a/pandas/tests/indexing/multiindex/test_slice.py b/pandas/tests/indexing/multiindex/test_slice.py index 024cc3ad72688..d58bc4713f99f 100644 --- a/pandas/tests/indexing/multiindex/test_slice.py +++ b/pandas/tests/indexing/multiindex/test_slice.py @@ -23,7 +23,12 @@ def test_per_axis_per_level_getitem(self): result = df.loc[(slice("A1", "A3"), slice(None), ["C1", "C3"]), :] expected = df.loc[ [ - tuple([a, b, c, d]) + ( + a, + b, + c, + d, + ) for a, b, c, d in df.index.values if (a == "A1" or a == "A2" or a == "A3") and (c == "C1" or c == "C3") ] @@ -32,7 +37,12 @@ def test_per_axis_per_level_getitem(self): expected = df.loc[ [ - tuple([a, b, c, d]) + ( + a, + b, + c, + d, + ) for a, b, c, d in df.index.values if (a == "A1" or a == "A2" or a == "A3") and (c == "C1" or c == "C2" or c == "C3") @@ -84,7 +94,7 @@ def test_per_axis_per_level_getitem(self): result = df.loc["A", "a"] expected = DataFrame( - dict(bar=[1, 5, 9], foo=[0, 4, 8]), + {"bar": [1, 5, 9], "foo": [0, 4, 8]}, index=Index([1, 2, 3], name="two"), columns=Index(["bar", "foo"], name="lvl1"), ) @@ -99,7 +109,12 @@ def test_per_axis_per_level_getitem(self): result = s.loc["A1":"A3", :, ["C1", "C3"]] expected = s.loc[ [ - tuple([a, b, c, d]) + ( + a, + b, + c, + d, + ) for a, b, c, d in s.index.values if (a == "A1" or a == "A2" or a == "A3") and (c == "C1" or c == "C3") ] @@ -150,19 +165,19 @@ def test_multiindex_slicers_non_unique(self): # non-unique mi index support df = ( DataFrame( - dict( - A=["foo", "foo", "foo", "foo"], - B=["a", "a", "a", "a"], - C=[1, 2, 1, 3], - D=[1, 2, 3, 4], - ) + { + "A": ["foo", "foo", "foo", "foo"], + "B": ["a", "a", "a", "a"], + "C": [1, 2, 1, 3], + "D": [1, 2, 3, 4], + } ) .set_index(["A", "B", "C"]) .sort_index() ) assert not df.index.is_unique expected = ( - DataFrame(dict(A=["foo", "foo"], B=["a", "a"], C=[1, 1], D=[1, 3])) + DataFrame({"A": ["foo", "foo"], "B": ["a", "a"], "C": [1, 1], "D": [1, 3]}) .set_index(["A", "B", "C"]) .sort_index() ) @@ -175,19 +190,19 @@ def test_multiindex_slicers_non_unique(self): df = ( DataFrame( - dict( - A=["foo", "foo", "foo", "foo"], - B=["a", "a", "a", "a"], - C=[1, 2, 1, 2], - D=[1, 2, 3, 4], - ) + { + "A": ["foo", "foo", "foo", "foo"], + "B": ["a", "a", "a", "a"], + "C": [1, 2, 1, 2], + "D": [1, 2, 3, 4], + } ) .set_index(["A", "B", "C"]) .sort_index() ) assert not df.index.is_unique expected = ( - DataFrame(dict(A=["foo", "foo"], B=["a", "a"], C=[1, 1], D=[1, 3])) + DataFrame({"A": ["foo", "foo"], "B": ["a", "a"], "C": [1, 1], "D": [1, 3]}) .set_index(["A", "B", "C"]) .sort_index() ) @@ -393,7 +408,12 @@ def test_per_axis_per_level_doc_examples(self): result = df.loc[(slice("A1", "A3"), slice(None), ["C1", "C3"]), :] expected = df.loc[ [ - tuple([a, b, c, d]) + ( + a, + b, + c, + d, + ) for a, b, c, d in df.index.values if (a == "A1" or a == "A2" or a == "A3") and (c == "C1" or c == "C3") ] @@ -405,7 +425,12 @@ def test_per_axis_per_level_doc_examples(self): result = df.loc[(slice(None), slice(None), ["C1", "C3"]), :] expected = df.loc[ [ - tuple([a, b, c, d]) + ( + a, + b, + c, + d, + ) for a, b, c, d in df.index.values if (c == "C1" or c == "C3") ] @@ -461,7 +486,12 @@ def test_loc_axis_arguments(self): result = df.loc(axis=0)["A1":"A3", :, ["C1", "C3"]] expected = df.loc[ [ - tuple([a, b, c, d]) + ( + a, + b, + c, + d, + ) for a, b, c, d in df.index.values if (a == "A1" or a == "A2" or a == "A3") and (c == "C1" or c == "C3") ] @@ -471,7 +501,12 @@ def test_loc_axis_arguments(self): result = df.loc(axis="index")[:, :, ["C1", "C3"]] expected = df.loc[ [ - tuple([a, b, c, d]) + ( + a, + b, + c, + d, + ) for a, b, c, d in df.index.values if (c == "C1" or c == "C3") ] diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index d162468235767..0e91600f06f2d 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -121,13 +121,13 @@ def test_setitem_chained_setfault(self): tm.assert_frame_equal(df, DataFrame({"response": mdata, "response1": data})) # GH 6056 - expected = DataFrame(dict(A=[np.nan, "bar", "bah", "foo", "bar"])) - df = DataFrame(dict(A=np.array(["foo", "bar", "bah", "foo", "bar"]))) + expected = DataFrame({"A": [np.nan, "bar", "bah", "foo", "bar"]}) + df = DataFrame({"A": np.array(["foo", "bar", "bah", "foo", "bar"])}) df["A"].iloc[0] = np.nan result = df.head() tm.assert_frame_equal(result, expected) - df = DataFrame(dict(A=np.array(["foo", "bar", "bah", "foo", "bar"]))) + df = DataFrame({"A": np.array(["foo", "bar", "bah", "foo", "bar"])}) df.A.iloc[0] = np.nan result = df.head() tm.assert_frame_equal(result, expected) @@ -299,12 +299,12 @@ def random_text(nobs=100): # Mixed type setting but same dtype & changing dtype df = DataFrame( - dict( - A=date_range("20130101", periods=5), - B=np.random.randn(5), - C=np.arange(5, dtype="int64"), - D=list("abcde"), - ) + { + "A": date_range("20130101", periods=5), + "B": np.random.randn(5), + "C": np.arange(5, dtype="int64"), + "D": ["abcde"], + } ) with pytest.raises(com.SettingWithCopyError, match=msg): diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 9ae9566ac87ef..971ceec1185fd 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -560,15 +560,15 @@ def test_iloc_setitem_list_of_lists(self): # GH 7551 # list-of-list is set incorrectly in mixed vs. single dtyped frames df = DataFrame( - dict(A=np.arange(5, dtype="int64"), B=np.arange(5, 10, dtype="int64")) + {"A": np.arange(5, dtype="int64"), "B": np.arange(5, 10, dtype="int64")} ) df.iloc[2:4] = [[10, 11], [12, 13]] - expected = DataFrame(dict(A=[0, 1, 10, 12, 4], B=[5, 6, 11, 13, 9])) + expected = DataFrame({"A": [0, 1, 10, 12, 4], "B": [5, 6, 11, 13, 9]}) tm.assert_frame_equal(df, expected) - df = DataFrame(dict(A=list("abcde"), B=np.arange(5, 10, dtype="int64"))) + df = DataFrame({"A": ["abcde"], "B": np.arange(5, 10, dtype="int64")}) df.iloc[2:4] = [["x", 11], ["y", 13]] - expected = DataFrame(dict(A=["a", "b", "x", "y", "e"], B=[5, 6, 11, 13, 9])) + expected = DataFrame({"A": ["a", "b", "x", "y", "e"], "B": [5, 6, 11, 13, 9]}) tm.assert_frame_equal(df, expected) @pytest.mark.parametrize("indexer", [[0], slice(None, 1, None), np.array([0])]) @@ -645,7 +645,10 @@ def test_iloc_mask(self): except (ValueError, IndexingError, NotImplementedError) as e: ans = str(e) - key = tuple([idx, method]) + key = ( + idx, + method, + ) r = expected.get(key) if r != ans: raise AssertionError( @@ -859,7 +862,7 @@ def test_iloc_setitem_pure_position_based(self, indexer): def test_iloc_setitem_dictionary_value(self): # GH#37728 df = DataFrame({"x": [1, 2], "y": [2, 2]}) - rhs = dict(x=9, y=99) + rhs = {"x": 9, "y": 99} df.iloc[1] = rhs expected = DataFrame({"x": [1, 9], "y": [2, 99]}) tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index b52c2ebbbc584..f750b3667cec2 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -463,12 +463,12 @@ def test_multi_assign(self): # broadcasting on the rhs is required df = DataFrame( - dict( - A=[1, 2, 0, 0, 0], - B=[0, 0, 0, 10, 11], - C=[0, 0, 0, 10, 11], - D=[3, 4, 5, 6, 7], - ) + { + "A": [1, 2, 0, 0, 0], + "B": [0, 0, 0, 10, 11], + "C": [0, 0, 0, 10, 11], + "D": [3, 4, 5, 6, 7], + } ) expected = df.copy() @@ -743,7 +743,7 @@ def test_slice_with_zero_step_raises(self): def test_indexing_assignment_dict_already_exists(self): df = DataFrame({"x": [1, 2, 6], "y": [2, 2, 8], "z": [-5, 0, 5]}).set_index("z") expected = df.copy() - rhs = dict(x=9, y=99) + rhs = {"x": 9, "y": 99} df.loc[5] = rhs expected.loc[5] = [9, 99] tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index ac3e944716486..f8ad16be607a5 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -183,17 +183,26 @@ def test_loc_setitem_dups(self): } ).set_index("me") - indexer = tuple(["r", ["bar", "bar2"]]) + indexer = ( + "r", + ["bar", "bar2"], + ) df = df_orig.copy() df.loc[indexer] *= 2.0 tm.assert_series_equal(df.loc[indexer], 2.0 * df_orig.loc[indexer]) - indexer = tuple(["r", "bar"]) + indexer = ( + "r", + "bar", + ) df = df_orig.copy() df.loc[indexer] *= 2.0 assert df.loc[indexer] == 2.0 * df_orig.loc[indexer] - indexer = tuple(["t", ["bar", "bar2"]]) + indexer = ( + "t", + ["bar", "bar2"], + ) df = df_orig.copy() df.loc[indexer] *= 2.0 tm.assert_frame_equal(df.loc[indexer], 2.0 * df_orig.loc[indexer]) @@ -563,7 +572,7 @@ def test_loc_setitem_frame(self): # setting issue df = DataFrame(index=[3, 5, 4], columns=["A"]) df.loc[[4, 3, 5], "A"] = np.array([1, 2, 3], dtype="int64") - expected = DataFrame(dict(A=Series([1, 2, 3], index=[4, 3, 5]))).reindex( + expected = DataFrame({"A": Series([1, 2, 3], index=[4, 3, 5])}).reindex( index=[3, 5, 4] ) tm.assert_frame_equal(df, expected) @@ -585,7 +594,7 @@ def test_loc_setitem_frame(self): df.loc[keys2, "B"] = val2 expected = DataFrame( - dict(A=Series(val1, index=keys1), B=Series(val2, index=keys2)) + {"A": Series(val1, index=keys1), "B": Series(val2, index=keys2)} ).reindex(index=index) tm.assert_frame_equal(df, expected) diff --git a/pandas/tests/indexing/test_partial.py b/pandas/tests/indexing/test_partial.py index 3bf37f4cade8b..a25922cfd2a0b 100644 --- a/pandas/tests/indexing/test_partial.py +++ b/pandas/tests/indexing/test_partial.py @@ -500,17 +500,17 @@ def test_partial_set_empty_frame_empty_consistencies(self): # consistency on empty frames df = DataFrame(columns=["x", "y"]) df["x"] = [1, 2] - expected = DataFrame(dict(x=[1, 2], y=[np.nan, np.nan])) + expected = DataFrame({"x": [1, 2], "y": [np.nan, np.nan]}) tm.assert_frame_equal(df, expected, check_dtype=False) df = DataFrame(columns=["x", "y"]) df["x"] = ["1", "2"] - expected = DataFrame(dict(x=["1", "2"], y=[np.nan, np.nan]), dtype=object) + expected = DataFrame({"x": ["1", "2"], "y": [np.nan, np.nan]}, dtype=object) tm.assert_frame_equal(df, expected) df = DataFrame(columns=["x", "y"]) df.loc[0, "x"] = 1 - expected = DataFrame(dict(x=[1], y=[np.nan])) + expected = DataFrame({"x": [1], "y": [np.nan]}) tm.assert_frame_equal(df, expected, check_dtype=False) @pytest.mark.parametrize( diff --git a/pandas/tests/io/conftest.py b/pandas/tests/io/conftest.py index 193baa8c3ed74..e9f228b5973b5 100644 --- a/pandas/tests/io/conftest.py +++ b/pandas/tests/io/conftest.py @@ -36,7 +36,7 @@ def feather_file(datapath): @pytest.fixture def s3so(worker_id): worker_id = "5" if worker_id == "master" else worker_id.lstrip("gw") - return dict(client_kwargs={"endpoint_url": f"http://127.0.0.1:555{worker_id}/"}) + return {"client_kwargs": {"endpoint_url": f"http://127.0.0.1:555{worker_id}/"}} @pytest.fixture(scope="session") diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index 569bc8a04862e..fca98175a0a24 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -199,7 +199,7 @@ def test_clipboard_copy_strings(self, sep, excel, df): def test_read_clipboard_infer_excel(self, request, mock_clipboard): # gh-19010: avoid warnings - clip_kwargs = dict(engine="python") + clip_kwargs = {"engine": "python"} text = dedent( """ diff --git a/pandas/tests/io/test_feather.py b/pandas/tests/io/test_feather.py index cef5d28b8ccf0..58ae5196151c1 100644 --- a/pandas/tests/io/test_feather.py +++ b/pandas/tests/io/test_feather.py @@ -173,7 +173,7 @@ def test_path_localpath(self): @td.skip_if_no("pyarrow", min_version="0.16.1.dev") def test_passthrough_keywords(self): df = tm.makeDataFrame().reset_index() - self.check_round_trip(df, write_kwargs=dict(version=1)) + self.check_round_trip(df, write_kwargs={"version": 1}) @td.skip_if_no("pyarrow") @tm.network diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index e3c2f20f80ee3..1be6022bc0fcd 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -346,13 +346,13 @@ def _load_test1_data(self): def _load_test2_data(self): df = DataFrame( - dict( - A=[4, 1, 3, 6], - B=["asd", "gsq", "ylt", "jkl"], - C=[1.1, 3.1, 6.9, 5.3], - D=[False, True, True, False], - E=["1990-11-22", "1991-10-26", "1993-11-26", "1995-12-12"], - ) + { + "A": [4, 1, 3, 6], + "B": ["asd", "gsq", "ylt", "jkl"], + "C": [1.1, 3.1, 6.9, 5.3], + "D": [False, True, True, False], + "E": ["1990-11-22", "1991-10-26", "1993-11-26", "1995-12-12"], + } ) df["E"] = to_datetime(df["E"]) diff --git a/pandas/tests/resample/test_period_index.py b/pandas/tests/resample/test_period_index.py index 79fc6bae1a9eb..e83196e9c7d56 100644 --- a/pandas/tests/resample/test_period_index.py +++ b/pandas/tests/resample/test_period_index.py @@ -72,7 +72,7 @@ def test_asfreq_fill_value(self, series): @pytest.mark.parametrize("freq", ["H", "12H", "2D", "W"]) @pytest.mark.parametrize("kind", [None, "period", "timestamp"]) - @pytest.mark.parametrize("kwargs", [dict(on="date"), dict(level="d")]) + @pytest.mark.parametrize("kwargs", [{"on": "date"}, {"level": "d"}]) def test_selection(self, index, freq, kind, kwargs): # This is a bug, these should be implemented # GH 14008 diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index 29f2aea1648ec..5588b185793cc 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -427,7 +427,7 @@ def test_agg_misc(): msg = r"Column\(s\) \['result1', 'result2'\] do not exist" for t in cases: with pytest.raises(pd.core.base.SpecificationError, match=msg): - t[["A", "B"]].agg(dict([("result1", np.sum), ("result2", np.mean)])) + t[["A", "B"]].agg({"result1": np.sum, "result2": np.mean}) # agg with different hows expected = pd.concat( @@ -437,7 +437,7 @@ def test_agg_misc(): [("A", "sum"), ("A", "std"), ("B", "mean"), ("B", "std")] ) for t in cases: - result = t.agg(dict([("A", ["sum", "std"]), ("B", ["mean", "std"])])) + result = t.agg({"A": ["sum", "std"], "B": ["mean", "std"]}) tm.assert_frame_equal(result, expected, check_like=True) # equivalent of using a selection list / or not diff --git a/pandas/tests/reshape/concat/test_concat.py b/pandas/tests/reshape/concat/test_concat.py index a1351ce782669..db93e831e8e0b 100644 --- a/pandas/tests/reshape/concat/test_concat.py +++ b/pandas/tests/reshape/concat/test_concat.py @@ -330,8 +330,8 @@ def test_concat_keys_with_none(self): # #1649 df0 = DataFrame([[10, 20, 30], [10, 20, 30], [10, 20, 30]]) - result = concat(dict(a=None, b=df0, c=df0[:2], d=df0[:1], e=df0)) - expected = concat(dict(b=df0, c=df0[:2], d=df0[:1], e=df0)) + result = concat({"a": None, "b": df0, "c": df0[:2], "d": df0[:1], "e": df0}) + expected = concat({"b": df0, "c": df0[:2], "d": df0[:1], "e": df0}) tm.assert_frame_equal(result, expected) result = concat( @@ -441,9 +441,7 @@ def test_concat_ordered_dict(self): expected = pd.concat( [Series(range(3)), Series(range(4))], keys=["First", "Another"] ) - result = pd.concat( - dict([("First", Series(range(3))), ("Another", Series(range(4)))]) - ) + result = pd.concat({"First": Series(range(3)), "Another": Series(range(4))}) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index 143bac3ad136a..f44909b61ff7a 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -1935,9 +1935,7 @@ def test_merge_index_types(index): result = left.merge(right, on=["index_col"]) - expected = DataFrame( - dict([("left_data", [1, 2]), ("right_data", [1.0, 2.0])]), index=index - ) + expected = DataFrame({"left_data": [1, 2], "right_data": [1.0, 2.0]}, index=index) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/test_get_dummies.py b/pandas/tests/reshape/test_get_dummies.py index 537bedfd1a6b9..a32adeb612e7c 100644 --- a/pandas/tests/reshape/test_get_dummies.py +++ b/pandas/tests/reshape/test_get_dummies.py @@ -567,7 +567,7 @@ def test_dataframe_dummies_preserve_categorical_dtype(self, dtype, ordered): @pytest.mark.parametrize("sparse", [True, False]) def test_get_dummies_dont_sparsify_all_columns(self, sparse): # GH18914 - df = DataFrame.from_dict(dict([("GDP", [1, 2]), ("Nation", ["AB", "CD"])])) + df = DataFrame.from_dict({"GDP": [1, 2], "Nation": ["AB", "CD"]}) df = get_dummies(df, columns=["Nation"], sparse=sparse) df2 = df.reindex(columns=["GDP"]) diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 92675f387fe1e..36d1b0911c909 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -309,24 +309,27 @@ def test_basics_nanos(self): "value, check_kwargs", [ [946688461000000000, {}], - [946688461000000000 / 1000, dict(unit="us")], - [946688461000000000 / 1_000_000, dict(unit="ms")], - [946688461000000000 / 1_000_000_000, dict(unit="s")], - [10957, dict(unit="D", h=0)], + [946688461000000000 / 1000, {"unit": "us"}], + [946688461000000000 / 1_000_000, {"unit": "ms"}], + [946688461000000000 / 1_000_000_000, {"unit": "s"}], + [10957, {"unit": "D", "h": 0}], [ (946688461000000000 + 500000) / 1000000000, - dict(unit="s", us=499, ns=964), + {"unit": "s", "us": 499, "ns": 964}, ], - [(946688461000000000 + 500000000) / 1000000000, dict(unit="s", us=500000)], - [(946688461000000000 + 500000) / 1000000, dict(unit="ms", us=500)], - [(946688461000000000 + 500000) / 1000, dict(unit="us", us=500)], - [(946688461000000000 + 500000000) / 1000000, dict(unit="ms", us=500000)], - [946688461000000000 / 1000.0 + 5, dict(unit="us", us=5)], - [946688461000000000 / 1000.0 + 5000, dict(unit="us", us=5000)], - [946688461000000000 / 1000000.0 + 0.5, dict(unit="ms", us=500)], - [946688461000000000 / 1000000.0 + 0.005, dict(unit="ms", us=5, ns=5)], - [946688461000000000 / 1000000000.0 + 0.5, dict(unit="s", us=500000)], - [10957 + 0.5, dict(unit="D", h=12)], + [ + (946688461000000000 + 500000000) / 1000000000, + {"unit": "s", "us": 500000}, + ], + [(946688461000000000 + 500000) / 1000000, {"unit": "ms", "us": 500}], + [(946688461000000000 + 500000) / 1000, {"unit": "us", "us": 500}], + [(946688461000000000 + 500000000) / 1000000, {"unit": "ms", "us": 500000}], + [946688461000000000 / 1000.0 + 5, {"unit": "us", "us": 5}], + [946688461000000000 / 1000.0 + 5000, {"unit": "us", "us": 5000}], + [946688461000000000 / 1000000.0 + 0.5, {"unit": "ms", "us": 500}], + [946688461000000000 / 1000000.0 + 0.005, {"unit": "ms", "us": 5, "ns": 5}], + [946688461000000000 / 1000000000.0 + 0.5, {"unit": "s", "us": 500000}], + [10957 + 0.5, {"unit": "D", "h": 12}], ], ) def test_unit(self, value, check_kwargs): diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 565debb98d8cc..faa7b872d9d06 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -253,7 +253,7 @@ def test_replace_with_dictlike_and_string_dtype(self): def test_replace_with_empty_dictlike(self): # GH 15289 s = pd.Series(list("abcd")) - tm.assert_series_equal(s, s.replace(dict())) + tm.assert_series_equal(s, s.replace({})) with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False): empty_series = pd.Series([]) diff --git a/pandas/tests/series/methods/test_to_csv.py b/pandas/tests/series/methods/test_to_csv.py index 72db87362584d..a22e125e68cba 100644 --- a/pandas/tests/series/methods/test_to_csv.py +++ b/pandas/tests/series/methods/test_to_csv.py @@ -13,7 +13,7 @@ class TestSeriesToCSV: def read_csv(self, path, **kwargs): - params = dict(squeeze=True, index_col=0, header=None, parse_dates=True) + params = {"squeeze": True, "index_col": 0, "header": None, "parse_dates": True} params.update(**kwargs) header = params.get("header") diff --git a/pandas/tests/series/methods/test_to_frame.py b/pandas/tests/series/methods/test_to_frame.py index b324fab5d97d4..6d52ab9da3f1b 100644 --- a/pandas/tests/series/methods/test_to_frame.py +++ b/pandas/tests/series/methods/test_to_frame.py @@ -12,13 +12,13 @@ def test_to_frame(self, datetime_series): datetime_series.name = "testname" rs = datetime_series.to_frame() xp = DataFrame( - dict(testname=datetime_series.values), index=datetime_series.index + {"testname": datetime_series.values}, index=datetime_series.index ) tm.assert_frame_equal(rs, xp) rs = datetime_series.to_frame(name="testdifferent") xp = DataFrame( - dict(testdifferent=datetime_series.values), index=datetime_series.index + {"testdifferent": datetime_series.values}, index=datetime_series.index ) tm.assert_frame_equal(rs, xp) diff --git a/pandas/tests/series/test_reductions.py b/pandas/tests/series/test_reductions.py index 0e8bf8f052206..c3c58f29fcbf6 100644 --- a/pandas/tests/series/test_reductions.py +++ b/pandas/tests/series/test_reductions.py @@ -66,7 +66,7 @@ def test_sum_with_level(): @pytest.mark.parametrize("func", [np.any, np.all]) -@pytest.mark.parametrize("kwargs", [dict(keepdims=True), dict(out=object())]) +@pytest.mark.parametrize("kwargs", [{"keepdims": True}, {"out": object()}]) def test_validate_any_all_out_keepdims_raises(kwargs, func): ser = Series([1, 2]) param = list(kwargs)[0] diff --git a/pandas/tests/series/test_validate.py b/pandas/tests/series/test_validate.py index e2f050650b298..3c867f7582b7d 100644 --- a/pandas/tests/series/test_validate.py +++ b/pandas/tests/series/test_validate.py @@ -17,7 +17,7 @@ def test_validate_bool_args(string_series, func, inplace): """Tests for error handling related to data types of method arguments.""" msg = 'For argument "inplace" expected type bool' - kwargs = dict(inplace=inplace) + kwargs = {"inplace": inplace} if func == "_set_name": kwargs["name"] = "hello" diff --git a/pandas/tests/test_sorting.py b/pandas/tests/test_sorting.py index 5f85ae2ec2318..da1c91a1ad218 100644 --- a/pandas/tests/test_sorting.py +++ b/pandas/tests/test_sorting.py @@ -270,12 +270,24 @@ def test_int64_overflow_issues(self): for k, lval in ldict.items(): rval = rdict.get(k, [np.nan]) for lv, rv in product(lval, rval): - vals.append(k + tuple([lv, rv])) + vals.append( + k + + ( + lv, + rv, + ) + ) for k, rval in rdict.items(): if k not in ldict: for rv in rval: - vals.append(k + tuple([np.nan, rv])) + vals.append( + k + + ( + np.nan, + rv, + ) + ) def align(df): df = df.sort_values(df.columns.tolist()) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index a40fcd725d604..e3f586d391fc6 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -117,7 +117,7 @@ def test_number_looking_strings_not_into_datetime(data): @pytest.mark.parametrize("errors", ["coerce", "raise"]) def test_coerce_outside_ns_bounds(invalid_date, errors): arr = np.array([invalid_date], dtype="object") - kwargs = dict(values=arr, errors=errors) + kwargs = {"values": arr, "errors": errors} if errors == "raise": msg = "Out of bounds nanosecond timestamp" @@ -144,7 +144,7 @@ def test_coerce_outside_ns_bounds_one_valid(): @pytest.mark.parametrize("errors", ["ignore", "coerce"]) def test_coerce_of_invalid_datetimes(errors): arr = np.array(["01-01-2013", "not_a_date", "1"], dtype=object) - kwargs = dict(values=arr, errors=errors) + kwargs = {"values": arr, "errors": errors} if errors == "ignore": # Without coercing, the presence of any invalid diff --git a/pandas/tests/tslibs/test_parsing.py b/pandas/tests/tslibs/test_parsing.py index 70fa724464226..e580b9112f3ec 100644 --- a/pandas/tests/tslibs/test_parsing.py +++ b/pandas/tests/tslibs/test_parsing.py @@ -75,7 +75,7 @@ def test_does_not_convert_mixed_integer(date_string, expected): [ ( "2013Q5", - dict(), + {}, ( "Incorrect quarterly string is given, " "quarter must be between 1 and 4: 2013Q5" @@ -84,7 +84,7 @@ def test_does_not_convert_mixed_integer(date_string, expected): # see gh-5418 ( "2013Q1", - dict(freq="INVLD-L-DEC-SAT"), + {"freq": "INVLD-L-DEC-SAT"}, ( "Unable to retrieve month information " "from given freq: INVLD-L-DEC-SAT" diff --git a/pandas/tests/util/test_assert_almost_equal.py b/pandas/tests/util/test_assert_almost_equal.py index c4bc3b7ee352d..ec8cb29c6dead 100644 --- a/pandas/tests/util/test_assert_almost_equal.py +++ b/pandas/tests/util/test_assert_almost_equal.py @@ -228,7 +228,7 @@ def test_assert_not_almost_equal_dicts(a, b): @pytest.mark.parametrize("val", [1, 2]) def test_assert_almost_equal_dict_like_object(val): dict_val = 1 - real_dict = dict(a=val) + real_dict = {"a": val} class DictLikeObj: def keys(self): diff --git a/pandas/tests/util/test_hashing.py b/pandas/tests/util/test_hashing.py index cf618f7c828aa..779d93eb14f24 100644 --- a/pandas/tests/util/test_hashing.py +++ b/pandas/tests/util/test_hashing.py @@ -305,14 +305,27 @@ def test_hash_with_tuple(): expected = Series([10345501319357378243, 8331063931016360761], dtype=np.uint64) tm.assert_series_equal(result, expected) - df2 = DataFrame({"data": [tuple([1]), tuple([2])]}) + df2 = DataFrame({"data": [(1,), (2,)]}) result = hash_pandas_object(df2) expected = Series([9408946347443669104, 3278256261030523334], dtype=np.uint64) tm.assert_series_equal(result, expected) # require that the elements of such tuples are themselves hashable - df3 = DataFrame({"data": [tuple([1, []]), tuple([2, {}])]}) + df3 = DataFrame( + { + "data": [ + ( + 1, + [], + ), + ( + 2, + {}, + ), + ] + } + ) with pytest.raises(TypeError, match="unhashable type: 'list'"): hash_pandas_object(df3) diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index ac77bfe0dfb48..6ce425e2575db 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -307,6 +307,6 @@ def test_multiple_agg_funcs(func, window_size, expected_vals): ) expected = DataFrame(expected_vals, index=index, columns=columns) - result = window.agg(dict((("low", ["mean", "max"]), ("high", ["mean", "min"])))) + result = window.agg({"low": ["mean", "max"], "high": ["mean", "min"]}) tm.assert_frame_equal(result, expected) diff --git a/setup.cfg b/setup.cfg index 7b404cb294f58..244e6f18bb0ef 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,9 +22,7 @@ ignore = W504, # line break after binary operator E402, # module level import not at top of file E731, # do not assign a lambda expression, use a def - C406, # Unnecessary list literal - rewrite as a dict literal. C408, # Unnecessary dict call - rewrite as a literal. - C409, # Unnecessary list passed to tuple() - rewrite as a tuple literal. S001 # found modulo formatter (incorrect picks up mod operations) exclude = doc/sphinxext/*.py, From 15c18285130b771a68246cede179c763f238d4a2 Mon Sep 17 00:00:00 2001 From: VirosaLi <2EkF8qUgpNkj> Date: Wed, 25 Nov 2020 22:57:32 -0600 Subject: [PATCH 2/5] fix list error --- pandas/tests/frame/methods/test_select_dtypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_select_dtypes.py b/pandas/tests/frame/methods/test_select_dtypes.py index f56debc1e4113..e923d7d088eff 100644 --- a/pandas/tests/frame/methods/test_select_dtypes.py +++ b/pandas/tests/frame/methods/test_select_dtypes.py @@ -202,7 +202,7 @@ def test_select_dtypes_duplicate_columns(self): # GH20839 df = DataFrame( { - "a": ["abc"], + "a": ['a', 'b', 'c'], "b": list(range(1, 4)), "c": np.arange(3, 6).astype("u1"), "d": np.arange(4.0, 7.0, dtype="float64"), From 239de8571abb4ff743c1fbad06735940fd8dcb83 Mon Sep 17 00:00:00 2001 From: VirosaLi <2EkF8qUgpNkj> Date: Wed, 25 Nov 2020 23:23:13 -0600 Subject: [PATCH 3/5] fix list error --- pandas/tests/frame/methods/test_select_dtypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/methods/test_select_dtypes.py b/pandas/tests/frame/methods/test_select_dtypes.py index e923d7d088eff..2a8826cedd50a 100644 --- a/pandas/tests/frame/methods/test_select_dtypes.py +++ b/pandas/tests/frame/methods/test_select_dtypes.py @@ -202,8 +202,8 @@ def test_select_dtypes_duplicate_columns(self): # GH20839 df = DataFrame( { - "a": ['a', 'b', 'c'], - "b": list(range(1, 4)), + "a": ["a", "b", "c"], + "b": [1, 2, 3], "c": np.arange(3, 6).astype("u1"), "d": np.arange(4.0, 7.0, dtype="float64"), "e": [True, False, True], From b746da2b1fe969eda6702bf96f2da3da79a4b21d Mon Sep 17 00:00:00 2001 From: VirosaLi <2EkF8qUgpNkj> Date: Wed, 25 Nov 2020 23:59:41 -0600 Subject: [PATCH 4/5] fix list error --- pandas/tests/indexes/base_class/test_setops.py | 4 ++-- pandas/tests/indexing/test_chaining_and_caching.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/base_class/test_setops.py b/pandas/tests/indexes/base_class/test_setops.py index 4d4940d3ab12c..6413b110dff2e 100644 --- a/pandas/tests/indexes/base_class/test_setops.py +++ b/pandas/tests/indexes/base_class/test_setops.py @@ -223,8 +223,8 @@ def test_tuple_union_bug(self, method, expected, sort): expected = Index(expected) tm.assert_index_equal(result, expected) - @pytest.mark.parametrize("first_list", [["ba"], []]) - @pytest.mark.parametrize("second_list", [["ab"], []]) + @pytest.mark.parametrize("first_list", [["b", "a"], []]) + @pytest.mark.parametrize("second_list", [["a", "b"], []]) @pytest.mark.parametrize( "first_name, second_name, expected_name", [("A", "B", None), (None, "B", None), ("A", None, None)], diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py index 0e91600f06f2d..90fa6e94d1bc8 100644 --- a/pandas/tests/indexing/test_chaining_and_caching.py +++ b/pandas/tests/indexing/test_chaining_and_caching.py @@ -303,7 +303,7 @@ def random_text(nobs=100): "A": date_range("20130101", periods=5), "B": np.random.randn(5), "C": np.arange(5, dtype="int64"), - "D": ["abcde"], + "D": ["a", "b", "c", "d", "e"], } ) From 7a7275a1e72a80a773568c7b5540c183846ada32 Mon Sep 17 00:00:00 2001 From: VirosaLi <2EkF8qUgpNkj> Date: Thu, 26 Nov 2020 09:03:13 -0600 Subject: [PATCH 5/5] fix list error --- pandas/tests/indexing/test_iloc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 971ceec1185fd..554b93c7cab5a 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -566,7 +566,9 @@ def test_iloc_setitem_list_of_lists(self): expected = DataFrame({"A": [0, 1, 10, 12, 4], "B": [5, 6, 11, 13, 9]}) tm.assert_frame_equal(df, expected) - df = DataFrame({"A": ["abcde"], "B": np.arange(5, 10, dtype="int64")}) + df = DataFrame( + {"A": ["a", "b", "c", "d", "e"], "B": np.arange(5, 10, dtype="int64")} + ) df.iloc[2:4] = [["x", 11], ["y", 13]] expected = DataFrame({"A": ["a", "b", "x", "y", "e"], "B": [5, 6, 11, 13, 9]}) tm.assert_frame_equal(df, expected)