Skip to content

Commit 3391a34

Browse files
authored
CLN: fix flake8 C406, C409, and some of C408 (#38078)
1 parent 1fc5efd commit 3391a34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+338
-270
lines changed

asv_bench/benchmarks/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def setup(self):
486486
tmp2 = (np.random.random(10000) * 10.0).astype(np.float32)
487487
tmp = np.concatenate((tmp1, tmp2))
488488
arr = np.repeat(tmp, 10)
489-
self.df = DataFrame(dict(a=arr, b=arr))
489+
self.df = DataFrame({"a": arr, "b": arr})
490490

491491
def time_sum(self):
492492
self.df.groupby(["a"])["b"].sum()

pandas/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ def index_with_missing(request):
472472
if request.param in ["tuples", "mi-with-dt64tz-level", "multi"]:
473473
# For setting missing values in the top level of MultiIndex
474474
vals = ind.tolist()
475-
vals[0] = tuple([None]) + vals[0][1:]
476-
vals[-1] = tuple([None]) + vals[-1][1:]
475+
vals[0] = (None,) + vals[0][1:]
476+
vals[-1] = (None,) + vals[-1][1:]
477477
return MultiIndex.from_tuples(vals)
478478
else:
479479
vals[0] = None

pandas/core/indexers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def is_empty_indexer(indexer, arr_value: np.ndarray) -> bool:
105105
return True
106106
if arr_value.ndim == 1:
107107
if not isinstance(indexer, tuple):
108-
indexer = tuple([indexer])
108+
indexer = (indexer,)
109109
return any(isinstance(idx, np.ndarray) and len(idx) == 0 for idx in indexer)
110110
return False
111111

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3558,7 +3558,7 @@ def _reindex_non_unique(self, target):
35583558
cur_labels = self.take(indexer[check]).values
35593559
cur_indexer = ensure_int64(length[check])
35603560

3561-
new_labels = np.empty(tuple([len(indexer)]), dtype=object)
3561+
new_labels = np.empty((len(indexer),), dtype=object)
35623562
new_labels[cur_indexer] = cur_labels
35633563
new_labels[missing_indexer] = missing_labels
35643564

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,7 @@ def _align_series(self, indexer, ser: "Series", multiindex_indexer: bool = False
19341934
to the locations selected by `indexer`
19351935
"""
19361936
if isinstance(indexer, (slice, np.ndarray, list, Index)):
1937-
indexer = tuple([indexer])
1937+
indexer = (indexer,)
19381938

19391939
if isinstance(indexer, tuple):
19401940

@@ -2073,7 +2073,7 @@ def __getitem__(self, key):
20732073

20742074
# we could have a convertible item here (e.g. Timestamp)
20752075
if not is_list_like_indexer(key):
2076-
key = tuple([key])
2076+
key = (key,)
20772077
else:
20782078
raise ValueError("Invalid call for scalar access (getting)!")
20792079

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ def __init__(
15501550
)
15511551

15521552
self.axes = [axis]
1553-
self.blocks = tuple([block])
1553+
self.blocks = (block,)
15541554

15551555
@classmethod
15561556
def from_blocks(

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def _init_dict(self, data, index=None, dtype=None):
368368
values = na_value_for_dtype(dtype)
369369
keys = index
370370
else:
371-
keys, values = tuple([]), []
371+
keys, values = tuple(), []
372372

373373
# Input is now list-like, so rely on "standard" construction:
374374

pandas/io/stata.py

+33-39
Original file line numberDiff line numberDiff line change
@@ -873,30 +873,26 @@ def __init__(self):
873873
(255, np.dtype(np.float64)),
874874
]
875875
)
876-
self.DTYPE_MAP_XML = dict(
877-
[
878-
(32768, np.dtype(np.uint8)), # Keys to GSO
879-
(65526, np.dtype(np.float64)),
880-
(65527, np.dtype(np.float32)),
881-
(65528, np.dtype(np.int32)),
882-
(65529, np.dtype(np.int16)),
883-
(65530, np.dtype(np.int8)),
884-
]
885-
)
876+
self.DTYPE_MAP_XML = {
877+
32768: np.dtype(np.uint8), # Keys to GSO
878+
65526: np.dtype(np.float64),
879+
65527: np.dtype(np.float32),
880+
65528: np.dtype(np.int32),
881+
65529: np.dtype(np.int16),
882+
65530: np.dtype(np.int8),
883+
}
886884
# error: Argument 1 to "list" has incompatible type "str";
887885
# expected "Iterable[int]" [arg-type]
888886
self.TYPE_MAP = list(range(251)) + list("bhlfd") # type: ignore[arg-type]
889-
self.TYPE_MAP_XML = dict(
890-
[
891-
# Not really a Q, unclear how to handle byteswap
892-
(32768, "Q"),
893-
(65526, "d"),
894-
(65527, "f"),
895-
(65528, "l"),
896-
(65529, "h"),
897-
(65530, "b"),
898-
]
899-
)
887+
self.TYPE_MAP_XML = {
888+
# Not really a Q, unclear how to handle byteswap
889+
32768: "Q",
890+
65526: "d",
891+
65527: "f",
892+
65528: "l",
893+
65529: "h",
894+
65530: "b",
895+
}
900896
# NOTE: technically, some of these are wrong. there are more numbers
901897
# that can be represented. it's the 27 ABOVE and BELOW the max listed
902898
# numeric data type in [U] 12.2.2 of the 11.2 manual
@@ -3138,24 +3134,22 @@ def _write_map(self) -> None:
31383134
all blocks have been written.
31393135
"""
31403136
if not self._map:
3141-
self._map = dict(
3142-
(
3143-
("stata_data", 0),
3144-
("map", self.handles.handle.tell()),
3145-
("variable_types", 0),
3146-
("varnames", 0),
3147-
("sortlist", 0),
3148-
("formats", 0),
3149-
("value_label_names", 0),
3150-
("variable_labels", 0),
3151-
("characteristics", 0),
3152-
("data", 0),
3153-
("strls", 0),
3154-
("value_labels", 0),
3155-
("stata_data_close", 0),
3156-
("end-of-file", 0),
3157-
)
3158-
)
3137+
self._map = {
3138+
"stata_data": 0,
3139+
"map": self.handles.handle.tell(),
3140+
"variable_types": 0,
3141+
"varnames": 0,
3142+
"sortlist": 0,
3143+
"formats": 0,
3144+
"value_label_names": 0,
3145+
"variable_labels": 0,
3146+
"characteristics": 0,
3147+
"data": 0,
3148+
"strls": 0,
3149+
"value_labels": 0,
3150+
"stata_data_close": 0,
3151+
"end-of-file": 0,
3152+
}
31593153
# Move to start of map
31603154
self.handles.handle.seek(self._map["map"])
31613155
bio = BytesIO()

pandas/tests/arithmetic/test_datetime64.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1288,8 +1288,8 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array):
12881288
("seconds", 2),
12891289
("microseconds", 5),
12901290
]
1291-
for i, kwd in enumerate(relative_kwargs):
1292-
off = DateOffset(**dict([kwd]))
1291+
for i, (unit, value) in enumerate(relative_kwargs):
1292+
off = DateOffset(**{unit: value})
12931293

12941294
expected = DatetimeIndex([x + off for x in vec_items])
12951295
expected = tm.box_expected(expected, box_with_array)

pandas/tests/base/test_conversion.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_iterable_map(self, index_or_series, dtype, rdtype):
109109
s = typ([1], dtype=dtype)
110110
result = s.map(type)[0]
111111
if not isinstance(rdtype, tuple):
112-
rdtype = tuple([rdtype])
112+
rdtype = (rdtype,)
113113
assert result in rdtype
114114

115115
@pytest.mark.parametrize(

pandas/tests/frame/apply/test_frame_apply.py

+21-31
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,9 @@ def test_apply_dict(self):
562562

563563
# GH 8735
564564
A = DataFrame([["foo", "bar"], ["spam", "eggs"]])
565-
A_dicts = Series(
566-
[dict([(0, "foo"), (1, "spam")]), dict([(0, "bar"), (1, "eggs")])]
567-
)
565+
A_dicts = Series([{0: "foo", 1: "spam"}, {0: "bar", 1: "eggs"}])
568566
B = DataFrame([[0, 1], [2, 3]])
569-
B_dicts = Series([dict([(0, 0), (1, 2)]), dict([(0, 1), (1, 3)])])
567+
B_dicts = Series([{0: 0, 1: 2}, {0: 1, 1: 3}])
570568
fn = lambda x: x.to_dict()
571569

572570
for df, dicts in [(A, A_dicts), (B, B_dicts)]:
@@ -1221,7 +1219,7 @@ def test_agg_reduce(self, axis, float_frame):
12211219
tm.assert_frame_equal(result, expected)
12221220

12231221
# dict input with scalars
1224-
func = dict([(name1, "mean"), (name2, "sum")])
1222+
func = {name1: "mean", name2: "sum"}
12251223
result = float_frame.agg(func, axis=axis)
12261224
expected = Series(
12271225
[
@@ -1233,7 +1231,7 @@ def test_agg_reduce(self, axis, float_frame):
12331231
tm.assert_series_equal(result, expected)
12341232

12351233
# dict input with lists
1236-
func = dict([(name1, ["mean"]), (name2, ["sum"])])
1234+
func = {name1: ["mean"], name2: ["sum"]}
12371235
result = float_frame.agg(func, axis=axis)
12381236
expected = DataFrame(
12391237
{
@@ -1249,33 +1247,25 @@ def test_agg_reduce(self, axis, float_frame):
12491247
tm.assert_frame_equal(result, expected)
12501248

12511249
# dict input with lists with multiple
1252-
func = dict([(name1, ["mean", "sum"]), (name2, ["sum", "max"])])
1250+
func = {name1: ["mean", "sum"], name2: ["sum", "max"]}
12531251
result = float_frame.agg(func, axis=axis)
12541252
expected = pd.concat(
1255-
dict(
1256-
[
1257-
(
1258-
name1,
1259-
Series(
1260-
[
1261-
float_frame.loc(other_axis)[name1].mean(),
1262-
float_frame.loc(other_axis)[name1].sum(),
1263-
],
1264-
index=["mean", "sum"],
1265-
),
1266-
),
1267-
(
1268-
name2,
1269-
Series(
1270-
[
1271-
float_frame.loc(other_axis)[name2].sum(),
1272-
float_frame.loc(other_axis)[name2].max(),
1273-
],
1274-
index=["sum", "max"],
1275-
),
1276-
),
1277-
]
1278-
),
1253+
{
1254+
name1: Series(
1255+
[
1256+
float_frame.loc(other_axis)[name1].mean(),
1257+
float_frame.loc(other_axis)[name1].sum(),
1258+
],
1259+
index=["mean", "sum"],
1260+
),
1261+
name2: Series(
1262+
[
1263+
float_frame.loc(other_axis)[name2].sum(),
1264+
float_frame.loc(other_axis)[name2].max(),
1265+
],
1266+
index=["sum", "max"],
1267+
),
1268+
},
12791269
axis=1,
12801270
)
12811271
expected = expected.T if axis in {1, "columns"} else expected

pandas/tests/frame/indexing/test_indexing.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -1503,10 +1503,29 @@ def test_loc_getitem_index_namedtuple(self):
15031503
result = df.loc[IndexType("foo", "bar")]["A"]
15041504
assert result == 1
15051505

1506-
@pytest.mark.parametrize("tpl", [tuple([1]), tuple([1, 2])])
1506+
@pytest.mark.parametrize(
1507+
"tpl",
1508+
[
1509+
(1,),
1510+
(
1511+
1,
1512+
2,
1513+
),
1514+
],
1515+
)
15071516
def test_loc_getitem_index_single_double_tuples(self, tpl):
15081517
# GH 20991
1509-
idx = Index([tuple([1]), tuple([1, 2])], name="A", tupleize_cols=False)
1518+
idx = Index(
1519+
[
1520+
(1,),
1521+
(
1522+
1,
1523+
2,
1524+
),
1525+
],
1526+
name="A",
1527+
tupleize_cols=False,
1528+
)
15101529
df = DataFrame(index=idx)
15111530

15121531
result = df.loc[[tpl]]

pandas/tests/frame/methods/test_dtypes.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_empty_frame_dtypes(self):
3232
norows_int_df.dtypes, Series(np.dtype("int32"), index=list("abc"))
3333
)
3434

35-
df = DataFrame(dict([("a", 1), ("b", True), ("c", 1.0)]), index=[1, 2, 3])
36-
ex_dtypes = Series(dict([("a", np.int64), ("b", np.bool_), ("c", np.float64)]))
35+
df = DataFrame({"a": 1, "b": True, "c": 1.0}, index=[1, 2, 3])
36+
ex_dtypes = Series({"a": np.int64, "b": np.bool_, "c": np.float64})
3737
tm.assert_series_equal(df.dtypes, ex_dtypes)
3838

3939
# same but for empty slice of df
@@ -66,12 +66,12 @@ def test_dtypes_are_correct_after_column_slice(self):
6666
df = DataFrame(index=range(5), columns=list("abc"), dtype=np.float_)
6767
tm.assert_series_equal(
6868
df.dtypes,
69-
Series(dict([("a", np.float_), ("b", np.float_), ("c", np.float_)])),
69+
Series({"a": np.float_, "b": np.float_, "c": np.float_}),
7070
)
71-
tm.assert_series_equal(df.iloc[:, 2:].dtypes, Series(dict([("c", np.float_)])))
71+
tm.assert_series_equal(df.iloc[:, 2:].dtypes, Series({"c": np.float_}))
7272
tm.assert_series_equal(
7373
df.dtypes,
74-
Series(dict([("a", np.float_), ("b", np.float_), ("c", np.float_)])),
74+
Series({"a": np.float_, "b": np.float_, "c": np.float_}),
7575
)
7676

7777
def test_dtypes_gh8722(self, float_string_frame):
@@ -90,10 +90,10 @@ def test_dtypes_gh8722(self, float_string_frame):
9090

9191
def test_dtypes_timedeltas(self):
9292
df = DataFrame(
93-
dict(
94-
A=Series(date_range("2012-1-1", periods=3, freq="D")),
95-
B=Series([timedelta(days=i) for i in range(3)]),
96-
)
93+
{
94+
"A": Series(date_range("2012-1-1", periods=3, freq="D")),
95+
"B": Series([timedelta(days=i) for i in range(3)]),
96+
}
9797
)
9898
result = df.dtypes
9999
expected = Series(

pandas/tests/frame/methods/test_select_dtypes.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,14 @@ def test_select_dtypes_include_exclude_mixed_scalars_lists(self):
201201
def test_select_dtypes_duplicate_columns(self):
202202
# GH20839
203203
df = DataFrame(
204-
dict(
205-
[
206-
("a", list("abc")),
207-
("b", list(range(1, 4))),
208-
("c", np.arange(3, 6).astype("u1")),
209-
("d", np.arange(4.0, 7.0, dtype="float64")),
210-
("e", [True, False, True]),
211-
("f", pd.date_range("now", periods=3).values),
212-
]
213-
)
204+
{
205+
"a": ["a", "b", "c"],
206+
"b": [1, 2, 3],
207+
"c": np.arange(3, 6).astype("u1"),
208+
"d": np.arange(4.0, 7.0, dtype="float64"),
209+
"e": [True, False, True],
210+
"f": pd.date_range("now", periods=3).values,
211+
}
214212
)
215213
df.columns = ["a", "a", "b", "b", "b", "c"]
216214

@@ -268,10 +266,10 @@ def test_select_dtypes_bad_datetime64(self):
268266
def test_select_dtypes_datetime_with_tz(self):
269267

270268
df2 = DataFrame(
271-
dict(
272-
A=Timestamp("20130102", tz="US/Eastern"),
273-
B=Timestamp("20130603", tz="CET"),
274-
),
269+
{
270+
"A": Timestamp("20130102", tz="US/Eastern"),
271+
"B": Timestamp("20130603", tz="CET"),
272+
},
275273
index=range(5),
276274
)
277275
df3 = pd.concat([df2.A.to_frame(), df2.B.to_frame()], axis=1)

0 commit comments

Comments
 (0)