Skip to content

CLN: fix flake8 C406, C409, and some of C408 #38078

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 5 commits into from
Nov 26, 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
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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)!")

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ def __init__(
)

self.axes = [axis]
self.blocks = tuple([block])
self.blocks = (block,)

@classmethod
def from_blocks(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
72 changes: 33 additions & 39 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/base/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
52 changes: 21 additions & 31 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]:
Expand Down Expand Up @@ -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(
[
Expand All @@ -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(
{
Expand All @@ -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
Expand Down
23 changes: 21 additions & 2 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
18 changes: 9 additions & 9 deletions pandas/tests/frame/methods/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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(
Expand Down
26 changes: 12 additions & 14 deletions pandas/tests/frame/methods/test_select_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": ["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],
"f": pd.date_range("now", periods=3).values,
}
)
df.columns = ["a", "a", "b", "b", "b", "c"]

Expand Down Expand Up @@ -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)
Expand Down
Loading