Skip to content

Commit c214e08

Browse files
Fix a few test failures on big-endian systems
These are all due to tests expecting little-endian dtypes, where in fact the endianness of the dtype is that of the host.
1 parent 361021b commit c214e08

File tree

8 files changed

+132
-38
lines changed

8 files changed

+132
-38
lines changed

pandas/tests/arrays/boolean/test_astype.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from sys import byteorder
2+
13
import numpy as np
24
import pytest
35

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

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

2629
# no missing values

pandas/tests/arrays/boolean/test_construction.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from sys import byteorder
2+
13
import numpy as np
24
import pytest
35

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

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

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

pandas/tests/arrays/floating/test_to_numpy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from sys import byteorder
2+
13
import numpy as np
24
import pytest
35

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

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

121124

pandas/tests/arrays/integer/test_dtypes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from sys import byteorder
2+
13
import numpy as np
24
import pytest
35

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

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

288291
tm.assert_numpy_array_equal(a.astype(str), expected)
289292
tm.assert_numpy_array_equal(a.astype("str"), expected)

pandas/tests/frame/methods/test_to_records.py

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

34
import numpy as np
45
import pytest
@@ -14,6 +15,9 @@
1415
import pandas._testing as tm
1516

1617

18+
endian = {"little": "<", "big": ">"}[byteorder]
19+
20+
1721
class TestDataFrameToRecords:
1822
def test_to_records_timeseries(self):
1923
index = date_range("1/1/2000", periods=10)
@@ -151,106 +155,166 @@ def test_to_records_with_categorical(self):
151155
{},
152156
np.rec.array(
153157
[(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+
],
155164
),
156165
),
157166
# Should have no effect in this case.
158167
(
159168
{"index": True},
160169
np.rec.array(
161170
[(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+
],
163177
),
164178
),
165179
# Column dtype applied across the board. Index unaffected.
166180
(
167-
{"column_dtypes": "<U4"},
181+
{"column_dtypes": f"{endian}U4"},
168182
np.rec.array(
169183
[("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+
],
171190
),
172191
),
173192
# Index dtype applied across the board. Columns unaffected.
174193
(
175-
{"index_dtypes": "<U1"},
194+
{"index_dtypes": f"{endian}U1"},
176195
np.rec.array(
177196
[("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+
],
179203
),
180204
),
181205
# Pass in a type instance.
182206
(
183207
{"column_dtypes": str},
184208
np.rec.array(
185209
[("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+
],
187216
),
188217
),
189218
# Pass in a dtype instance.
190219
(
191220
{"column_dtypes": np.dtype("unicode")},
192221
np.rec.array(
193222
[("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+
],
195229
),
196230
),
197231
# Pass in a dictionary (name-only).
198232
(
199-
{"column_dtypes": {"A": np.int8, "B": np.float32, "C": "<U2"}},
233+
{"column_dtypes": {"A": np.int8, "B": np.float32, "C": f"{endian}U2"}},
200234
np.rec.array(
201235
[("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+
],
203242
),
204243
),
205244
# Pass in a dictionary (indices-only).
206245
(
207246
{"index_dtypes": {0: "int16"}},
208247
np.rec.array(
209248
[(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+
],
211255
),
212256
),
213257
# Ignore index mappings if index is not True.
214258
(
215-
{"index": False, "index_dtypes": "<U2"},
259+
{"index": False, "index_dtypes": f"{endian}U2"},
216260
np.rec.array(
217261
[(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")],
219263
),
220264
),
221265
# Non-existent names / indices in mapping should not error.
222266
(
223267
{"index_dtypes": {0: "int16", "not-there": "float32"}},
224268
np.rec.array(
225269
[(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+
],
227276
),
228277
),
229278
# Names / indices not in mapping default to array dtype.
230279
(
231280
{"column_dtypes": {"A": np.int8, "B": np.float32}},
232281
np.rec.array(
233282
[("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+
],
235289
),
236290
),
237291
# Names / indices not in dtype mapping default to array dtype.
238292
(
239293
{"column_dtypes": {"A": np.dtype("int8"), "B": np.dtype("float32")}},
240294
np.rec.array(
241295
[("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+
],
243302
),
244303
),
245304
# Mixture of everything.
246305
(
247306
{
248307
"column_dtypes": {"A": np.int8, "B": np.float32},
249-
"index_dtypes": "<U2",
308+
"index_dtypes": f"{endian}U2",
250309
},
251310
np.rec.array(
252311
[("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+
],
254318
),
255319
),
256320
# Invalid dype values.
@@ -299,7 +363,7 @@ def test_to_records_dtype(self, kwargs, expected):
299363
{"column_dtypes": "float64", "index_dtypes": {0: "int32", 1: "int8"}},
300364
np.rec.array(
301365
[(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")],
303367
),
304368
),
305369
# MultiIndex in the columns.
@@ -310,14 +374,17 @@ def test_to_records_dtype(self, kwargs, expected):
310374
[("a", "d"), ("b", "e"), ("c", "f")]
311375
),
312376
),
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+
},
314381
np.rec.array(
315382
[(0.0, "1", 2, 3.0), (1.0, "4", 5, 6.0), (2.0, "7", 8, 9.0)],
316383
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"),
321388
],
322389
),
323390
),
@@ -332,19 +399,22 @@ def test_to_records_dtype(self, kwargs, expected):
332399
[("d", -4), ("d", -5), ("f", -6)], names=list("cd")
333400
),
334401
),
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+
},
336406
np.rec.array(
337407
[
338408
("d", -4, 1.0, 2.0, 3.0),
339409
("d", -5, 4.0, 5.0, 6.0),
340410
("f", -6, 7, 8, 9.0),
341411
],
342412
dtype=[
343-
("c", "<U2"),
413+
("c", f"{endian}U2"),
344414
("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"),
348418
],
349419
),
350420
),
@@ -374,13 +444,18 @@ def keys(self):
374444

375445
dtype_mappings = {
376446
"column_dtypes": DictLike(**{"A": np.int8, "B": np.float32}),
377-
"index_dtypes": "<U2",
447+
"index_dtypes": f"{endian}U2",
378448
}
379449

380450
result = df.to_records(**dtype_mappings)
381451
expected = np.rec.array(
382452
[("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+
],
384459
)
385460
tm.assert_almost_equal(result, expected)
386461

pandas/tests/io/parser/test_c_parser_only.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
import mmap
1414
import os
15+
from sys import byteorder
1516
import tarfile
1617

1718
import numpy as np
@@ -31,6 +32,9 @@
3132
import pandas._testing as tm
3233

3334

35+
endian = {"little": "<", "big": ">"}[byteorder]
36+
37+
3438
@pytest.mark.parametrize(
3539
"malformed",
3640
["1\r1\r1\r 1\r 1\r", "1\r1\r1\r 1\r 1\r11\r", "1\r1\r1\r 1\r 1\r11\r1\r"],
@@ -144,9 +148,9 @@ def test_dtype_and_names_error(c_parser_only):
144148
"the dtype timedelta64 is not supported for parsing",
145149
{"dtype": {"A": "timedelta64", "B": "float64"}},
146150
),
147-
("the dtype <U8 is not supported for parsing", {"dtype": {"A": "U8"}}),
151+
(f"the dtype {endian}U8 is not supported for parsing", {"dtype": {"A": "U8"}}),
148152
],
149-
ids=["dt64-0", "dt64-1", "td64", "<U8"],
153+
ids=["dt64-0", "dt64-1", "td64", f"{endian}U8"],
150154
)
151155
def test_unsupported_dtype(c_parser_only, match, kwargs):
152156
parser = c_parser_only

pandas/tests/scalar/timedelta/test_arithmetic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
timedelta,
77
)
88
import operator
9+
from sys import byteorder
910

1011
import numpy as np
1112
import pytest

0 commit comments

Comments
 (0)