numpy / numpy/numpy

ENH: create a way to take random uniform datetimes

Open
#21,961 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

00 - Bug
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Describe the issue:

Internally, the datetime64 dtype is represented as an integer. One can do operations on it such as adding or substracting, which are well defined.

Nevertheless, np.random.Generator.uniform will not let me use this dtype to specify the highs and lows. I would want to do something like this:

import numpy as np
low = np.datetime64("2020-01-01 00:00:00")
high = np.datetime64("2020-01-31 23:59:59")
np.random.default_rng().uniform(
    low,
    high,
    size=10
)

But it errors out so I have to add boilerplate code:

low + np.random.default_rng().uniform(
    high=(high - low).astype(int),
    size=10
).astype(int)

One could argue that in theory uniform is not the most appropriate choice since it generates floating points which then have to be rounded to integers, but np.random.Generator.integers also errors out:

np.random.default_rng().integers(low, high, size=10)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-8d188f6ec7e3> in <module>
----> 1 np.random.default_rng().integers(low, high, size=10)

_generator.pyx in numpy.random._generator.Generator.integers()

_bounded_integers.pyx in numpy.random._bounded_integers._rand_int64()

TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'
Reproduce the code example:
import numpy as np
low = np.datetime64("2020-01-01 00:00:00")
high = np.datetime64("2020-01-31 23:59:59")
np.random.default_rng().uniform(
    low,
    high,
    size=10
)
Error message:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-3abdd025d9c8> in <module>
      2 low = np.datetime64("2020-01-01 00:00:00")
      3 high = np.datetime64("2020-01-31 23:59:59")
----> 4 np.random.default_rng().uniform(
      5     low,
      6     high,

_generator.pyx in numpy.random._generator.Generator.uniform()

TypeError: float() argument must be a string or a number, not 'datetime.datetime'
NumPy/Python version information:

1.21.5 3.9.12 (main, Apr 5 2022, 06:56:58)
[GCC 7.5.0]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the np.random.Generator.uniform and Generator.integers entry points shown in the traceback, then inspect the surrounding random-generation implementation and tests. Define how the requested datetime64 bounds should be handled and add coverage for the examples; the work is done when datetime64 inputs produce random datetime results without the conversion boilerplate.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.