NumPy - time

https://numpy.org/doc/stable/reference/arrays.datetime.html

DATETIMES AND TIMEDELTAS

The 'datetime' object represents a single moment in time.


# NumPy uses the name 'datetime64' because 'datetime' is already taken.

a = np.datetime64('2005-02-25')   # a simple ISO date
b = np.datetime64('2005-02-27')   # a simple ISO date
b - a   # np.timedelta64(2,'D')

# Date units: (D)ay, (M)onth, (Y)ear, (W)eek.
# Time units: (h)our, (m)inute, (s)econd, ms, us, ns, ps, as.

np.datetime64('2005-02-25T03:30')   # date and time

np.arange('2005-02-01', '2005-03-01', dtype='datetime64[D]')
array(['2005-02-01', '2005-02-02', '2005-02-03', '2005-02-04',
       '2005-02-05', '2005-02-06', '2005-02-07', '2005-02-08',
       '2005-02-09', '2005-02-10', '2005-02-11', '2005-02-12',
       '2005-02-13', '2005-02-14', '2005-02-15', '2005-02-16',
       '2005-02-17', '2005-02-18', '2005-02-19', '2005-02-20',
       '2005-02-21', '2005-02-22', '2005-02-23', '2005-02-24',
       '2005-02-25', '2005-02-26', '2005-02-27', '2005-02-28'],
      dtype='datetime64[D]')   # all the dates for one month

np.arange('2005-01-01', '2005-01-02', dtype="datetime64[h]")
array(['2005-01-01T00', '2005-01-01T01', '2005-01-01T02', '2005-01-01T03',
       '2005-01-01T04', '2005-01-01T05', '2005-01-01T06', '2005-01-01T07',
       '2005-01-01T08', '2005-01-01T09', '2005-01-01T10', '2005-01-01T11',
       '2005-01-01T12', '2005-01-01T13', '2005-01-01T14', '2005-01-01T15',
       '2005-01-01T16', '2005-01-01T17', '2005-01-01T18', '2005-01-01T19',
       '2005-01-01T20', '2005-01-01T21', '2005-01-01T22', '2005-01-01T23'],
      dtype='datetime64[h]')

# When creating an array of datetimes from a string, it is still possible
# to automatically select the unit from the inputs, by using the datetime
# type with generic units.

np.array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64')
# array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64[D]')

np.array(['2001-01-01T12:00', '2002-02-03T13:56:03'], dtype='datetime64')
# array(['2001-01-01T12:00:00', '2002-02-03T13:56:03'], dtype='datetime64[s]')

np.datetime64('2009-01-01') + np.timedelta64(20, 'D')
# numpy.datetime64('2009-01-21')

np.datetime64('2009') + np.timedelta64(20, 'm')
# numpy.datetime64('2009-01-01T00:20')

np.datetime64('2020-01-01') + np.timedelta64(3, 'W')
# numpy.datetime64('2020-01-22')

np.timedelta64(1,'W') / np.timedelta64(1,'D')   # 7.0