MCPcopy Create free account
hub / github.com/numpy/numpy / test_iter_copy_casts_structured

Function test_iter_copy_casts_structured

numpy/core/tests/test_nditer.py:1428–1457  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1426
1427
1428def test_iter_copy_casts_structured():
1429 # Test a complicated structured dtype for casting, as it requires
1430 # both multiple steps and a more complex casting setup.
1431 # Includes a structured -> unstructured (any to object), and many other
1432 # casts, which cause this to require all steps in the casting machinery
1433 # one level down as well as the iterator copy (which uses NpyAuxData clone)
1434 in_dtype = np.dtype([("a", np.dtype("i,")),
1435 ("b", np.dtype(">i,<i,>d,S17,>d,(3)f,O,i1"))])
1436 out_dtype = np.dtype([("a", np.dtype("O")),
1437 ("b", np.dtype(">i,>i,S17,>d,>U3,(3)d,i1,O"))])
1438 arr = np.ones(1000, dtype=in_dtype)
1439
1440 it = np.nditer((arr,), ["buffered", "external_loop", "refs_ok"],
1441 op_dtypes=[out_dtype], casting="unsafe")
1442 it_copy = it.copy()
1443
1444 res1 = next(it)
1445 del it
1446 res2 = next(it_copy)
1447 del it_copy
1448
1449 expected = arr["a"].astype(out_dtype["a"])
1450 assert_array_equal(res1["a"], expected)
1451 assert_array_equal(res2["a"], expected)
1452
1453 for field in in_dtype["b"].names:
1454 # Note that the .base avoids the subarray field
1455 expected = arr["b"][field].astype(out_dtype["b"][field].base)
1456 assert_array_equal(res1["b"][field], expected)
1457 assert_array_equal(res2["b"][field], expected)
1458
1459
1460def test_iter_copy_casts_structured2():

Callers

nothing calls this directly

Calls 5

assert_array_equalFunction · 0.90
astypeMethod · 0.80
nextFunction · 0.50
dtypeMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected