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

Method test_time_to_time

numpy/core/tests/test_casting_unittests.py:429–475  ·  view source on GitHub ↗
(self, from_dt, to_dt,
                          expected_casting, expected_view_off,
                          nom, denom)

Source from the content-addressed store, hash-verified

427 # give full values based on NumPy 1.19.x
428 [-2**63, 0, 0, 1314, -1315, 564442610])])
429 def test_time_to_time(self, from_dt, to_dt,
430 expected_casting, expected_view_off,
431 nom, denom):
432 from_dt = np.dtype(from_dt)
433 if to_dt is not None:
434 to_dt = np.dtype(to_dt)
435
436 # Test a few values for casting (results generated with NumPy 1.19)
437 values = np.array([-2**63, 1, 2**63-1, 10000, -10000, 2**32])
438 values = values.astype(np.dtype("int64").newbyteorder(from_dt.byteorder))
439 assert values.dtype.byteorder == from_dt.byteorder
440 assert np.isnat(values.view(from_dt)[0])
441
442 DType = type(from_dt)
443 cast = get_castingimpl(DType, DType)
444 casting, (from_res, to_res), view_off = cast._resolve_descriptors(
445 (from_dt, to_dt))
446 assert from_res is from_dt
447 assert to_res is to_dt or to_dt is None
448 assert casting == expected_casting
449 assert view_off == expected_view_off
450
451 if nom is not None:
452 expected_out = (values * nom // denom).view(to_res)
453 expected_out[0] = "NaT"
454 else:
455 expected_out = np.empty_like(values)
456 expected_out[...] = denom
457 expected_out = expected_out.view(to_dt)
458
459 orig_arr = values.view(from_dt)
460 orig_out = np.empty_like(expected_out)
461
462 if casting == Casting.unsafe and (to_dt == "m8" or to_dt == "M8"):
463 # Casting from non-generic to generic units is an error and should
464 # probably be reported as an invalid cast earlier.
465 with pytest.raises(ValueError):
466 cast._simple_strided_call((orig_arr, orig_out))
467 return
468
469 for aligned in [True, True]:
470 for contig in [True, True]:
471 arr, out = self.get_data_variation(
472 orig_arr, orig_out, aligned, contig)
473 out[...] = 0
474 cast._simple_strided_call((arr, out))
475 assert_array_equal(out.view("int64"), expected_out.view("int64"))
476
477 def string_with_modified_length(self, dtype, change_length):
478 fact = 1 if dtype.char == "S" else 4

Callers

nothing calls this directly

Calls 5

get_data_variationMethod · 0.95
assert_array_equalFunction · 0.90
astypeMethod · 0.80
dtypeMethod · 0.45
viewMethod · 0.45

Tested by

no test coverage detected