Skip to content

Commit b687883

Browse files
authored
change dict to literal (#38207)
1 parent 6afcb69 commit b687883

File tree

8 files changed

+71
-68
lines changed

8 files changed

+71
-68
lines changed

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def test_setitem_empty(self):
714714
tm.assert_frame_equal(result, df)
715715

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

12401240
# GH 11617
1241-
df = DataFrame(dict(a=[1.23]))
1241+
df = DataFrame({"a": [1.23]})
12421242
df["b"] = 666
12431243

12441244
result = df.loc[0, "b"]

pandas/tests/frame/indexing/test_where.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ def test_where_datetime(self):
356356

357357
# GH 3311
358358
df = DataFrame(
359-
dict(
360-
A=date_range("20130102", periods=5),
361-
B=date_range("20130104", periods=5),
362-
C=np.random.randn(5),
363-
)
359+
{
360+
"A": date_range("20130102", periods=5),
361+
"B": date_range("20130104", periods=5),
362+
"C": np.random.randn(5),
363+
}
364364
)
365365

366366
stamp = datetime(2013, 1, 3)
@@ -618,7 +618,7 @@ def test_df_where_change_dtype(self):
618618

619619
tm.assert_frame_equal(result, expected)
620620

621-
@pytest.mark.parametrize("kwargs", [dict(), dict(other=None)])
621+
@pytest.mark.parametrize("kwargs", [{}, {"other": None}])
622622
def test_df_where_with_category(self, kwargs):
623623
# GH#16979
624624
df = DataFrame(np.arange(2 * 3).reshape(2, 3), columns=list("ABC"))

pandas/tests/frame/methods/test_fillna.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ def test_fillna_mixed_float(self, mixed_float_frame):
5353
mf = mixed_float_frame.reindex(columns=["A", "B", "D"])
5454
mf.loc[mf.index[-10:], "A"] = np.nan
5555
result = mf.fillna(value=0)
56-
_check_mixed_float(result, dtype=dict(C=None))
56+
_check_mixed_float(result, dtype={"C": None})
5757

5858
result = mf.fillna(method="pad")
59-
_check_mixed_float(result, dtype=dict(C=None))
59+
_check_mixed_float(result, dtype={"C": None})
6060

6161
def test_fillna_empty(self):
6262
# empty frame (GH#2778)
@@ -262,7 +262,7 @@ def test_fillna_dtype_conversion(self):
262262
tm.assert_frame_equal(result, expected)
263263

264264
# equiv of replace
265-
df = DataFrame(dict(A=[1, np.nan], B=[1.0, 2.0]))
265+
df = DataFrame({"A": [1, np.nan], "B": [1.0, 2.0]})
266266
for v in ["", 1, np.nan, 1.0]:
267267
expected = df.replace(np.nan, v)
268268
result = df.fillna(v)

pandas/tests/frame/methods/test_sort_values.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ def test_sort_values_nat_values_in_int_column(self):
305305
float_values = (2.0, -1.797693e308)
306306

307307
df = DataFrame(
308-
dict(int=int_values, float=float_values), columns=["int", "float"]
308+
{"int": int_values, "float": float_values}, columns=["int", "float"]
309309
)
310310

311311
df_reversed = DataFrame(
312-
dict(int=int_values[::-1], float=float_values[::-1]),
312+
{"int": int_values[::-1], "float": float_values[::-1]},
313313
columns=["int", "float"],
314314
index=[1, 0],
315315
)
@@ -329,12 +329,12 @@ def test_sort_values_nat_values_in_int_column(self):
329329
# and now check if NaT is still considered as "na" for datetime64
330330
# columns:
331331
df = DataFrame(
332-
dict(datetime=[Timestamp("2016-01-01"), NaT], float=float_values),
332+
{"datetime": [Timestamp("2016-01-01"), NaT], "float": float_values},
333333
columns=["datetime", "float"],
334334
)
335335

336336
df_reversed = DataFrame(
337-
dict(datetime=[NaT, Timestamp("2016-01-01")], float=float_values[::-1]),
337+
{"datetime": [NaT, Timestamp("2016-01-01")], "float": float_values[::-1]},
338338
columns=["datetime", "float"],
339339
index=[1, 0],
340340
)

pandas/tests/frame/methods/test_to_csv.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
class TestDataFrameToCSV:
4040
def read_csv(self, path, **kwargs):
41-
params = dict(index_col=0, parse_dates=True)
41+
params = {"index_col": 0, "parse_dates": True}
4242
params.update(**kwargs)
4343

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

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

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

263-
kwargs = dict(parse_dates=False)
263+
kwargs = {"parse_dates": False}
264264
if cnlvl:
265265
if rnlvl is not None:
266266
kwargs["index_col"] = list(range(rnlvl))
@@ -291,7 +291,7 @@ def _to_uni(x):
291291
recons.index = ix
292292
recons = recons.iloc[:, rnlvl - 1 :]
293293

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

740740
# dtype
741-
dtypes = dict()
741+
dtypes = {}
742742
for n, dtype in [
743743
("float", np.float64),
744744
("int", np.int64),

pandas/tests/frame/methods/test_to_records.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -131,128 +131,131 @@ def test_to_records_with_categorical(self):
131131
[
132132
# No dtypes --> default to array dtypes.
133133
(
134-
dict(),
134+
{},
135135
np.rec.array(
136136
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
137137
dtype=[("index", "<i8"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
138138
),
139139
),
140140
# Should have no effect in this case.
141141
(
142-
dict(index=True),
142+
{"index": True},
143143
np.rec.array(
144144
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
145145
dtype=[("index", "<i8"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
146146
),
147147
),
148148
# Column dtype applied across the board. Index unaffected.
149149
(
150-
dict(column_dtypes="<U4"),
150+
{"column_dtypes": "<U4"},
151151
np.rec.array(
152152
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
153153
dtype=[("index", "<i8"), ("A", "<U4"), ("B", "<U4"), ("C", "<U4")],
154154
),
155155
),
156156
# Index dtype applied across the board. Columns unaffected.
157157
(
158-
dict(index_dtypes="<U1"),
158+
{"index_dtypes": "<U1"},
159159
np.rec.array(
160160
[("0", 1, 0.2, "a"), ("1", 2, 1.5, "bc")],
161161
dtype=[("index", "<U1"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
162162
),
163163
),
164164
# Pass in a type instance.
165165
(
166-
dict(column_dtypes=str),
166+
{"column_dtypes": str},
167167
np.rec.array(
168168
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
169169
dtype=[("index", "<i8"), ("A", "<U"), ("B", "<U"), ("C", "<U")],
170170
),
171171
),
172172
# Pass in a dtype instance.
173173
(
174-
dict(column_dtypes=np.dtype("unicode")),
174+
{"column_dtypes": np.dtype("unicode")},
175175
np.rec.array(
176176
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
177177
dtype=[("index", "<i8"), ("A", "<U"), ("B", "<U"), ("C", "<U")],
178178
),
179179
),
180180
# Pass in a dictionary (name-only).
181181
(
182-
dict(column_dtypes={"A": np.int8, "B": np.float32, "C": "<U2"}),
182+
{"column_dtypes": {"A": np.int8, "B": np.float32, "C": "<U2"}},
183183
np.rec.array(
184184
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
185185
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "<U2")],
186186
),
187187
),
188188
# Pass in a dictionary (indices-only).
189189
(
190-
dict(index_dtypes={0: "int16"}),
190+
{"index_dtypes": {0: "int16"}},
191191
np.rec.array(
192192
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
193193
dtype=[("index", "i2"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
194194
),
195195
),
196196
# Ignore index mappings if index is not True.
197197
(
198-
dict(index=False, index_dtypes="<U2"),
198+
{"index": False, "index_dtypes": "<U2"},
199199
np.rec.array(
200200
[(1, 0.2, "a"), (2, 1.5, "bc")],
201201
dtype=[("A", "<i8"), ("B", "<f8"), ("C", "O")],
202202
),
203203
),
204204
# Non-existent names / indices in mapping should not error.
205205
(
206-
dict(index_dtypes={0: "int16", "not-there": "float32"}),
206+
{"index_dtypes": {0: "int16", "not-there": "float32"}},
207207
np.rec.array(
208208
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
209209
dtype=[("index", "i2"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
210210
),
211211
),
212212
# Names / indices not in mapping default to array dtype.
213213
(
214-
dict(column_dtypes={"A": np.int8, "B": np.float32}),
214+
{"column_dtypes": {"A": np.int8, "B": np.float32}},
215215
np.rec.array(
216216
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
217217
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
218218
),
219219
),
220220
# Names / indices not in dtype mapping default to array dtype.
221221
(
222-
dict(column_dtypes={"A": np.dtype("int8"), "B": np.dtype("float32")}),
222+
{"column_dtypes": {"A": np.dtype("int8"), "B": np.dtype("float32")}},
223223
np.rec.array(
224224
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
225225
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
226226
),
227227
),
228228
# Mixture of everything.
229229
(
230-
dict(column_dtypes={"A": np.int8, "B": np.float32}, index_dtypes="<U2"),
230+
{
231+
"column_dtypes": {"A": np.int8, "B": np.float32},
232+
"index_dtypes": "<U2",
233+
},
231234
np.rec.array(
232235
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
233236
dtype=[("index", "<U2"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
234237
),
235238
),
236239
# Invalid dype values.
237240
(
238-
dict(index=False, column_dtypes=list()),
241+
{"index": False, "column_dtypes": list()},
239242
(ValueError, "Invalid dtype \\[\\] specified for column A"),
240243
),
241244
(
242-
dict(index=False, column_dtypes={"A": "int32", "B": 5}),
245+
{"index": False, "column_dtypes": {"A": "int32", "B": 5}},
243246
(ValueError, "Invalid dtype 5 specified for column B"),
244247
),
245248
# Numpy can't handle EA types, so check error is raised
246249
(
247-
dict(
248-
index=False,
249-
column_dtypes={"A": "int32", "B": CategoricalDtype(["a", "b"])},
250-
),
250+
{
251+
"index": False,
252+
"column_dtypes": {"A": "int32", "B": CategoricalDtype(["a", "b"])},
253+
},
251254
(ValueError, "Invalid dtype category specified for column B"),
252255
),
253256
# Check that bad types raise
254257
(
255-
dict(index=False, column_dtypes={"A": "int32", "B": "foo"}),
258+
{"index": False, "column_dtypes": {"A": "int32", "B": "foo"}},
256259
(TypeError, "data type [\"']foo[\"'] not understood"),
257260
),
258261
],
@@ -276,7 +279,7 @@ def test_to_records_dtype(self, kwargs, expected):
276279
DataFrame(
277280
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], columns=list("abc")
278281
).set_index(["a", "b"]),
279-
dict(column_dtypes="float64", index_dtypes={0: "int32", 1: "int8"}),
282+
{"column_dtypes": "float64", "index_dtypes": {0: "int32", 1: "int8"}},
280283
np.rec.array(
281284
[(1, 2, 3.0), (4, 5, 6.0), (7, 8, 9.0)],
282285
dtype=[("a", "<i4"), ("b", "i1"), ("c", "<f8")],
@@ -290,7 +293,7 @@ def test_to_records_dtype(self, kwargs, expected):
290293
[("a", "d"), ("b", "e"), ("c", "f")]
291294
),
292295
),
293-
dict(column_dtypes={0: "<U1", 2: "float32"}, index_dtypes="float32"),
296+
{"column_dtypes": {0: "<U1", 2: "float32"}, "index_dtypes": "float32"},
294297
np.rec.array(
295298
[(0.0, "1", 2, 3.0), (1.0, "4", 5, 6.0), (2.0, "7", 8, 9.0)],
296299
dtype=[
@@ -312,7 +315,7 @@ def test_to_records_dtype(self, kwargs, expected):
312315
[("d", -4), ("d", -5), ("f", -6)], names=list("cd")
313316
),
314317
),
315-
dict(column_dtypes="float64", index_dtypes={0: "<U2", 1: "int8"}),
318+
{"column_dtypes": "float64", "index_dtypes": {0: "<U2", 1: "int8"}},
316319
np.rec.array(
317320
[
318321
("d", -4, 1.0, 2.0, 3.0),
@@ -352,10 +355,10 @@ def keys(self):
352355

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

355-
dtype_mappings = dict(
356-
column_dtypes=DictLike(**{"A": np.int8, "B": np.float32}),
357-
index_dtypes="<U2",
358-
)
358+
dtype_mappings = {
359+
"column_dtypes": DictLike(**{"A": np.int8, "B": np.float32}),
360+
"index_dtypes": "<U2",
361+
}
359362

360363
result = df.to_records(**dtype_mappings)
361364
expected = np.rec.array(

0 commit comments

Comments
 (0)