Skip to content
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

Rewrite dict literal for files in pandas/tests/frame #38207

Merged
merged 1 commit into from
Dec 2, 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
4 changes: 2 additions & 2 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def test_setitem_empty(self):
tm.assert_frame_equal(result, df)

@pytest.mark.parametrize("dtype", ["float", "int64"])
@pytest.mark.parametrize("kwargs", [dict(), dict(index=[1]), dict(columns=["A"])])
@pytest.mark.parametrize("kwargs", [{}, {"index": [1]}, {"columns": ["A"]}])
def test_setitem_empty_frame_with_boolean(self, dtype, kwargs):
# see gh-10126
kwargs["dtype"] = dtype
Expand Down Expand Up @@ -1238,7 +1238,7 @@ def test_single_element_ix_dont_upcast(self, float_frame):
assert is_integer(result)

# GH 11617
df = DataFrame(dict(a=[1.23]))
df = DataFrame({"a": [1.23]})
df["b"] = 666

result = df.loc[0, "b"]
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/frame/indexing/test_where.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ def test_where_datetime(self):

# GH 3311
df = DataFrame(
dict(
A=date_range("20130102", periods=5),
B=date_range("20130104", periods=5),
C=np.random.randn(5),
)
{
"A": date_range("20130102", periods=5),
"B": date_range("20130104", periods=5),
"C": np.random.randn(5),
}
)

stamp = datetime(2013, 1, 3)
Expand Down Expand Up @@ -618,7 +618,7 @@ def test_df_where_change_dtype(self):

tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("kwargs", [dict(), dict(other=None)])
@pytest.mark.parametrize("kwargs", [{}, {"other": None}])
def test_df_where_with_category(self, kwargs):
# GH#16979
df = DataFrame(np.arange(2 * 3).reshape(2, 3), columns=list("ABC"))
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ def test_fillna_mixed_float(self, mixed_float_frame):
mf = mixed_float_frame.reindex(columns=["A", "B", "D"])
mf.loc[mf.index[-10:], "A"] = np.nan
result = mf.fillna(value=0)
_check_mixed_float(result, dtype=dict(C=None))
_check_mixed_float(result, dtype={"C": None})

result = mf.fillna(method="pad")
_check_mixed_float(result, dtype=dict(C=None))
_check_mixed_float(result, dtype={"C": None})

def test_fillna_empty(self):
# empty frame (GH#2778)
Expand Down Expand Up @@ -262,7 +262,7 @@ def test_fillna_dtype_conversion(self):
tm.assert_frame_equal(result, expected)

# equiv of replace
df = DataFrame(dict(A=[1, np.nan], B=[1.0, 2.0]))
df = DataFrame({"A": [1, np.nan], "B": [1.0, 2.0]})
for v in ["", 1, np.nan, 1.0]:
expected = df.replace(np.nan, v)
result = df.fillna(v)
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/frame/methods/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ def test_sort_values_nat_values_in_int_column(self):
float_values = (2.0, -1.797693e308)

df = DataFrame(
dict(int=int_values, float=float_values), columns=["int", "float"]
{"int": int_values, "float": float_values}, columns=["int", "float"]
)

df_reversed = DataFrame(
dict(int=int_values[::-1], float=float_values[::-1]),
{"int": int_values[::-1], "float": float_values[::-1]},
columns=["int", "float"],
index=[1, 0],
)
Expand All @@ -329,12 +329,12 @@ def test_sort_values_nat_values_in_int_column(self):
# and now check if NaT is still considered as "na" for datetime64
# columns:
df = DataFrame(
dict(datetime=[Timestamp("2016-01-01"), NaT], float=float_values),
{"datetime": [Timestamp("2016-01-01"), NaT], "float": float_values},
columns=["datetime", "float"],
)

df_reversed = DataFrame(
dict(datetime=[NaT, Timestamp("2016-01-01")], float=float_values[::-1]),
{"datetime": [NaT, Timestamp("2016-01-01")], "float": float_values[::-1]},
columns=["datetime", "float"],
index=[1, 0],
)
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/frame/methods/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

class TestDataFrameToCSV:
def read_csv(self, path, **kwargs):
params = dict(index_col=0, parse_dates=True)
params = {"index_col": 0, "parse_dates": True}
params.update(**kwargs)

return pd.read_csv(path, **params)
Expand Down Expand Up @@ -248,7 +248,7 @@ def make_dtnat_arr(n, nnat=None):

# s3=make_dtnjat_arr(chunksize+5,0)
with tm.ensure_clean("1.csv") as pth:
df = DataFrame(dict(a=s1, b=s2))
df = DataFrame({"a": s1, "b": s2})
df.to_csv(pth, chunksize=chunksize)

recons = self.read_csv(pth).apply(to_datetime)
Expand All @@ -260,7 +260,7 @@ def _do_test(
df, r_dtype=None, c_dtype=None, rnlvl=None, cnlvl=None, dupe_col=False
):

kwargs = dict(parse_dates=False)
kwargs = {"parse_dates": False}
if cnlvl:
if rnlvl is not None:
kwargs["index_col"] = list(range(rnlvl))
Expand Down Expand Up @@ -291,7 +291,7 @@ def _to_uni(x):
recons.index = ix
recons = recons.iloc[:, rnlvl - 1 :]

type_map = dict(i="i", f="f", s="O", u="O", dt="O", p="O")
type_map = {"i": "i", "f": "f", "s": "O", "u": "O", "dt": "O", "p": "O"}
if r_dtype:
if r_dtype == "u": # unicode
r_dtype = "O"
Expand Down Expand Up @@ -738,7 +738,7 @@ def create_cols(name):
df = pd.concat([df_float, df_int, df_bool, df_object, df_dt], axis=1)

# dtype
dtypes = dict()
dtypes = {}
for n, dtype in [
("float", np.float64),
("int", np.int64),
Expand Down
57 changes: 30 additions & 27 deletions pandas/tests/frame/methods/test_to_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,128 +131,131 @@ def test_to_records_with_categorical(self):
[
# No dtypes --> default to array dtypes.
(
dict(),
{},
np.rec.array(
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
dtype=[("index", "<i8"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
),
),
# Should have no effect in this case.
(
dict(index=True),
{"index": True},
np.rec.array(
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
dtype=[("index", "<i8"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
),
),
# Column dtype applied across the board. Index unaffected.
(
dict(column_dtypes="<U4"),
{"column_dtypes": "<U4"},
np.rec.array(
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "<U4"), ("B", "<U4"), ("C", "<U4")],
),
),
# Index dtype applied across the board. Columns unaffected.
(
dict(index_dtypes="<U1"),
{"index_dtypes": "<U1"},
np.rec.array(
[("0", 1, 0.2, "a"), ("1", 2, 1.5, "bc")],
dtype=[("index", "<U1"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
),
),
# Pass in a type instance.
(
dict(column_dtypes=str),
{"column_dtypes": str},
np.rec.array(
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "<U"), ("B", "<U"), ("C", "<U")],
),
),
# Pass in a dtype instance.
(
dict(column_dtypes=np.dtype("unicode")),
{"column_dtypes": np.dtype("unicode")},
np.rec.array(
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "<U"), ("B", "<U"), ("C", "<U")],
),
),
# Pass in a dictionary (name-only).
(
dict(column_dtypes={"A": np.int8, "B": np.float32, "C": "<U2"}),
{"column_dtypes": {"A": np.int8, "B": np.float32, "C": "<U2"}},
np.rec.array(
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "<U2")],
),
),
# Pass in a dictionary (indices-only).
(
dict(index_dtypes={0: "int16"}),
{"index_dtypes": {0: "int16"}},
np.rec.array(
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
dtype=[("index", "i2"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
),
),
# Ignore index mappings if index is not True.
(
dict(index=False, index_dtypes="<U2"),
{"index": False, "index_dtypes": "<U2"},
np.rec.array(
[(1, 0.2, "a"), (2, 1.5, "bc")],
dtype=[("A", "<i8"), ("B", "<f8"), ("C", "O")],
),
),
# Non-existent names / indices in mapping should not error.
(
dict(index_dtypes={0: "int16", "not-there": "float32"}),
{"index_dtypes": {0: "int16", "not-there": "float32"}},
np.rec.array(
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
dtype=[("index", "i2"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
),
),
# Names / indices not in mapping default to array dtype.
(
dict(column_dtypes={"A": np.int8, "B": np.float32}),
{"column_dtypes": {"A": np.int8, "B": np.float32}},
np.rec.array(
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
),
),
# Names / indices not in dtype mapping default to array dtype.
(
dict(column_dtypes={"A": np.dtype("int8"), "B": np.dtype("float32")}),
{"column_dtypes": {"A": np.dtype("int8"), "B": np.dtype("float32")}},
np.rec.array(
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
),
),
# Mixture of everything.
(
dict(column_dtypes={"A": np.int8, "B": np.float32}, index_dtypes="<U2"),
{
"column_dtypes": {"A": np.int8, "B": np.float32},
"index_dtypes": "<U2",
},
np.rec.array(
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
dtype=[("index", "<U2"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
),
),
# Invalid dype values.
(
dict(index=False, column_dtypes=list()),
{"index": False, "column_dtypes": list()},
(ValueError, "Invalid dtype \\[\\] specified for column A"),
),
(
dict(index=False, column_dtypes={"A": "int32", "B": 5}),
{"index": False, "column_dtypes": {"A": "int32", "B": 5}},
(ValueError, "Invalid dtype 5 specified for column B"),
),
# Numpy can't handle EA types, so check error is raised
(
dict(
index=False,
column_dtypes={"A": "int32", "B": CategoricalDtype(["a", "b"])},
),
{
"index": False,
"column_dtypes": {"A": "int32", "B": CategoricalDtype(["a", "b"])},
},
(ValueError, "Invalid dtype category specified for column B"),
),
# Check that bad types raise
(
dict(index=False, column_dtypes={"A": "int32", "B": "foo"}),
{"index": False, "column_dtypes": {"A": "int32", "B": "foo"}},
(TypeError, "data type [\"']foo[\"'] not understood"),
),
],
Expand All @@ -276,7 +279,7 @@ def test_to_records_dtype(self, kwargs, expected):
DataFrame(
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("abc")
).set_index(["a", "b"]),
dict(column_dtypes="float64", index_dtypes={0: "int32", 1: "int8"}),
{"column_dtypes": "float64", "index_dtypes": {0: "int32", 1: "int8"}},
np.rec.array(
[(1, 2, 3.0), (4, 5, 6.0), (7, 8, 9.0)],
dtype=[("a", "<i4"), ("b", "i1"), ("c", "<f8")],
Expand All @@ -290,7 +293,7 @@ def test_to_records_dtype(self, kwargs, expected):
[("a", "d"), ("b", "e"), ("c", "f")]
),
),
dict(column_dtypes={0: "<U1", 2: "float32"}, index_dtypes="float32"),
{"column_dtypes": {0: "<U1", 2: "float32"}, "index_dtypes": "float32"},
np.rec.array(
[(0.0, "1", 2, 3.0), (1.0, "4", 5, 6.0), (2.0, "7", 8, 9.0)],
dtype=[
Expand All @@ -312,7 +315,7 @@ def test_to_records_dtype(self, kwargs, expected):
[("d", -4), ("d", -5), ("f", -6)], names=list("cd")
),
),
dict(column_dtypes="float64", index_dtypes={0: "<U2", 1: "int8"}),
{"column_dtypes": "float64", "index_dtypes": {0: "<U2", 1: "int8"}},
np.rec.array(
[
("d", -4, 1.0, 2.0, 3.0),
Expand Down Expand Up @@ -352,10 +355,10 @@ def keys(self):

df = DataFrame({"A": [1, 2], "B": [0.2, 1.5], "C": ["a", "bc"]})

dtype_mappings = dict(
column_dtypes=DictLike(**{"A": np.int8, "B": np.float32}),
index_dtypes="<U2",
)
dtype_mappings = {
"column_dtypes": DictLike(**{"A": np.int8, "B": np.float32}),
"index_dtypes": "<U2",
}

result = df.to_records(**dtype_mappings)
expected = np.rec.array(
Expand Down
Loading