Skip to content

Commit

Permalink
Merge pull request #3946 from Zac-HD/numpy-nat
Browse files Browse the repository at this point in the history
Allow excluding `NaT`
  • Loading branch information
Zac-HD authored Mar 31, 2024
2 parents 15ae3c2 + e6567e5 commit 77914f8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: minor

The :func:`~hypothesis.extra.numpy.from_dtype` function no longer generates
``NaT`` ("not-a-time") values for the ``datetime64`` or ``timedelta64`` dtypes
if passed ``allow_nan=False`` (:issue:`3943`).
2 changes: 1 addition & 1 deletion hypothesis-python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def local_file(name):
"pytest": ["pytest>=4.6"],
"dpcontracts": ["dpcontracts>=0.4"],
"redis": ["redis>=3.0.0"],
"crosshair": ["hypothesis-crosshair>=0.0.2", "crosshair-tool>=0.0.53"],
"crosshair": ["hypothesis-crosshair>=0.0.2", "crosshair-tool>=0.0.54"],
# zoneinfo is an odd one: every dependency is conditional, because they're
# only necessary on old versions of Python or Windows systems or emscripten.
"zoneinfo": [
Expand Down
6 changes: 5 additions & 1 deletion hypothesis-python/src/hypothesis/extra/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ def compat_kw(*args, **kw):
# it here because we'd have to guard against equivalents in arrays()
# regardless and drawing scalars is a valid use-case.
res = st.sampled_from(TIME_RESOLUTIONS)
result = st.builds(dtype.type, st.integers(-(2**63), 2**63 - 1), res)
if allow_nan is not False:
elems = st.integers(-(2**63), 2**63 - 1) | st.just("NaT")
else: # NEP-7 defines the NaT value as integer -(2**63)
elems = st.integers(-(2**63) + 1, 2**63 - 1)
result = st.builds(dtype.type, elems, res)
else:
raise InvalidArgument(f"No strategy inference for {dtype}")
return result.map(dtype.type)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Version 2024031000, Last Updated Sun Mar 10 07:07:01 2024 UTC
# Version 2024033000, Last Updated Sat Mar 30 07:07:01 2024 UTC
AAA
AARP
ABB
Expand Down Expand Up @@ -85,7 +85,6 @@ AUSPOST
AUTHOR
AUTO
AUTOS
AVIANCA
AW
AWS
AX
Expand Down
7 changes: 7 additions & 0 deletions hypothesis-python/tests/numpy/test_from_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ def test_arrays_selects_consistent_time_unit(data, dtype_str):
assert (dtype_str + "[") in arr.dtype.str


@pytest.mark.parametrize("dtype", ["m8", "M8"])
def test_from_dtype_can_include_or_exclude_nat(dtype):
find_any(nps.from_dtype(np.dtype(dtype), allow_nan=None), np.isnat)
find_any(nps.from_dtype(np.dtype(dtype), allow_nan=True), np.isnat)
assert_no_examples(nps.from_dtype(np.dtype(dtype), allow_nan=False), np.isnat)


def test_arrays_gives_useful_error_on_inconsistent_time_unit():
with pytest.raises(InvalidArgument, match="mismatch of time units in dtypes"):
check_can_generate_examples(
Expand Down

0 comments on commit 77914f8

Please sign in to comment.