SciSharp / SciSharp/NumSharp

[Types] Add timedelta64 support

Open
#571 0 comments 0 reactions 1 assignee View on GitHub

@Nucs is already working on this.

Since Feb 18, 2026.

core documentation-needed enhancement missing feature/s
Dominant language
C#
Stars
1.5k
Forks
205
Avg merge
7d 7h
Merged PRs (30d)
2

Description

Overview

Add support for timedelta64 type to NumSharp for duration arithmetic and time series operations.

Problem

NumSharp lacks timedelta support, needed for datetime arithmetic:

>>> import numpy as np
>>> np.timedelta64(30, 'D')
numpy.timedelta64(30,'D')

>>> np.timedelta64(2, 'h') + np.timedelta64(30, 'm')
numpy.timedelta64(150,'m')

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

>>> np.array([1, 2, 3], dtype='timedelta64[D]')
array([1, 2, 3], dtype='timedelta64[D]')

Use cases:

  • Duration arithmetic — Add/subtract time periods
  • Time differencesdatetime64 - datetime64 = timedelta64
  • Time series — Intervals, sampling rates
  • Data science — Age calculations, time-to-event

Proposal

Task List
  • Create TimeDelta64 struct with unit awareness:
    public readonly struct TimeDelta64
    {
        public readonly long Value;  // Duration in units
        public readonly DateTimeUnit Unit;  // ns, us, ms, s, m, h, D, W, M, Y
    }
    
  • Add NPTypeCode.TimeDelta64 to NPTypeCode.cs
  • Implement unit conversion logic (shared with datetime64)
  • Implement arithmetic:
    • timedelta + timedelta → timedelta
    • timedelta - timedelta → timedelta
    • timedelta * scalar → timedelta
    • timedelta / scalar → timedelta
    • timedelta / timedelta → float
  • Implement comparison operators
  • Integrate with datetime64:
    • datetime + timedelta → datetime
    • datetime - timedelta → datetime
    • datetime - datetime → timedelta
  • Add np.timedelta64() constructor
  • Update type promotion tables
  • Add tests with NumPy verification
Units (matching NumPy)

Same as datetime64: Y, M, W, D, h, m, s, ms, us, ns

C# Type Mapping
NumPy C# Notes
timedelta64 TimeDelta64 struct Custom with unit
timedelta64 TimeSpan Limited to 100ns, no unit tracking

Implementation Effort

MEDIUM — Similar to datetime64, with additional arithmetic operations.

Estimated: ~300 lines for struct and operations.

Related

  • Part of NumPy 2.x alignment: #529
  • Will benefit from DynamicMethod IL emission: #544
  • Blocked by: datetime64 support (#570) should be implemented together

References

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.