MCPcopy Index your code
hub / github.com/numpy/numpy / datetime_as_string

Function datetime_as_string

numpy/_core/multiarray.py:1678–1743  ·  view source on GitHub ↗

datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind') Convert an array of datetimes into an array of strings. Parameters ---------- arr : array_like of datetime64 The array of UTC timestamps to format. unit : str One of None, 'auto', or

(arr, unit=None, timezone="naive", casting="same_kind")

Source from the content-addressed store, hash-verified

1676
1677@array_function_from_c_func_and_dispatcher(_multiarray_umath.datetime_as_string)
1678def datetime_as_string(arr, unit=None, timezone="naive", casting="same_kind"):
1679 """
1680 datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind')
1681
1682 Convert an array of datetimes into an array of strings.
1683
1684 Parameters
1685 ----------
1686 arr : array_like of datetime64
1687 The array of UTC timestamps to format.
1688 unit : str
1689 One of None, 'auto', or
1690 a :ref:`datetime unit <arrays.dtypes.dateunits>`.
1691 timezone : {'naive', 'UTC', 'local'} or tzinfo
1692 Timezone information to use when displaying the datetime. If 'UTC',
1693 end with a Z to indicate UTC time. If 'local', convert to the local
1694 timezone first, and suffix with a +-#### timezone offset. If a tzinfo
1695 object, then do as with 'local', but use the specified timezone.
1696 casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}
1697 Casting to allow when changing between datetime units.
1698
1699 Returns
1700 -------
1701 str_arr : ndarray
1702 An array of strings the same shape as `arr`.
1703
1704 Examples
1705 --------
1706 >>> import numpy as np
1707 >>> from zoneinfo import ZoneInfo
1708 >>> d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8[m]')
1709 >>> d
1710 array(['2002-10-27T04:30', '2002-10-27T05:30', '2002-10-27T06:30',
1711 '2002-10-27T07:30'], dtype='datetime64[m]')
1712
1713 Setting the timezone to UTC shows the same information, but with a Z suffix
1714
1715 >>> np.datetime_as_string(d, timezone='UTC')
1716 array(['2002-10-27T04:30Z', '2002-10-27T05:30Z', '2002-10-27T06:30Z',
1717 '2002-10-27T07:30Z'], dtype='<U35')
1718
1719 Note that we picked datetimes that cross a DST boundary. Passing in a
1720 ``ZoneInfo`` object will print the appropriate offset
1721
1722 >>> np.datetime_as_string(d, timezone=ZoneInfo('US/Eastern'))
1723 array(['2002-10-27T00:30-0400', '2002-10-27T01:30-0400',
1724 '2002-10-27T01:30-0500', '2002-10-27T02:30-0500'], dtype='<U39')
1725
1726 Passing in a unit will change the precision
1727
1728 >>> np.datetime_as_string(d, unit='h')
1729 array(['2002-10-27T04', '2002-10-27T05', '2002-10-27T06', '2002-10-27T07'],
1730 dtype='<U32')
1731 >>> np.datetime_as_string(d, unit='s')
1732 array(['2002-10-27T04:30:00', '2002-10-27T05:30:00', '2002-10-27T06:30:00',
1733 '2002-10-27T07:30:00'], dtype='<U38')
1734
1735 'casting' can be used to specify whether precision can be changed

Callers 1

_format_non_natMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…