jztools.datetime64#

Module Attributes

FlexDateTime

A naive date-time as a numpy.datetime64 (string or object) or naive datetime or timezone-aware datetime.

UTCFlexDateTime

The same as FlexDateTime but with the UTC convention made explicit.

UTCDateTime64

A np.datetime64 in UTC.

Functions

as_datetime(dt64[, tzinfo])

Converts the np.datetime64 object to a datetime.datetime, for dtypes containing hour or higher resolution, or datetime.date object otherwise.

as_naive(in_datetime[, in_tzinfo, out_tzinfo])

Takes an input datetime (as an str, datetime or datetime64) in the specified input timezone and converts it into a naive datetime64 in the specified output timezone.

as_naive_utc(in_datetime[, in_tzinfo])

Same as as_naive() but with out_tzinfo = pytz.UTC.

as_str(dt64)

date_slot(dates, child_resolution, ...[, ...])

Returns an integer specification of the date when counting a specific child time resolution in a given parent time resolution.

datetime64_dtype_to_timedelta64_dtype(dtype)

month(dates, *args)

Returns the month as an integer in 0,..,11.

monthday(dates)

Returns the day of the month as an integer in 0,..,30.

now([res, tz])

param res:

A numpy datetime64 specifier, e.g., one of ('H', 'm', 's', 'ms', 'us', 'ns').

now_str(*args, **kwargs)

Returns now() as a string with 'T' substituted by a space.

parse_numpy_time_dtype(val)

Returns a dictionary with fields 'type' ('datetime64' or 'timedelta64'), 'num' (number of date time units) and 'dt_unit' (datetime unit, e.g., 'D', 'm', 's', 'h')

time_dtype_to_item(dtype)

utc_from_eastern(in_datetime)

Converts a datetime from US/Eastern (standard or dalyight saving depending on the time) to UTC.

weekday(dates, *args)

Returns the day of the week as an integer in 0,..,6, with 0 corresponding to the day of the week of the specified reference date (defaults to Monday as 0).

jztools.datetime64.FlexDateTime#

A naive date-time as a numpy.datetime64 (string or object) or naive datetime or timezone-aware datetime. All naive values are assumed to be in UTC by convention.

alias of Union[datetime64, datetime, str]

jztools.datetime64.UTCFlexDateTime#

The same as FlexDateTime but with the UTC convention made explicit.

alias of Union[datetime64, datetime, str]

jztools.datetime64.UTCDateTime64#

A np.datetime64 in UTC.

jztools.datetime64.date_slot(dates: ndarray, child_resolution: str | dtype, parent_resolution: str | dtype, ref_date=numpy.datetime64('2018-01-01')) Tuple[ndarray, int]#

Returns an integer specification of the date when counting a specific child time resolution in a given parent time resolution. For example, for child resolution of 1 minute in parent resolution of 1 hour, returns an array of integers in 0,…,59 of the same shape as dates.

Parameters:
  • dates – A numpy datetime64 array.

  • parent_resolution (child_resolution,) – A numpy dtype that is a sub-dtype of numpy.timedelta64, or a string specification of such a dtype. Valid strings include 'ns', 'us', 'ms', 's', 'm', 'h', 'D', 'M', 'Y'. Child resolutions can further be any such string prepended with a string integer to indicate non-unit resolutions (e.g., '10m'). Parent resolutions cannot include an integer. Integer-prepended parent resolutions produce undefined results or raise an exception.

  • ref_date – The reference date representing the position 0 as a numpy.datetime64 object. Defaults to np.datetime64('2018-01-01') - a Monday.

Returns:

Returns the dates specified as integer positions of the child time resolution in the parent time resolution, and the total number of children. E.g., for child resolution 'D' and parent resoluiton 'W', returns an array integers in the range 0, …, 6 and a number of children equal to 7.

jztools.datetime64.weekday(dates: ndarray, *args)#

Returns the day of the week as an integer in 0,..,6, with 0 corresponding to the day of the week of the specified reference date (defaults to Monday as 0).

Parameters:

ref_date – Same as get_time_slot().

from jztools.datetime64 import weekday

assert weekday(np.array(['2018-01-01'], dtype=='datetime64')) == 0
jztools.datetime64.monthday(dates: ndarray)#

Returns the day of the month as an integer in 0,..,30.

jztools.datetime64.month(dates: ndarray, *args)#

Returns the month as an integer in 0,..,11.

jztools.datetime64.as_datetime(dt64: datetime64, tzinfo: str | tzinfo | None = None) date | datetime#

Converts the np.datetime64 object to a datetime.datetime, for dtypes containing hour or higher resolution, or datetime.date object otherwise.

If the returned object is a datetime.datetime, it will be localized to the specified tzinfo, if any.

jztools.datetime64.as_naive_utc(in_datetime: datetime64 | datetime | str, in_tzinfo: str | tzinfo | None = None) datetime64#

Same as as_naive() but with out_tzinfo = pytz.UTC.

jztools.datetime64.as_naive(in_datetime: datetime64 | datetime | str, in_tzinfo: str | tzinfo | None = None, out_tzinfo: str | tzinfo | None = None) datetime64#

Takes an input datetime (as an str, datetime or datetime64) in the specified input timezone and converts it into a naive datetime64 in the specified output timezone. The input datetime can be naive or timezone-aware. If naive, the in_tzinfo parameter must be supplied. Otherwise, that parameter is ignored.

Returns:

If the input contains a time-less date (as a string or a datetime.date object, or an np.datetime64 object of dtype 'datetime64[D]' or lower resolution), that same date is returned as a np.datetime64 object

of dtype ‘datetime64[D]’ (or of the same dtype, for np.datetime64 inputs. If the input does contain a time, the output dtype is np.datetime64[us].

Parameters:
  • in_datetime – A FlexDateTime with the input datetime.

  • in_tzinfo – If in_datetime is naive, this will need to be supplied. Otherwise, it is ignored.

  • out_tzinfo – The output tzinfo of the supplied datetime object.

jztools.datetime64.utc_from_eastern(in_datetime: datetime64 | datetime | str)#

Converts a datetime from US/Eastern (standard or dalyight saving depending on the time) to UTC.

This function is undefined for one hour where it is not possible to infer whether the input datetime is in standard time daylight savings time. This should not be a problem if the times are outside the overlapping hour each year between the two timezones.

jztools.datetime64.parse_numpy_time_dtype(val: datetime64 | timedelta64) dict#

Returns a dictionary with fields ‘type’ (‘datetime64’ or ‘timedelta64’), ‘num’ (number of date time units) and ‘dt_unit’ (datetime unit, e.g., ‘D’, ‘m’, ‘s’, ‘h’)

jztools.datetime64.now(res='us', tz: str | tzinfo | None = None)#
Parameters:

res – A numpy datetime64 specifier, e.g., one of (‘H’, ‘m’, ‘s’, ‘ms’, ‘us’, ‘ns’).

jztools.datetime64.now_str(*args, **kwargs)#

Returns now() as a string with 'T' substituted by a space.