NumPy represents dates internally using an int64 counter and a unit metadata struct. Time differences are represented similarly using an int64 and a unit metadata struct. The functions described below are available to to facilitate converting between ISO 8601 date strings, NumPy datetimes, and Python datetime objects in C.
In addition to the npy_datetime and npy_timedelta typedefs for npy_int64, NumPy defines two additional structs that represent time unit metadata and an “exploded” view of a datetime.
Represents datetime unit metadata.
typedef struct {
NPY_DATETIMEUNIT base;
int num;
} PyArray_DatetimeMetaData;
The unit of the datetime.
A multiplier for the unit.
An “exploded” view of a datetime value
typedef struct {
npy_int64 year;
npy_int32 month, day, hour, min, sec, us, ps, as;
} npy_datetimestruct;
Time units supported by NumPy. The “FR” in the names of the enum variants is short for frequency.
Error or undetermined units.
Years
Months
Weeks
Days
Hours
Minutes
Seconds
Milliseconds
Microseconds
Nanoseconds
Picoseconds
Femtoseconds
Attoseconds
Unbound units, can convert to anything
Converts a datetime from a datetimestruct to a datetime in the units specified by the unit metadata. The date is assumed to be valid.
If the num member of the metadata struct is large, there may be integer overflow in this function.
Returns 0 on success and -1 on failure.
Converts a datetime with units specified by the unit metadata to an exploded datetime struct.
Returns 0 on success and -1 on failure.
Tests for and converts a Python datetime.datetime or datetime.date object into a NumPy npy_datetimestruct.
out_bestunit gives a suggested unit based on whether the object was a datetime.date or datetime.datetime object.
If apply_tzinfo is 1, this function uses the tzinfo to convert to UTC time, otherwise it returns the struct with the local time.
Returns -1 on error, 0 on success, and 1 (with no error set) if obj doesn’t have the needed date or datetime attributes.
Parses (almost) standard ISO 8601 date strings. The differences are:
str must be a NULL-terminated string, and len must be its length.
unit should contain -1 if the unit is unknown, or the unit which will be used if it is.
casting controls how the detected unit from the string is allowed to be cast to the ‘unit’ parameter.
out gets filled with the parsed date-time.
out_bestunit gives a suggested unit based on the amount of resolution provided in the string, or -1 for NaT.
out_special gets set to 1 if the parsed time was ‘today’, ‘now’, empty string, or ‘NaT’. For ‘today’, the unit recommended is ‘D’, for ‘now’, the unit recommended is ‘s’, and for ‘NaT’ the unit recommended is ‘Y’.
Returns 0 on success, -1 on failure.
Returns the string length to use for converting datetime objects with the given local time and unit settings to strings. Use this when constructing strings to supply to NpyDatetime_MakeISO8601Datetime.
Converts an npy_datetimestruct to an (almost) ISO 8601 NULL-terminated string. If the string fits in the space exactly, it leaves out the NULL terminator and returns success.
The differences from ISO 8601 are the ‘NaT’ string, and the number of year digits is >= 4 instead of strictly 4.
If local is non-zero, it produces a string in local time with a +-#### timezone offset. If local is zero and utc is non-zero, produce a string ending with ‘Z’ to denote UTC. By default, no time zone information is attached.
base restricts the output to that unit. Set base to -1 to auto-detect a base after which all the values are zero.
tzoffset is used if local is enabled, and tzoffset is set to a value other than -1. This is a manual override for the local time zone to use, as an offset in minutes.
casting controls whether data loss is allowed by truncating the data to a coarser unit. This interacts with local, slightly, in order to form a date unit string as a local time, the casting must be unsafe.
Returns 0 on success, -1 on failure (for example if the output string was too short).
© 2005–2024 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/2.4/reference/c-api/datetimes.html