1
1
from collections import abc
2
+ from sys import byteorder
2
3
3
4
import numpy as np
4
5
import pytest
14
15
import pandas ._testing as tm
15
16
16
17
18
+ endian = {"little" : "<" , "big" : ">" }[byteorder ]
19
+
20
+
17
21
class TestDataFrameToRecords :
18
22
def test_to_records_timeseries (self ):
19
23
index = date_range ("1/1/2000" , periods = 10 )
@@ -151,106 +155,166 @@ def test_to_records_with_categorical(self):
151
155
{},
152
156
np .rec .array (
153
157
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
154
- dtype = [("index" , "<i8" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
158
+ dtype = [
159
+ ("index" , f"{ endian } i8" ),
160
+ ("A" , f"{ endian } i8" ),
161
+ ("B" , f"{ endian } f8" ),
162
+ ("C" , "O" ),
163
+ ],
155
164
),
156
165
),
157
166
# Should have no effect in this case.
158
167
(
159
168
{"index" : True },
160
169
np .rec .array (
161
170
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
162
- dtype = [("index" , "<i8" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
171
+ dtype = [
172
+ ("index" , f"{ endian } i8" ),
173
+ ("A" , f"{ endian } i8" ),
174
+ ("B" , f"{ endian } f8" ),
175
+ ("C" , "O" ),
176
+ ],
163
177
),
164
178
),
165
179
# Column dtype applied across the board. Index unaffected.
166
180
(
167
- {"column_dtypes" : "< U4" },
181
+ {"column_dtypes" : f" { endian } U4" },
168
182
np .rec .array (
169
183
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
170
- dtype = [("index" , "<i8" ), ("A" , "<U4" ), ("B" , "<U4" ), ("C" , "<U4" )],
184
+ dtype = [
185
+ ("index" , f"{ endian } i8" ),
186
+ ("A" , f"{ endian } U4" ),
187
+ ("B" , f"{ endian } U4" ),
188
+ ("C" , f"{ endian } U4" ),
189
+ ],
171
190
),
172
191
),
173
192
# Index dtype applied across the board. Columns unaffected.
174
193
(
175
- {"index_dtypes" : "< U1" },
194
+ {"index_dtypes" : f" { endian } U1" },
176
195
np .rec .array (
177
196
[("0" , 1 , 0.2 , "a" ), ("1" , 2 , 1.5 , "bc" )],
178
- dtype = [("index" , "<U1" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
197
+ dtype = [
198
+ ("index" , f"{ endian } U1" ),
199
+ ("A" , f"{ endian } i8" ),
200
+ ("B" , f"{ endian } f8" ),
201
+ ("C" , "O" ),
202
+ ],
179
203
),
180
204
),
181
205
# Pass in a type instance.
182
206
(
183
207
{"column_dtypes" : str },
184
208
np .rec .array (
185
209
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
186
- dtype = [("index" , "<i8" ), ("A" , "<U" ), ("B" , "<U" ), ("C" , "<U" )],
210
+ dtype = [
211
+ ("index" , f"{ endian } i8" ),
212
+ ("A" , f"{ endian } U" ),
213
+ ("B" , f"{ endian } U" ),
214
+ ("C" , f"{ endian } U" ),
215
+ ],
187
216
),
188
217
),
189
218
# Pass in a dtype instance.
190
219
(
191
220
{"column_dtypes" : np .dtype ("unicode" )},
192
221
np .rec .array (
193
222
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
194
- dtype = [("index" , "<i8" ), ("A" , "<U" ), ("B" , "<U" ), ("C" , "<U" )],
223
+ dtype = [
224
+ ("index" , f"{ endian } i8" ),
225
+ ("A" , f"{ endian } U" ),
226
+ ("B" , f"{ endian } U" ),
227
+ ("C" , f"{ endian } U" ),
228
+ ],
195
229
),
196
230
),
197
231
# Pass in a dictionary (name-only).
198
232
(
199
- {"column_dtypes" : {"A" : np .int8 , "B" : np .float32 , "C" : "< U2" }},
233
+ {"column_dtypes" : {"A" : np .int8 , "B" : np .float32 , "C" : f" { endian } U2" }},
200
234
np .rec .array (
201
235
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
202
- dtype = [("index" , "<i8" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "<U2" )],
236
+ dtype = [
237
+ ("index" , f"{ endian } i8" ),
238
+ ("A" , "i1" ),
239
+ ("B" , f"{ endian } f4" ),
240
+ ("C" , f"{ endian } U2" ),
241
+ ],
203
242
),
204
243
),
205
244
# Pass in a dictionary (indices-only).
206
245
(
207
246
{"index_dtypes" : {0 : "int16" }},
208
247
np .rec .array (
209
248
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
210
- dtype = [("index" , "i2" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
249
+ dtype = [
250
+ ("index" , "i2" ),
251
+ ("A" , f"{ endian } i8" ),
252
+ ("B" , f"{ endian } f8" ),
253
+ ("C" , "O" ),
254
+ ],
211
255
),
212
256
),
213
257
# Ignore index mappings if index is not True.
214
258
(
215
- {"index" : False , "index_dtypes" : "< U2" },
259
+ {"index" : False , "index_dtypes" : f" { endian } U2" },
216
260
np .rec .array (
217
261
[(1 , 0.2 , "a" ), (2 , 1.5 , "bc" )],
218
- dtype = [("A" , "< i8" ), ("B" , "< f8" ), ("C" , "O" )],
262
+ dtype = [("A" , f" { endian } i8" ), ("B" , f" { endian } f8" ), ("C" , "O" )],
219
263
),
220
264
),
221
265
# Non-existent names / indices in mapping should not error.
222
266
(
223
267
{"index_dtypes" : {0 : "int16" , "not-there" : "float32" }},
224
268
np .rec .array (
225
269
[(0 , 1 , 0.2 , "a" ), (1 , 2 , 1.5 , "bc" )],
226
- dtype = [("index" , "i2" ), ("A" , "<i8" ), ("B" , "<f8" ), ("C" , "O" )],
270
+ dtype = [
271
+ ("index" , "i2" ),
272
+ ("A" , f"{ endian } i8" ),
273
+ ("B" , f"{ endian } f8" ),
274
+ ("C" , "O" ),
275
+ ],
227
276
),
228
277
),
229
278
# Names / indices not in mapping default to array dtype.
230
279
(
231
280
{"column_dtypes" : {"A" : np .int8 , "B" : np .float32 }},
232
281
np .rec .array (
233
282
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
234
- dtype = [("index" , "<i8" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "O" )],
283
+ dtype = [
284
+ ("index" , f"{ endian } i8" ),
285
+ ("A" , "i1" ),
286
+ ("B" , f"{ endian } f4" ),
287
+ ("C" , "O" ),
288
+ ],
235
289
),
236
290
),
237
291
# Names / indices not in dtype mapping default to array dtype.
238
292
(
239
293
{"column_dtypes" : {"A" : np .dtype ("int8" ), "B" : np .dtype ("float32" )}},
240
294
np .rec .array (
241
295
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
242
- dtype = [("index" , "<i8" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "O" )],
296
+ dtype = [
297
+ ("index" , f"{ endian } i8" ),
298
+ ("A" , "i1" ),
299
+ ("B" , f"{ endian } f4" ),
300
+ ("C" , "O" ),
301
+ ],
243
302
),
244
303
),
245
304
# Mixture of everything.
246
305
(
247
306
{
248
307
"column_dtypes" : {"A" : np .int8 , "B" : np .float32 },
249
- "index_dtypes" : "< U2" ,
308
+ "index_dtypes" : f" { endian } U2" ,
250
309
},
251
310
np .rec .array (
252
311
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
253
- dtype = [("index" , "<U2" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "O" )],
312
+ dtype = [
313
+ ("index" , f"{ endian } U2" ),
314
+ ("A" , "i1" ),
315
+ ("B" , f"{ endian } f4" ),
316
+ ("C" , "O" ),
317
+ ],
254
318
),
255
319
),
256
320
# Invalid dype values.
@@ -299,7 +363,7 @@ def test_to_records_dtype(self, kwargs, expected):
299
363
{"column_dtypes" : "float64" , "index_dtypes" : {0 : "int32" , 1 : "int8" }},
300
364
np .rec .array (
301
365
[(1 , 2 , 3.0 ), (4 , 5 , 6.0 ), (7 , 8 , 9.0 )],
302
- dtype = [("a" , "< i4" ), ("b" , "i1" ), ("c" , "< f8" )],
366
+ dtype = [("a" , f" { endian } i4" ), ("b" , "i1" ), ("c" , f" { endian } f8" )],
303
367
),
304
368
),
305
369
# MultiIndex in the columns.
@@ -310,14 +374,17 @@ def test_to_records_dtype(self, kwargs, expected):
310
374
[("a" , "d" ), ("b" , "e" ), ("c" , "f" )]
311
375
),
312
376
),
313
- {"column_dtypes" : {0 : "<U1" , 2 : "float32" }, "index_dtypes" : "float32" },
377
+ {
378
+ "column_dtypes" : {0 : f"{ endian } U1" , 2 : "float32" },
379
+ "index_dtypes" : "float32" ,
380
+ },
314
381
np .rec .array (
315
382
[(0.0 , "1" , 2 , 3.0 ), (1.0 , "4" , 5 , 6.0 ), (2.0 , "7" , 8 , 9.0 )],
316
383
dtype = [
317
- ("index" , "< f4" ),
318
- ("('a', 'd')" , "< U1" ),
319
- ("('b', 'e')" , "< i8" ),
320
- ("('c', 'f')" , "< f4" ),
384
+ ("index" , f" { endian } f4" ),
385
+ ("('a', 'd')" , f" { endian } U1" ),
386
+ ("('b', 'e')" , f" { endian } i8" ),
387
+ ("('c', 'f')" , f" { endian } f4" ),
321
388
],
322
389
),
323
390
),
@@ -332,19 +399,22 @@ def test_to_records_dtype(self, kwargs, expected):
332
399
[("d" , - 4 ), ("d" , - 5 ), ("f" , - 6 )], names = list ("cd" )
333
400
),
334
401
),
335
- {"column_dtypes" : "float64" , "index_dtypes" : {0 : "<U2" , 1 : "int8" }},
402
+ {
403
+ "column_dtypes" : "float64" ,
404
+ "index_dtypes" : {0 : f"{ endian } U2" , 1 : "int8" },
405
+ },
336
406
np .rec .array (
337
407
[
338
408
("d" , - 4 , 1.0 , 2.0 , 3.0 ),
339
409
("d" , - 5 , 4.0 , 5.0 , 6.0 ),
340
410
("f" , - 6 , 7 , 8 , 9.0 ),
341
411
],
342
412
dtype = [
343
- ("c" , "< U2" ),
413
+ ("c" , f" { endian } U2" ),
344
414
("d" , "i1" ),
345
- ("('a', 'd')" , "< f8" ),
346
- ("('b', 'e')" , "< f8" ),
347
- ("('c', 'f')" , "< f8" ),
415
+ ("('a', 'd')" , f" { endian } f8" ),
416
+ ("('b', 'e')" , f" { endian } f8" ),
417
+ ("('c', 'f')" , f" { endian } f8" ),
348
418
],
349
419
),
350
420
),
@@ -374,13 +444,18 @@ def keys(self):
374
444
375
445
dtype_mappings = {
376
446
"column_dtypes" : DictLike (** {"A" : np .int8 , "B" : np .float32 }),
377
- "index_dtypes" : "< U2" ,
447
+ "index_dtypes" : f" { endian } U2" ,
378
448
}
379
449
380
450
result = df .to_records (** dtype_mappings )
381
451
expected = np .rec .array (
382
452
[("0" , "1" , "0.2" , "a" ), ("1" , "2" , "1.5" , "bc" )],
383
- dtype = [("index" , "<U2" ), ("A" , "i1" ), ("B" , "<f4" ), ("C" , "O" )],
453
+ dtype = [
454
+ ("index" , f"{ endian } U2" ),
455
+ ("A" , "i1" ),
456
+ ("B" , f"{ endian } f4" ),
457
+ ("C" , "O" ),
458
+ ],
384
459
)
385
460
tm .assert_almost_equal (result , expected )
386
461
0 commit comments