Skip to content

Commit 44ba9e6

Browse files
Set ENDIAN in pandas._testing
Setting an ENDIAN constant here to represent the character for host-endianness to use for numpy dtypes ("<" or ">") saves an import and a little duplicated code across a number of test modules.
1 parent c214e08 commit 44ba9e6

File tree

8 files changed

+82
-83
lines changed

8 files changed

+82
-83
lines changed

pandas/_testing/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import re
1010
import string
11+
from sys import byteorder
1112
from typing import (
1213
TYPE_CHECKING,
1314
Callable,
@@ -168,6 +169,8 @@
168169
np.uint32,
169170
]
170171

172+
ENDIAN = {"little": "<", "big": ">"}[byteorder]
173+
171174
NULL_OBJECTS = [None, np.nan, pd.NaT, float("nan"), pd.NA, Decimal("NaN")]
172175
NP_NAT_OBJECTS = [
173176
cls("NaT", unit)

pandas/tests/arrays/boolean/test_astype.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from sys import byteorder
2-
31
import numpy as np
42
import pytest
53

@@ -22,8 +20,7 @@ def test_astype():
2220
tm.assert_numpy_array_equal(result, expected)
2321

2422
result = arr.astype("str")
25-
endian = {"little": "<", "big": ">"}[byteorder]
26-
expected = np.array(["True", "False", "<NA>"], dtype=f"{endian}U5")
23+
expected = np.array(["True", "False", "<NA>"], dtype=f"{tm.ENDIAN}U5")
2724
tm.assert_numpy_array_equal(result, expected)
2825

2926
# no missing values

pandas/tests/arrays/boolean/test_construction.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from sys import byteorder
2-
31
import numpy as np
42
import pytest
53

@@ -275,8 +273,7 @@ def test_to_numpy(box):
275273

276274
arr = con([True, False, None], dtype="boolean")
277275
result = arr.to_numpy(dtype="str")
278-
endian = {"little": "<", "big": ">"}[byteorder]
279-
expected = np.array([True, False, pd.NA], dtype=f"{endian}U5")
276+
expected = np.array([True, False, pd.NA], dtype=f"{tm.ENDIAN}U5")
280277
tm.assert_numpy_array_equal(result, expected)
281278

282279
# no missing values -> can convert to bool, otherwise raises

pandas/tests/arrays/floating/test_to_numpy.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from sys import byteorder
2-
31
import numpy as np
42
import pytest
53

@@ -117,8 +115,7 @@ def test_to_numpy_string(box, dtype):
117115
arr = con([0.0, 1.0, None], dtype="Float64")
118116

119117
result = arr.to_numpy(dtype="str")
120-
endian = {"little": "<", "big": ">"}[byteorder]
121-
expected = np.array([0.0, 1.0, pd.NA], dtype=f"{endian}U32")
118+
expected = np.array([0.0, 1.0, pd.NA], dtype=f"{tm.ENDIAN}U32")
122119
tm.assert_numpy_array_equal(result, expected)
123120

124121

pandas/tests/arrays/integer/test_dtypes.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from sys import byteorder
2-
31
import numpy as np
42
import pytest
53

@@ -285,8 +283,7 @@ def test_to_numpy_na_raises(dtype):
285283

286284
def test_astype_str():
287285
a = pd.array([1, 2, None], dtype="Int64")
288-
endian = {"little": "<", "big": ">"}[byteorder]
289-
expected = np.array(["1", "2", "<NA>"], dtype=f"{endian}U21")
286+
expected = np.array(["1", "2", "<NA>"], dtype=f"{tm.ENDIAN}U21")
290287

291288
tm.assert_numpy_array_equal(a.astype(str), expected)
292289
tm.assert_numpy_array_equal(a.astype("str"), expected)

pandas/tests/frame/methods/test_to_records.py

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from collections import abc
2-
from sys import byteorder
32

43
import numpy as np
54
import pytest
@@ -15,9 +14,6 @@
1514
import pandas._testing as tm
1615

1716

18-
endian = {"little": "<", "big": ">"}[byteorder]
19-
20-
2117
class TestDataFrameToRecords:
2218
def test_to_records_timeseries(self):
2319
index = date_range("1/1/2000", periods=10)
@@ -156,9 +152,9 @@ def test_to_records_with_categorical(self):
156152
np.rec.array(
157153
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
158154
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"),
162158
("C", "O"),
163159
],
164160
),
@@ -169,35 +165,35 @@ def test_to_records_with_categorical(self):
169165
np.rec.array(
170166
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
171167
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"),
175171
("C", "O"),
176172
],
177173
),
178174
),
179175
# Column dtype applied across the board. Index unaffected.
180176
(
181-
{"column_dtypes": f"{endian}U4"},
177+
{"column_dtypes": f"{tm.ENDIAN}U4"},
182178
np.rec.array(
183179
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
184180
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"),
189185
],
190186
),
191187
),
192188
# Index dtype applied across the board. Columns unaffected.
193189
(
194-
{"index_dtypes": f"{endian}U1"},
190+
{"index_dtypes": f"{tm.ENDIAN}U1"},
195191
np.rec.array(
196192
[("0", 1, 0.2, "a"), ("1", 2, 1.5, "bc")],
197193
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"),
201197
("C", "O"),
202198
],
203199
),
@@ -208,10 +204,10 @@ def test_to_records_with_categorical(self):
208204
np.rec.array(
209205
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
210206
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"),
215211
],
216212
),
217213
),
@@ -221,23 +217,29 @@ def test_to_records_with_categorical(self):
221217
np.rec.array(
222218
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
223219
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"),
228224
],
229225
),
230226
),
231227
# Pass in a dictionary (name-only).
232228
(
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+
},
234236
np.rec.array(
235237
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
236238
dtype=[
237-
("index", f"{endian}i8"),
239+
("index", f"{tm.ENDIAN}i8"),
238240
("A", "i1"),
239-
("B", f"{endian}f4"),
240-
("C", f"{endian}U2"),
241+
("B", f"{tm.ENDIAN}f4"),
242+
("C", f"{tm.ENDIAN}U2"),
241243
],
242244
),
243245
),
@@ -248,18 +250,22 @@ def test_to_records_with_categorical(self):
248250
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
249251
dtype=[
250252
("index", "i2"),
251-
("A", f"{endian}i8"),
252-
("B", f"{endian}f8"),
253+
("A", f"{tm.ENDIAN}i8"),
254+
("B", f"{tm.ENDIAN}f8"),
253255
("C", "O"),
254256
],
255257
),
256258
),
257259
# Ignore index mappings if index is not True.
258260
(
259-
{"index": False, "index_dtypes": f"{endian}U2"},
261+
{"index": False, "index_dtypes": f"{tm.ENDIAN}U2"},
260262
np.rec.array(
261263
[(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+
],
263269
),
264270
),
265271
# Non-existent names / indices in mapping should not error.
@@ -269,8 +275,8 @@ def test_to_records_with_categorical(self):
269275
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
270276
dtype=[
271277
("index", "i2"),
272-
("A", f"{endian}i8"),
273-
("B", f"{endian}f8"),
278+
("A", f"{tm.ENDIAN}i8"),
279+
("B", f"{tm.ENDIAN}f8"),
274280
("C", "O"),
275281
],
276282
),
@@ -281,9 +287,9 @@ def test_to_records_with_categorical(self):
281287
np.rec.array(
282288
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
283289
dtype=[
284-
("index", f"{endian}i8"),
290+
("index", f"{tm.ENDIAN}i8"),
285291
("A", "i1"),
286-
("B", f"{endian}f4"),
292+
("B", f"{tm.ENDIAN}f4"),
287293
("C", "O"),
288294
],
289295
),
@@ -294,9 +300,9 @@ def test_to_records_with_categorical(self):
294300
np.rec.array(
295301
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
296302
dtype=[
297-
("index", f"{endian}i8"),
303+
("index", f"{tm.ENDIAN}i8"),
298304
("A", "i1"),
299-
("B", f"{endian}f4"),
305+
("B", f"{tm.ENDIAN}f4"),
300306
("C", "O"),
301307
],
302308
),
@@ -305,14 +311,14 @@ def test_to_records_with_categorical(self):
305311
(
306312
{
307313
"column_dtypes": {"A": np.int8, "B": np.float32},
308-
"index_dtypes": f"{endian}U2",
314+
"index_dtypes": f"{tm.ENDIAN}U2",
309315
},
310316
np.rec.array(
311317
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
312318
dtype=[
313-
("index", f"{endian}U2"),
319+
("index", f"{tm.ENDIAN}U2"),
314320
("A", "i1"),
315-
("B", f"{endian}f4"),
321+
("B", f"{tm.ENDIAN}f4"),
316322
("C", "O"),
317323
],
318324
),
@@ -363,7 +369,11 @@ def test_to_records_dtype(self, kwargs, expected):
363369
{"column_dtypes": "float64", "index_dtypes": {0: "int32", 1: "int8"}},
364370
np.rec.array(
365371
[(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+
],
367377
),
368378
),
369379
# MultiIndex in the columns.
@@ -375,16 +385,16 @@ def test_to_records_dtype(self, kwargs, expected):
375385
),
376386
),
377387
{
378-
"column_dtypes": {0: f"{endian}U1", 2: "float32"},
388+
"column_dtypes": {0: f"{tm.ENDIAN}U1", 2: "float32"},
379389
"index_dtypes": "float32",
380390
},
381391
np.rec.array(
382392
[(0.0, "1", 2, 3.0), (1.0, "4", 5, 6.0), (2.0, "7", 8, 9.0)],
383393
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"),
388398
],
389399
),
390400
),
@@ -401,7 +411,7 @@ def test_to_records_dtype(self, kwargs, expected):
401411
),
402412
{
403413
"column_dtypes": "float64",
404-
"index_dtypes": {0: f"{endian}U2", 1: "int8"},
414+
"index_dtypes": {0: f"{tm.ENDIAN}U2", 1: "int8"},
405415
},
406416
np.rec.array(
407417
[
@@ -410,11 +420,11 @@ def test_to_records_dtype(self, kwargs, expected):
410420
("f", -6, 7, 8, 9.0),
411421
],
412422
dtype=[
413-
("c", f"{endian}U2"),
423+
("c", f"{tm.ENDIAN}U2"),
414424
("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"),
418428
],
419429
),
420430
),
@@ -444,16 +454,16 @@ def keys(self):
444454

445455
dtype_mappings = {
446456
"column_dtypes": DictLike(**{"A": np.int8, "B": np.float32}),
447-
"index_dtypes": f"{endian}U2",
457+
"index_dtypes": f"{tm.ENDIAN}U2",
448458
}
449459

450460
result = df.to_records(**dtype_mappings)
451461
expected = np.rec.array(
452462
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
453463
dtype=[
454-
("index", f"{endian}U2"),
464+
("index", f"{tm.ENDIAN}U2"),
455465
("A", "i1"),
456-
("B", f"{endian}f4"),
466+
("B", f"{tm.ENDIAN}f4"),
457467
("C", "O"),
458468
],
459469
)

0 commit comments

Comments
 (0)