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=None, casting=None)
| 1650 | @array_function_from_c_func_and_dispatcher( |
| 1651 | _multiarray_umath.datetime_as_string) |
| 1652 | def datetime_as_string(arr, unit=None, timezone=None, casting=None): |
| 1653 | """ |
| 1654 | datetime_as_string(arr, unit=None, timezone='naive', casting='same_kind') |
| 1655 | |
| 1656 | Convert an array of datetimes into an array of strings. |
| 1657 | |
| 1658 | Parameters |
| 1659 | ---------- |
| 1660 | arr : array_like of datetime64 |
| 1661 | The array of UTC timestamps to format. |
| 1662 | unit : str |
| 1663 | One of None, 'auto', or a :ref:`datetime unit <arrays.dtypes.dateunits>`. |
| 1664 | timezone : {'naive', 'UTC', 'local'} or tzinfo |
| 1665 | Timezone information to use when displaying the datetime. If 'UTC', end |
| 1666 | with a Z to indicate UTC time. If 'local', convert to the local timezone |
| 1667 | first, and suffix with a +-#### timezone offset. If a tzinfo object, |
| 1668 | then do as with 'local', but use the specified timezone. |
| 1669 | casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'} |
| 1670 | Casting to allow when changing between datetime units. |
| 1671 | |
| 1672 | Returns |
| 1673 | ------- |
| 1674 | str_arr : ndarray |
| 1675 | An array of strings the same shape as `arr`. |
| 1676 | |
| 1677 | Examples |
| 1678 | -------- |
| 1679 | >>> import pytz |
| 1680 | >>> d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8[m]') |
| 1681 | >>> d |
| 1682 | array(['2002-10-27T04:30', '2002-10-27T05:30', '2002-10-27T06:30', |
| 1683 | '2002-10-27T07:30'], dtype='datetime64[m]') |
| 1684 | |
| 1685 | Setting the timezone to UTC shows the same information, but with a Z suffix |
| 1686 | |
| 1687 | >>> np.datetime_as_string(d, timezone='UTC') |
| 1688 | array(['2002-10-27T04:30Z', '2002-10-27T05:30Z', '2002-10-27T06:30Z', |
| 1689 | '2002-10-27T07:30Z'], dtype='<U35') |
| 1690 | |
| 1691 | Note that we picked datetimes that cross a DST boundary. Passing in a |
| 1692 | ``pytz`` timezone object will print the appropriate offset |
| 1693 | |
| 1694 | >>> np.datetime_as_string(d, timezone=pytz.timezone('US/Eastern')) |
| 1695 | array(['2002-10-27T00:30-0400', '2002-10-27T01:30-0400', |
| 1696 | '2002-10-27T01:30-0500', '2002-10-27T02:30-0500'], dtype='<U39') |
| 1697 | |
| 1698 | Passing in a unit will change the precision |
| 1699 | |
| 1700 | >>> np.datetime_as_string(d, unit='h') |
| 1701 | array(['2002-10-27T04', '2002-10-27T05', '2002-10-27T06', '2002-10-27T07'], |
| 1702 | dtype='<U32') |
| 1703 | >>> np.datetime_as_string(d, unit='s') |
| 1704 | array(['2002-10-27T04:30:00', '2002-10-27T05:30:00', '2002-10-27T06:30:00', |
| 1705 | '2002-10-27T07:30:00'], dtype='<U38') |
| 1706 | |
| 1707 | 'casting' can be used to specify whether precision can be changed |
| 1708 | |
| 1709 | >>> np.datetime_as_string(d, unit='h', casting='safe') |