Skip to content

Commit 40e9cbe

Browse files
Fix a few test failures on big-endian systems (pandas-dev#46681)
1 parent 361021b commit 40e9cbe

File tree

8 files changed

+130
-38
lines changed

8 files changed

+130
-38
lines changed

pandas/_testing/__init__.py

+3
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_astype():
2020
tm.assert_numpy_array_equal(result, expected)
2121

2222
result = arr.astype("str")
23-
expected = np.array(["True", "False", "<NA>"], dtype="<U5")
23+
expected = np.array(["True", "False", "<NA>"], dtype=f"{tm.ENDIAN}U5")
2424
tm.assert_numpy_array_equal(result, expected)
2525

2626
# no missing values

pandas/tests/arrays/boolean/test_construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def test_to_numpy(box):
273273

274274
arr = con([True, False, None], dtype="boolean")
275275
result = arr.to_numpy(dtype="str")
276-
expected = np.array([True, False, pd.NA], dtype="<U5")
276+
expected = np.array([True, False, pd.NA], dtype=f"{tm.ENDIAN}U5")
277277
tm.assert_numpy_array_equal(result, expected)
278278

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

pandas/tests/arrays/floating/test_to_numpy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_to_numpy_string(box, dtype):
115115
arr = con([0.0, 1.0, None], dtype="Float64")
116116

117117
result = arr.to_numpy(dtype="str")
118-
expected = np.array([0.0, 1.0, pd.NA], dtype="<U32")
118+
expected = np.array([0.0, 1.0, pd.NA], dtype=f"{tm.ENDIAN}U32")
119119
tm.assert_numpy_array_equal(result, expected)
120120

121121

pandas/tests/arrays/integer/test_dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def test_to_numpy_na_raises(dtype):
283283

284284
def test_astype_str():
285285
a = pd.array([1, 2, None], dtype="Int64")
286-
expected = np.array(["1", "2", "<NA>"], dtype="<U21")
286+
expected = np.array(["1", "2", "<NA>"], dtype=f"{tm.ENDIAN}U21")
287287

288288
tm.assert_numpy_array_equal(a.astype(str), expected)
289289
tm.assert_numpy_array_equal(a.astype("str"), expected)

pandas/tests/frame/methods/test_to_records.py

+116-31
Original file line numberDiff line numberDiff line change
@@ -151,106 +151,176 @@ def test_to_records_with_categorical(self):
151151
{},
152152
np.rec.array(
153153
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
154-
dtype=[("index", "<i8"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
154+
dtype=[
155+
("index", f"{tm.ENDIAN}i8"),
156+
("A", f"{tm.ENDIAN}i8"),
157+
("B", f"{tm.ENDIAN}f8"),
158+
("C", "O"),
159+
],
155160
),
156161
),
157162
# Should have no effect in this case.
158163
(
159164
{"index": True},
160165
np.rec.array(
161166
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
162-
dtype=[("index", "<i8"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
167+
dtype=[
168+
("index", f"{tm.ENDIAN}i8"),
169+
("A", f"{tm.ENDIAN}i8"),
170+
("B", f"{tm.ENDIAN}f8"),
171+
("C", "O"),
172+
],
163173
),
164174
),
165175
# Column dtype applied across the board. Index unaffected.
166176
(
167-
{"column_dtypes": "<U4"},
177+
{"column_dtypes": f"{tm.ENDIAN}U4"},
168178
np.rec.array(
169179
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
170-
dtype=[("index", "<i8"), ("A", "<U4"), ("B", "<U4"), ("C", "<U4")],
180+
dtype=[
181+
("index", f"{tm.ENDIAN}i8"),
182+
("A", f"{tm.ENDIAN}U4"),
183+
("B", f"{tm.ENDIAN}U4"),
184+
("C", f"{tm.ENDIAN}U4"),
185+
],
171186
),
172187
),
173188
# Index dtype applied across the board. Columns unaffected.
174189
(
175-
{"index_dtypes": "<U1"},
190+
{"index_dtypes": f"{tm.ENDIAN}U1"},
176191
np.rec.array(
177192
[("0", 1, 0.2, "a"), ("1", 2, 1.5, "bc")],
178-
dtype=[("index", "<U1"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
193+
dtype=[
194+
("index", f"{tm.ENDIAN}U1"),
195+
("A", f"{tm.ENDIAN}i8"),
196+
("B", f"{tm.ENDIAN}f8"),
197+
("C", "O"),
198+
],
179199
),
180200
),
181201
# Pass in a type instance.
182202
(
183203
{"column_dtypes": str},
184204
np.rec.array(
185205
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
186-
dtype=[("index", "<i8"), ("A", "<U"), ("B", "<U"), ("C", "<U")],
206+
dtype=[
207+
("index", f"{tm.ENDIAN}i8"),
208+
("A", f"{tm.ENDIAN}U"),
209+
("B", f"{tm.ENDIAN}U"),
210+
("C", f"{tm.ENDIAN}U"),
211+
],
187212
),
188213
),
189214
# Pass in a dtype instance.
190215
(
191216
{"column_dtypes": np.dtype("unicode")},
192217
np.rec.array(
193218
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
194-
dtype=[("index", "<i8"), ("A", "<U"), ("B", "<U"), ("C", "<U")],
219+
dtype=[
220+
("index", f"{tm.ENDIAN}i8"),
221+
("A", f"{tm.ENDIAN}U"),
222+
("B", f"{tm.ENDIAN}U"),
223+
("C", f"{tm.ENDIAN}U"),
224+
],
195225
),
196226
),
197227
# Pass in a dictionary (name-only).
198228
(
199-
{"column_dtypes": {"A": np.int8, "B": np.float32, "C": "<U2"}},
229+
{
230+
"column_dtypes": {
231+
"A": np.int8,
232+
"B": np.float32,
233+
"C": f"{tm.ENDIAN}U2",
234+
}
235+
},
200236
np.rec.array(
201237
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
202-
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "<U2")],
238+
dtype=[
239+
("index", f"{tm.ENDIAN}i8"),
240+
("A", "i1"),
241+
("B", f"{tm.ENDIAN}f4"),
242+
("C", f"{tm.ENDIAN}U2"),
243+
],
203244
),
204245
),
205246
# Pass in a dictionary (indices-only).
206247
(
207248
{"index_dtypes": {0: "int16"}},
208249
np.rec.array(
209250
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
210-
dtype=[("index", "i2"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
251+
dtype=[
252+
("index", "i2"),
253+
("A", f"{tm.ENDIAN}i8"),
254+
("B", f"{tm.ENDIAN}f8"),
255+
("C", "O"),
256+
],
211257
),
212258
),
213259
# Ignore index mappings if index is not True.
214260
(
215-
{"index": False, "index_dtypes": "<U2"},
261+
{"index": False, "index_dtypes": f"{tm.ENDIAN}U2"},
216262
np.rec.array(
217263
[(1, 0.2, "a"), (2, 1.5, "bc")],
218-
dtype=[("A", "<i8"), ("B", "<f8"), ("C", "O")],
264+
dtype=[
265+
("A", f"{tm.ENDIAN}i8"),
266+
("B", f"{tm.ENDIAN}f8"),
267+
("C", "O"),
268+
],
219269
),
220270
),
221271
# Non-existent names / indices in mapping should not error.
222272
(
223273
{"index_dtypes": {0: "int16", "not-there": "float32"}},
224274
np.rec.array(
225275
[(0, 1, 0.2, "a"), (1, 2, 1.5, "bc")],
226-
dtype=[("index", "i2"), ("A", "<i8"), ("B", "<f8"), ("C", "O")],
276+
dtype=[
277+
("index", "i2"),
278+
("A", f"{tm.ENDIAN}i8"),
279+
("B", f"{tm.ENDIAN}f8"),
280+
("C", "O"),
281+
],
227282
),
228283
),
229284
# Names / indices not in mapping default to array dtype.
230285
(
231286
{"column_dtypes": {"A": np.int8, "B": np.float32}},
232287
np.rec.array(
233288
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
234-
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
289+
dtype=[
290+
("index", f"{tm.ENDIAN}i8"),
291+
("A", "i1"),
292+
("B", f"{tm.ENDIAN}f4"),
293+
("C", "O"),
294+
],
235295
),
236296
),
237297
# Names / indices not in dtype mapping default to array dtype.
238298
(
239299
{"column_dtypes": {"A": np.dtype("int8"), "B": np.dtype("float32")}},
240300
np.rec.array(
241301
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
242-
dtype=[("index", "<i8"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
302+
dtype=[
303+
("index", f"{tm.ENDIAN}i8"),
304+
("A", "i1"),
305+
("B", f"{tm.ENDIAN}f4"),
306+
("C", "O"),
307+
],
243308
),
244309
),
245310
# Mixture of everything.
246311
(
247312
{
248313
"column_dtypes": {"A": np.int8, "B": np.float32},
249-
"index_dtypes": "<U2",
314+
"index_dtypes": f"{tm.ENDIAN}U2",
250315
},
251316
np.rec.array(
252317
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
253-
dtype=[("index", "<U2"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
318+
dtype=[
319+
("index", f"{tm.ENDIAN}U2"),
320+
("A", "i1"),
321+
("B", f"{tm.ENDIAN}f4"),
322+
("C", "O"),
323+
],
254324
),
255325
),
256326
# Invalid dype values.
@@ -299,7 +369,11 @@ def test_to_records_dtype(self, kwargs, expected):
299369
{"column_dtypes": "float64", "index_dtypes": {0: "int32", 1: "int8"}},
300370
np.rec.array(
301371
[(1, 2, 3.0), (4, 5, 6.0), (7, 8, 9.0)],
302-
dtype=[("a", "<i4"), ("b", "i1"), ("c", "<f8")],
372+
dtype=[
373+
("a", f"{tm.ENDIAN}i4"),
374+
("b", "i1"),
375+
("c", f"{tm.ENDIAN}f8"),
376+
],
303377
),
304378
),
305379
# MultiIndex in the columns.
@@ -310,14 +384,17 @@ def test_to_records_dtype(self, kwargs, expected):
310384
[("a", "d"), ("b", "e"), ("c", "f")]
311385
),
312386
),
313-
{"column_dtypes": {0: "<U1", 2: "float32"}, "index_dtypes": "float32"},
387+
{
388+
"column_dtypes": {0: f"{tm.ENDIAN}U1", 2: "float32"},
389+
"index_dtypes": "float32",
390+
},
314391
np.rec.array(
315392
[(0.0, "1", 2, 3.0), (1.0, "4", 5, 6.0), (2.0, "7", 8, 9.0)],
316393
dtype=[
317-
("index", "<f4"),
318-
("('a', 'd')", "<U1"),
319-
("('b', 'e')", "<i8"),
320-
("('c', 'f')", "<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"),
321398
],
322399
),
323400
),
@@ -332,19 +409,22 @@ def test_to_records_dtype(self, kwargs, expected):
332409
[("d", -4), ("d", -5), ("f", -6)], names=list("cd")
333410
),
334411
),
335-
{"column_dtypes": "float64", "index_dtypes": {0: "<U2", 1: "int8"}},
412+
{
413+
"column_dtypes": "float64",
414+
"index_dtypes": {0: f"{tm.ENDIAN}U2", 1: "int8"},
415+
},
336416
np.rec.array(
337417
[
338418
("d", -4, 1.0, 2.0, 3.0),
339419
("d", -5, 4.0, 5.0, 6.0),
340420
("f", -6, 7, 8, 9.0),
341421
],
342422
dtype=[
343-
("c", "<U2"),
423+
("c", f"{tm.ENDIAN}U2"),
344424
("d", "i1"),
345-
("('a', 'd')", "<f8"),
346-
("('b', 'e')", "<f8"),
347-
("('c', 'f')", "<f8"),
425+
("('a', 'd')", f"{tm.ENDIAN}f8"),
426+
("('b', 'e')", f"{tm.ENDIAN}f8"),
427+
("('c', 'f')", f"{tm.ENDIAN}f8"),
348428
],
349429
),
350430
),
@@ -374,13 +454,18 @@ def keys(self):
374454

375455
dtype_mappings = {
376456
"column_dtypes": DictLike(**{"A": np.int8, "B": np.float32}),
377-
"index_dtypes": "<U2",
457+
"index_dtypes": f"{tm.ENDIAN}U2",
378458
}
379459

380460
result = df.to_records(**dtype_mappings)
381461
expected = np.rec.array(
382462
[("0", "1", "0.2", "a"), ("1", "2", "1.5", "bc")],
383-
dtype=[("index", "<U2"), ("A", "i1"), ("B", "<f4"), ("C", "O")],
463+
dtype=[
464+
("index", f"{tm.ENDIAN}U2"),
465+
("A", "i1"),
466+
("B", f"{tm.ENDIAN}f4"),
467+
("C", "O"),
468+
],
384469
)
385470
tm.assert_almost_equal(result, expected)
386471

pandas/tests/io/parser/test_c_parser_only.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,12 @@ def test_dtype_and_names_error(c_parser_only):
144144
"the dtype timedelta64 is not supported for parsing",
145145
{"dtype": {"A": "timedelta64", "B": "float64"}},
146146
),
147-
("the dtype <U8 is not supported for parsing", {"dtype": {"A": "U8"}}),
147+
(
148+
f"the dtype {tm.ENDIAN}U8 is not supported for parsing",
149+
{"dtype": {"A": "U8"}},
150+
),
148151
],
149-
ids=["dt64-0", "dt64-1", "td64", "<U8"],
152+
ids=["dt64-0", "dt64-1", "td64", f"{tm.ENDIAN}U8"],
150153
)
151154
def test_unsupported_dtype(c_parser_only, match, kwargs):
152155
parser = c_parser_only

pandas/tests/tools/test_to_timedelta.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ def test_to_timedelta_on_missing_values(self):
198198

199199
actual = to_timedelta(Series(["00:00:01", np.nan]))
200200
expected = Series(
201-
[np.timedelta64(1000000000, "ns"), timedelta_NaT], dtype="<m8[ns]"
201+
[np.timedelta64(1000000000, "ns"), timedelta_NaT],
202+
dtype=f"{tm.ENDIAN}m8[ns]",
202203
)
203204
tm.assert_series_equal(actual, expected)
204205

0 commit comments

Comments
 (0)