Skip to content

[Types] Add datetime64 support #570

Description

@Nucs

Overview

Add support for datetime64 type to NumSharp for time series and data science workloads.

Problem

NumSharp lacks datetime support, which is essential for:

>>> import numpy as np
>>> np.array(['2024-01-15', '2024-06-30'], dtype='datetime64[D]')
array(['2024-01-15', '2024-06-30'], dtype='datetime64[D]')

>>> np.datetime64('2024-01-15') + np.timedelta64(30, 'D')
numpy.datetime64('2024-02-14')

>>> np.arange('2024-01', '2024-06', dtype='datetime64[M]')
array(['2024-01', '2024-02', '2024-03', '2024-04', '2024-05'], dtype='datetime64[M]')

Use cases:

  • Time series analysis — Financial data, sensor data, logs
  • Pandas interop — Pandas uses NumPy datetime64 internally
  • Data science — Date ranges, filtering, aggregation
  • File I/O — Many datasets have datetime columns

Proposal

Task List

  • Design datetime64 representation (epoch + unit)
  • Create DateTime64 struct with unit awareness:
    public readonly struct DateTime64
    {
        public readonly long Value;  // Ticks from epoch
        public readonly DateTimeUnit Unit;  // ns, us, ms, s, m, h, D, W, M, Y
    }
  • Add NPTypeCode.DateTime64 to NPTypeCode.cs
  • Implement unit conversion logic
  • Implement parsing from strings ("2024-01-15", "2024-01-15T12:30:00")
  • Implement arithmetic (datetime + timedelta)
  • Implement comparison operators
  • Add np.datetime64() constructor
  • Update type promotion tables
  • Add tests with NumPy verification

Units (matching NumPy)

Unit Code Description
Y Year
M Month
W Week
D Day
h Hour
m Minute
s Second
ms Millisecond
us Microsecond
ns Nanosecond Default

C# Type Mapping Options

NumPy C# Option Notes
datetime64 DateTime64 struct Custom, epoch-based with unit
datetime64 DateTime Limited to 100ns precision
datetime64 DateTimeOffset Has timezone, may be overkill

Implementation Effort

MEDIUM — Requires unit handling, parsing, arithmetic with timedelta64.

Estimated: ~500 lines for struct, parsing, and operations.

Related

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

coreInternal engine: Shape, Storage, TensorEngine, iteratorsdocumentation-neededFeature requires documentation after implementation or depiction of lack of documentationenhancementNew feature or requestmissing feature/sNumPy function not yet implemented in NumSharp

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions