Struct DatetimeMetaData
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
The per-INSTANCE parameter of a datetime64 / timedelta64 descriptor — NumPy's
PyArray_DatetimeMetaData ({ NPY_DATETIMEUNIT base; int num; }): the unit and an integer
multiplier, so datetime64[5m] counts five-minute ticks. This is what makes the datetime pair
PARAMETRIC: M8[ns] and M8[s] share one DTypeMeta and one 8-byte storage but are
different dtypes, and promotion / casting between units is arithmetic on these two fields.
public readonly struct DatetimeMetaData : IEquatable<DatetimeMetaData>
- Implements
- Inherited Members
Remarks
Everything here is a line-for-line port of numpy/_core/src/multiarray/datetime.c (2.4.2):
the metadata grammar [<num><unit>] / [<unit>/<den>]
(parse_datetime_metadata_from_metastr, parse_datetime_extended_unit_from_string,
parse_datetime_unit_from_string, convert_datetime_divisor_to_multiple), the formatting
(metastr_to_unicode), the unit factors (get_datetime_units_factor,
get_datetime_conversion_factor), divisibility (datetime_metadata_divides), the GCD used by
promotion (compute_datetime_metadata_greatest_common_divisor — strict about the non-linear Y/M units
for timedelta operands, relaxed for datetime) and the unit/metadata casting rules
(can_cast_datetime64_units / _metadata, can_cast_timedelta64_units / _metadata).
Every error text is NumPy's verbatim, with one deliberate difference: a zero divisor (M8[s/0]) crashes
the CPython interpreter with an integer division by zero; NumSharp raises the metadata TypeError.
Constructors
DatetimeMetaData(NPY_DATETIMEUNIT, int)
public DatetimeMetaData(NPY_DATETIMEUNIT @base, int num = 1)
Parameters
baseNPY_DATETIMEUNITnumint
Fields
Base
The unit (base in NumPy's struct).
public readonly NPY_DATETIMEUNIT Base
Field Value
Generic
The generic (unbound) metadata — the parameter of a bare M8 / m8.
public static readonly DatetimeMetaData Generic
Field Value
Num
The unit multiplier (num in NumPy's struct); 1 for a plain unit.
public readonly int Num
Field Value
NumUnits
NumPy's NPY_DATETIME_NUMUNITS (one more than the number of real units, because of the gap).
public const int NumUnits = 15
Field Value
Properties
IsGeneric
True for the generic (unbound) unit.
public bool IsGeneric { get; }
Property Value
UnitString
The unit string NumPy's np.datetime_data returns ("ns", "Y", "generic").
public string UnitString { get; }
Property Value
Methods
CanCastDatetime64Metadata(in DatetimeMetaData, in DatetimeMetaData, NPY_CASTING)
Port of can_cast_datetime64_metadata: the unit rule, and under safe also exact divisibility.
public static bool CanCastDatetime64Metadata(in DatetimeMetaData src, in DatetimeMetaData dst, NPY_CASTING casting)
Parameters
srcDatetimeMetaDatadstDatetimeMetaDatacastingNPY_CASTING
Returns
CanCastDatetime64Units(NPY_DATETIMEUNIT, NPY_DATETIMEUNIT, NPY_CASTING)
Port of can_cast_datetime64_units: unsafe allows everything; same_kind allows any
unit pair (generic only as the SOURCE); safe only towards more precise units
(src_unit <= dst_unit, generic only as the source); no / equiv require equality.
public static bool CanCastDatetime64Units(NPY_DATETIMEUNIT srcUnit, NPY_DATETIMEUNIT dstUnit, NPY_CASTING casting)
Parameters
srcUnitNPY_DATETIMEUNITdstUnitNPY_DATETIMEUNITcastingNPY_CASTING
Returns
CanCastTimedelta64Metadata(in DatetimeMetaData, in DatetimeMetaData, NPY_CASTING)
Port of can_cast_timedelta64_metadata: the timedelta unit rule, and under safe also strict divisibility.
public static bool CanCastTimedelta64Metadata(in DatetimeMetaData src, in DatetimeMetaData dst, NPY_CASTING casting)
Parameters
srcDatetimeMetaDatadstDatetimeMetaDatacastingNPY_CASTING
Returns
CanCastTimedelta64Units(NPY_DATETIMEUNIT, NPY_DATETIMEUNIT, NPY_CASTING)
Port of can_cast_timedelta64_units: as the datetime rule, plus the hard barrier between the
"date units" (years, months) and the "time units" (weeks and finer) for every rule but unsafe.
public static bool CanCastTimedelta64Units(NPY_DATETIMEUNIT srcUnit, NPY_DATETIMEUNIT dstUnit, NPY_CASTING casting)
Parameters
srcUnitNPY_DATETIMEUNITdstUnitNPY_DATETIMEUNITcastingNPY_CASTING
Returns
Divides(in DatetimeMetaData, in DatetimeMetaData, bool)
Port of datetime_metadata_divides: whether divisor divides evenly into
dividend — the "is casting towards this finer unit exact" test. Generic dividends divide
into everything; nothing specific divides into generic. Years/months are incompatible with every other unit
(except each other) when strictWithNonlinearUnits — the timedelta rule; the datetime rule
says "yes" there ("could do something complicated").
public static bool Divides(in DatetimeMetaData dividend, in DatetimeMetaData divisor, bool strictWithNonlinearUnits)
Parameters
dividendDatetimeMetaDatadivisorDatetimeMetaDatastrictWithNonlinearUnitsbool
Returns
Equals(DatetimeMetaData)
Indicates whether the current object is equal to another object of the same type.
public bool Equals(DatetimeMetaData other)
Parameters
otherDatetimeMetaDataAn object to compare with this object.
Returns
Equals(object)
Indicates whether this instance and a specified object are equal.
public override bool Equals(object obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
GreatestCommonDivisor(in DatetimeMetaData, in DatetimeMetaData, bool, bool)
Port of compute_datetime_metadata_greatest_common_divisor — the metadata of the promoted dtype of
two temporal operands: the GCD of the two multipliers expressed in the finer unit ([h] + [30m]
→ [30m], [10s] + [15s] → [5s]). A generic operand adopts the other's metadata.
Years/months are compatible only with each other; mixing them with a linear unit is an error when the
corresponding operand is strict (a timedelta64) and relaxed otherwise (a datetime64).
public static DatetimeMetaData GreatestCommonDivisor(in DatetimeMetaData meta1, in DatetimeMetaData meta2, bool strictWithNonlinearUnits1, bool strictWithNonlinearUnits2)
Parameters
meta1DatetimeMetaDatameta2DatetimeMetaDatastrictWithNonlinearUnits1boolstrictWithNonlinearUnits2bool
Returns
Exceptions
- TypeError
Cannot get a common metadata divisor for Numpy datetime metadata [Y] and [D] because they have incompatible nonlinear base time units.- OverflowException
Integer overflow getting a common metadata divisor for NumPy datetime metadata [as] and [Y].
Parse(string)
Parses a metadata string — the part of a datetime typestr after M8 / datetime64:
"" (generic), "[ns]", "[10ns]", "[s/2]" (a divisor, rewritten to a multiple of a
finer unit: [500ms]). Port of parse_datetime_metadata_from_metastr.
public static DatetimeMetaData Parse(string metastr)
Parameters
metastrstring
Returns
Exceptions
- TypeError
NumPy's verbatim
Invalid datetime metadata string "…"[ at position N]/Invalid datetime unit in metadata string "…".- ValueError
A divisor that is not a multiple of a lower unit, or a divisor on generic units.
ParseUnit(string)
Parses a bare unit string ("ns", "Y", "μs", "generic") — port of
parse_datetime_unit_from_string with no surrounding metadata string.
public static NPY_DATETIMEUNIT ParseUnit(string unit)
Parameters
unitstring
Returns
Exceptions
- TypeError
Invalid datetime unit "…" in metadata.
ToString()
The bracketed metadata string ("[ns]", "[10ns]", "" for generic).
public override string ToString()
Returns
ToString(bool)
NumPy's metastr_to_unicode: "[ns]" / "[10ns]", and "" for generic units — or,
with skipBrackets, "ns" / "10ns" / "generic".
public string ToString(bool skipBrackets)
Parameters
skipBracketsbool
Returns
Operators
operator ==(DatetimeMetaData, DatetimeMetaData)
public static bool operator ==(DatetimeMetaData left, DatetimeMetaData right)
Parameters
leftDatetimeMetaDatarightDatetimeMetaData
Returns
operator !=(DatetimeMetaData, DatetimeMetaData)
public static bool operator !=(DatetimeMetaData left, DatetimeMetaData right)
Parameters
leftDatetimeMetaDatarightDatetimeMetaData