{ } Raw JSON

bundles / numpy 2.4.4 / numpy / datetime_as_string

_ArrayFunctionDispatcher

numpy:datetime_as_string

Signature

def   datetime_as_string ( arr unit = None timezone = naive casting = same_kind )

Summary

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 a datetime unit <arrays.dtypes.dateunits>.

timezone : {'naive', 'UTC', 'local'} or tzinfo

Timezone information to use when displaying the datetime. If 'UTC', end with a Z to indicate UTC time. If 'local', convert to the local timezone first, and suffix with a +-#### timezone offset. If a tzinfo object, then do as with 'local', but use the specified timezone.

casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}

Casting to allow when changing between datetime units.

Returns

str_arr : ndarray

An array of strings the same shape as arr.

Examples

import numpy as np
from zoneinfo import ZoneInfo
d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8[m]')
d
Setting the timezone to UTC shows the same information, but with a Z suffix
np.datetime_as_string(d, timezone='UTC')
Note that we picked datetimes that cross a DST boundary. Passing in a ``ZoneInfo`` object will print the appropriate offset
np.datetime_as_string(d, timezone=ZoneInfo('US/Eastern'))
Passing in a unit will change the precision
np.datetime_as_string(d, unit='h')
np.datetime_as_string(d, unit='s')
'casting' can be used to specify whether precision can be changed
np.datetime_as_string(d, unit='h', casting='safe')

Aliases

  • numpy.datetime_as_string