0

What is the easiest and most pythonic way to get the number of days in a month given a np.datetime64 value.

For example: '2020-01-31T00:00:00.000000000' = 31

I'm trying to find something equivalent to Pandas.daysinmonth, but for numpy.

#pd.daysinmonth example:
p = pd.Period('2018-2-17')
p.days_in_month
28
mikelowry
  • 1,307
  • 4
  • 21
  • 43
  • Just trying to use Numpy to improve speed/efficiency of code. Could you give me an example of how using pandas datetime could work? – mikelowry Feb 27 '21 at 20:01

1 Answers1

2

You can use pandas functions on np.datetime64 objects:

>>> ts = np.datetime64('2020-01-31T00:00:00.000000000')
>>> ts
numpy.datetime64('2020-01-31T00:00:00.000000000')
>>> pd.Period(ts, freq='D').days_in_month
31

Or, using np.datetime_as_string:

>>> pd.Period(np.datetime_as_string(ts)).days_in_month
31

Here's a pure numpy solution, although I'm mildly positive there exists a better one.

>>> ts = np.datetime64('2020-01-31T00:00:00.000000000')
>>> def days_in_month(ts):
        ts_month = ts.astype('datetime64[M]')
        return (
            ((ts_month+1).astype('datetime64[D]') - ts_month) 
            // np.timedelta64(1, 'D')
         )
>>> days_in_month(ts)
31

It is vectorized and reasonably fast:

>>> arr = np.arange('2000-01', '2021-12', dtype='datetime64[D]')

>>> arr.shape
(8005,)

>>> %timeit days_in_month(arr)
586 µs ± 12.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52