Skip to content

Commit

Permalink
DOC: add example showing how to convert POSIX timestamps to datetime64 (
Browse files Browse the repository at this point in the history
#18984)

* DOC: add example showing how to convert POSIX timestamps to datetime64

Co-authored-by: Melissa Weber Mendonça <melissawm@gmail.com>
  • Loading branch information
jonathangjertsen and melissawm committed Oct 1, 2021
1 parent c783285 commit 6ad932c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions doc/source/reference/arrays.datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ form of the string, and can be either a :ref:`date unit <arrays.dtypes.dateunits
:ref:`time unit <arrays.dtypes.timeunits>`. The date units are years ('Y'),
months ('M'), weeks ('W'), and days ('D'), while the time units are
hours ('h'), minutes ('m'), seconds ('s'), milliseconds ('ms'), and
some additional SI-prefix seconds-based units. The datetime64 data type
some additional SI-prefix seconds-based units. The datetime64 data type
also accepts the string "NAT", in any combination of lowercase/uppercase
letters, for a "Not A Time" value.

Expand Down Expand Up @@ -74,6 +74,18 @@ datetime type with generic units.
array(['2001-01-01T12:00:00.000', '2002-02-03T13:56:03.172'],
dtype='datetime64[ms]')

An array of datetimes can be constructed from integers representing
POSIX timestamps with the given unit.

.. admonition:: Example

>>> np.array([0, 1577836800], dtype='datetime64[s]')
array(['1970-01-01T00:00:00', '2020-01-01T00:00:00'],
dtype='datetime64[s]')

>>> np.array([0, 1577836800000]).astype('datetime64[ms]')
array(['1970-01-01T00:00:00.000', '2020-01-01T00:00:00.000'],
dtype='datetime64[ms]')

The datetime type works with many common NumPy functions, for
example :func:`arange` can be used to generate ranges of dates.
Expand Down Expand Up @@ -120,9 +132,9 @@ Datetime and Timedelta Arithmetic
NumPy allows the subtraction of two Datetime values, an operation which
produces a number with a time unit. Because NumPy doesn't have a physical
quantities system in its core, the timedelta64 data type was created
to complement datetime64. The arguments for timedelta64 are a number,
to complement datetime64. The arguments for timedelta64 are a number,
to represent the number of units, and a date/time unit, such as
(D)ay, (M)onth, (Y)ear, (h)ours, (m)inutes, or (s)econds. The timedelta64
(D)ay, (M)onth, (Y)ear, (h)ours, (m)inutes, or (s)econds. The timedelta64
data type also accepts the string "NAT" in place of the number for a "Not A Time" value.

.. admonition:: Example
Expand Down

0 comments on commit 6ad932c

Please sign in to comment.