@@ -131,128 +131,131 @@ def test_to_records_with_categorical(self):
131
131
[
132
132
# No dtypes --> default to array dtypes.
133
133
(
134
- dict () ,
134
+ {} ,
135
135
np .rec .array (
136
136
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
137
137
dtype = [("index" , "<i8" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
138
138
),
139
139
),
140
140
# Should have no effect in this case.
141
141
(
142
- dict ( index = True ) ,
142
+ { " index" : True } ,
143
143
np .rec .array (
144
144
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
145
145
dtype = [("index" , "<i8" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
146
146
),
147
147
),
148
148
# Column dtype applied across the board. Index unaffected.
149
149
(
150
- dict ( column_dtypes = " <U4") ,
150
+ { " column_dtypes" : " <U4"} ,
151
151
np .rec .array (
152
152
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
153
153
dtype = [("index" , "<i8" ), ("A" , "<U4" ), ("B" , "<U4" ), ("C" , "<U4" )],
154
154
),
155
155
),
156
156
# Index dtype applied across the board. Columns unaffected.
157
157
(
158
- dict ( index_dtypes = " <U1") ,
158
+ { " index_dtypes" : " <U1"} ,
159
159
np .rec .array (
160
160
[("0" , 1 , 0.2 , "a" ), ("1" , 2 , 1.5 , "bc" )],
161
161
dtype = [("index" , "<U1" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
162
162
),
163
163
),
164
164
# Pass in a type instance.
165
165
(
166
- dict ( column_dtypes = str ) ,
166
+ { " column_dtypes" : str } ,
167
167
np .rec .array (
168
168
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
169
169
dtype = [("index" , "<i8" ), ("A" , "<U" ), ("B" , "<U" ), ("C" , "<U" )],
170
170
),
171
171
),
172
172
# Pass in a dtype instance.
173
173
(
174
- dict ( column_dtypes = np .dtype ("unicode" )) ,
174
+ { " column_dtypes" : np .dtype ("unicode" )} ,
175
175
np .rec .array (
176
176
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
177
177
dtype = [("index" , "<i8" ), ("A" , "<U" ), ("B" , "<U" ), ("C" , "<U" )],
178
178
),
179
179
),
180
180
# Pass in a dictionary (name-only).
181
181
(
182
- dict ( column_dtypes = {"A" : np .int8 , "B" : np .float32 , "C" : "<U2" }) ,
182
+ { " column_dtypes" : {"A" : np .int8 , "B" : np .float32 , "C" : "<U2" }} ,
183
183
np .rec .array (
184
184
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
185
185
dtype = [("index" , "<i8" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "<U2" )],
186
186
),
187
187
),
188
188
# Pass in a dictionary (indices-only).
189
189
(
190
- dict ( index_dtypes = {0 : "int16" }) ,
190
+ { " index_dtypes" : {0 : "int16" }} ,
191
191
np .rec .array (
192
192
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
193
193
dtype = [("index" , "i2" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
194
194
),
195
195
),
196
196
# Ignore index mappings if index is not True.
197
197
(
198
- dict ( index = False , index_dtypes = " <U2") ,
198
+ { " index" : False , " index_dtypes" : " <U2"} ,
199
199
np .rec .array (
200
200
[(1 , 0.2 , "a" ), (2 , 1.5 , "bc" )],
201
201
dtype = [("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
202
202
),
203
203
),
204
204
# Non-existent names / indices in mapping should not error.
205
205
(
206
- dict ( index_dtypes = {0 : "int16" , "not-there" : "float32" }) ,
206
+ { " index_dtypes" : {0 : "int16" , "not-there" : "float32" }} ,
207
207
np .rec .array (
208
208
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
209
209
dtype = [("index" , "i2" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
210
210
),
211
211
),
212
212
# Names / indices not in mapping default to array dtype.
213
213
(
214
- dict ( column_dtypes = {"A" : np .int8 , "B" : np .float32 }) ,
214
+ { " column_dtypes" : {"A" : np .int8 , "B" : np .float32 }} ,
215
215
np .rec .array (
216
216
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
217
217
dtype = [("index" , "<i8" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "O" )],
218
218
),
219
219
),
220
220
# Names / indices not in dtype mapping default to array dtype.
221
221
(
222
- dict ( column_dtypes = {"A" : np .dtype ("int8" ), "B" : np .dtype ("float32" )}) ,
222
+ { " column_dtypes" : {"A" : np .dtype ("int8" ), "B" : np .dtype ("float32" )}} ,
223
223
np .rec .array (
224
224
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
225
225
dtype = [("index" , "<i8" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "O" )],
226
226
),
227
227
),
228
228
# Mixture of everything.
229
229
(
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
+ },
231
234
np .rec .array (
232
235
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
233
236
dtype = [("index" , "<U2" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "O" )],
234
237
),
235
238
),
236
239
# Invalid dype values.
237
240
(
238
- dict ( index = False , column_dtypes = list ()) ,
241
+ { " index" : False , " column_dtypes" : list ()} ,
239
242
(ValueError , "Invalid dtype \\ [\\ ] specified for column A" ),
240
243
),
241
244
(
242
- dict ( index = False , column_dtypes = {"A" : "int32" , "B" : 5 }) ,
245
+ { " index" : False , " column_dtypes" : {"A" : "int32" , "B" : 5 }} ,
243
246
(ValueError , "Invalid dtype 5 specified for column B" ),
244
247
),
245
248
# Numpy can't handle EA types, so check error is raised
246
249
(
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
+ } ,
251
254
(ValueError , "Invalid dtype category specified for column B" ),
252
255
),
253
256
# Check that bad types raise
254
257
(
255
- dict ( index = False , column_dtypes = {"A" : "int32" , "B" : "foo" }) ,
258
+ { " index" : False , " column_dtypes" : {"A" : "int32" , "B" : "foo" }} ,
256
259
(TypeError , "data type [\" ']foo[\" '] not understood" ),
257
260
),
258
261
],
@@ -276,7 +279,7 @@ def test_to_records_dtype(self, kwargs, expected):
276
279
DataFrame (
277
280
[[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]], columns = list ("abc" )
278
281
).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" }} ,
280
283
np .rec .array (
281
284
[(1 , 2 , 3.0 ), (4 , 5 , 6.0 ), (7 , 8 , 9.0 )],
282
285
dtype = [("a" , "<i4" ), ("b" , "i1" ), ("c" , "<f8" )],
@@ -290,7 +293,7 @@ def test_to_records_dtype(self, kwargs, expected):
290
293
[("a" , "d" ), ("b" , "e" ), ("c" , "f" )]
291
294
),
292
295
),
293
- dict ( column_dtypes = {0 : "<U1" , 2 : "float32" }, index_dtypes = " float32") ,
296
+ { " column_dtypes" : {0 : "<U1" , 2 : "float32" }, " index_dtypes" : " float32"} ,
294
297
np .rec .array (
295
298
[(0.0 , "1" , 2 , 3.0 ), (1.0 , "4" , 5 , 6.0 ), (2.0 , "7" , 8 , 9.0 )],
296
299
dtype = [
@@ -312,7 +315,7 @@ def test_to_records_dtype(self, kwargs, expected):
312
315
[("d" , - 4 ), ("d" , - 5 ), ("f" , - 6 )], names = list ("cd" )
313
316
),
314
317
),
315
- dict ( column_dtypes = " float64" , index_dtypes = {0 : "<U2" , 1 : "int8" }) ,
318
+ { " column_dtypes" : " float64" , " index_dtypes" : {0 : "<U2" , 1 : "int8" }} ,
316
319
np .rec .array (
317
320
[
318
321
("d" , - 4 , 1.0 , 2.0 , 3.0 ),
@@ -352,10 +355,10 @@ def keys(self):
352
355
353
356
df = DataFrame ({"A" : [1 , 2 ], "B" : [0.2 , 1.5 ], "C" : ["a" , "bc" ]})
354
357
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
+ }
359
362
360
363
result = df .to_records (** dtype_mappings )
361
364
expected = np .rec .array (
0 commit comments