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