# Numpy date utilities#

This library provides utility functions to perform conversions and get information about numpy dates quickly.

```import datetime
import random

import numpy

import pyinterp

def make_date(samples=10000):
"""Generates random dates."""
epoch = datetime.datetime(1970, 1, 1)
delta = datetime.datetime.now() - datetime.datetime(1970, 1, 1)

pydates = [epoch + random.random() * delta for _ in range(samples)]
npdates = numpy.array(pydates).astype('datetime64[ns]')

return npdates
```
```dates = make_date()
dates
```
```array(['1981-06-22T21:20:18.214385000', '2008-11-22T13:16:05.184959000',
'1975-05-08T08:06:50.950819000', ...,
'2014-07-09T21:16:17.339595000', '1994-12-06T07:05:40.230832000',
'1987-10-29T14:14:00.179224000'], dtype='datetime64[ns]')
```

Get the date part as a structured numpy array of three fields: `year`, `month` and `day`:

```pyinterp.dateutils.date(dates)
```
```array([(1981,  6, 22), (2008, 11, 22), (1975,  5,  8), ...,
(2014,  7,  9), (1994, 12,  6), (1987, 10, 29)],
dtype=[('year', '<i4'), ('month', '<u4'), ('day', '<u4')])
```

Get the time part as a structured numpy array of three fields: `hour`, `minute` and `second`:

```pyinterp.dateutils.time(dates)
```
```array([(21, 20, 18), (13, 16,  5), ( 8,  6, 50), ..., (21, 16, 17),
( 7,  5, 40), (14, 14,  0)],
dtype=[('hour', '<u4'), ('minute', '<u4'), ('second', '<u4')])
```

Get the ISO calendar of the date as a structured numpy array of three fields: `year`, `weekday` and `week`:

```pyinterp.dateutils.isocalendar(dates)
```
```array([(1981, 26, 1), (2008, 47, 6), (1975, 19, 4), ..., (2014, 28, 3),
(1994, 49, 2), (1987, 44, 4)],
dtype=[('year', '<i4'), ('week', '<u4'), ('weekday', '<u4')])
```

Get the week day of the dates (Sunday is 0 … Saturday is 6):

```pyinterp.dateutils.weekday(dates)
```
```array([1, 6, 4, ..., 3, 2, 4], dtype=uint32)
```

Get the timedelta from since January

```pyinterp.dateutils.timedelta_since_january(dates)
```
```array([14937618214385000, 28214165184959000, 11002010950819000, ...,
16406177339595000, 29315140230832000, 26057640179224000],
dtype='timedelta64[ns]')
```

Get the dates as datetime.datetime array

```pyinterp.dateutils.datetime(dates)
```
```array([datetime.datetime(1981, 6, 22, 21, 20, 18, 214385),
datetime.datetime(2008, 11, 22, 13, 16, 5, 184959),
datetime.datetime(1975, 5, 8, 8, 6, 50, 950819), ...,
datetime.datetime(2014, 7, 9, 21, 16, 17, 339595),
datetime.datetime(1994, 12, 6, 7, 5, 40, 230832),
datetime.datetime(1987, 10, 29, 14, 14, 0, 179224)], dtype=object)
```

Total running time of the script: (0 minutes 0.041 seconds)

Gallery generated by Sphinx-Gallery