NumPy | datetime_as_string method
Start your free 7-days trial now!
NumPy's datetime_as_string(~)
method converts an array of datetimes into an array of strings, and offers a myriad of ways for formatting.
Parameters
1. arr
| array-like
An array of datetimes.
2. unit
| None
or string
| optional
The format of the resulting strings. The allowed values are as follows:
Unit | Description |
---|---|
| The unit becomes the lowest unit in the input array. For instance, if you had |
| The unit is equivalent to |
Dateunit | Can be any of |
Note that for dates that lack a certain unit, the default value of unit one is used. For instance, specifying a unit of "D"
(i.e. days) would convert datetimes like "2020-12"
to "2020-12-01"
.
By default, unit=None
.
3. timezone
| string
| optional
The timezone to use. The allowed values are as follows:
Timezone | Description |
---|---|
naive | A "naive" timezone is one that unaware of timezone - it's just a plain datetime without absolutely any context of locality. |
UTC | The Coordinated Universal Time. |
local | Takes into account the timezone of your geographical location. |
By default, timezone="naive"
. See examples below for clarification.
4. casting
| string
| optional
The casting rules to abide by. The official documentation does not provide any explanation on the specifics of the rules, so here we will attempt to introduce some of the main rules at a basic level.
Rule | Description |
---|---|
no | Throws an error whenever any type of casting is performed. |
safe | Throws an error when you cast to a less specific unit (e.g. from days to weeks). |
unsafe | Allows for any type of casting. This is the opposite of the "no" rule. |
For those who have more insights, we'd love to talk with you!
Return value
A NumPy array of strings that represent the input datetimes.
Examples
Basic usage
datetimes = [np.datetime64("2020-05-30"), np.datetime64("2020-12-25")]np.datetime_as_string(datetimes)
array(['2020-05-30', '2020-12-25'], dtype='<U28')
Changing the units
None
datetimes = [np.datetime64("2020-05"), np.datetime64("2020-12")]np.datetime_as_string(datetimes, unit=None)
array(['2020-05', '2020-12'], dtype='<U25')
auto
datetimes = [np.datetime64("2020-05"), np.datetime64("2020-12")]np.datetime_as_string(datetimes, unit="auto")
array(['2020-05-01', '2020-12-01'], dtype='<U62')
Notice how the resulting strings have days, that is, unit="auto"
is identical to unit="D"
(days).
Months
datetimes = [np.datetime64("2020-05-30"), np.datetime64("2020-12-25")]np.datetime_as_string(datetimes, unit="M")
array(['2020-05', '2020-12'], dtype='<U25')
Changing the timezone
Naive
datetimes = [np.datetime64("2020-12-25T03:30"), np.datetime64("2020-12-06T02:50")]np.datetime_as_string(datetimes, timezone="naive")
array(['2020-12-25T03:30', '2020-12-06T02:50'], dtype='<U35')
UTC
datetimes = [np.datetime64("2020-12-25T03:30"), np.datetime64("2020-12-06T02:50")]np.datetime_as_string(datetimes, timezone="UTC")
array(['2020-12-25T03:30Z', '2020-12-06T02:50Z'], dtype='<U35')
Notice how we have Z
at the end, indicating that the timezone is UTC.
local
datetimes = [np.datetime64("2020-12-25T03:30"), np.datetime64("2020-12-06T02:50")]np.datetime_as_string(datetimes, timezone="local")
array(['2020-12-25T11:30+0800', '2020-12-06T10:50+0800'], dtype='<U39')
Notice how we have +0800 at the end, which means that my current location is 8 hours ahead of UTC.
Casting rules
no
datetimes = [np.datetime64("2020-05-05")]np.datetime_as_string(datetimes, unit="M", casting="no")
TypeError: Cannot create a datetime string as units 'M' from a NumPy datetime with units 'D' according to the rule 'no'
This throws an error because the "no"
rule does not allow for any form of casting.
safe
datetimes = [np.datetime64("2020-05-05")]np.datetime_as_string(datetimes, unit="M", casting="safe")
TypeError: Cannot create a datetime string as units 'M' from a NumPy datetime with units 'D' according to the rule 'safe'
This throws an error because the "safe"
rule does not allow to cast to a less specific unit.
unsafe
datetimes = [np.datetime64("2020-05-05"), np.datetime64("2020")]np.datetime_as_string(datetimes, unit="M", casting="unsafe")
array(['2020-05', '2020-01'], dtype='<U25')
Here, no error this thrown since the "unsafe"
rule allows for any form of casting.