[Types] Add timedelta64 support
Open
@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 differences —
datetime64 - datetime64 = timedelta64 - Time series — Intervals, sampling rates
- Data science — Age calculations, time-to-event
Proposal
Task List
- Create
TimeDelta64struct 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.TimeDelta64toNPTypeCode.cs - Implement unit conversion logic (shared with datetime64)
- Implement arithmetic:
timedelta + timedelta → timedeltatimedelta - timedelta → timedeltatimedelta * scalar → timedeltatimedelta / scalar → timedeltatimedelta / timedelta → float
- Implement comparison operators
- Integrate with datetime64:
datetime + timedelta → datetimedatetime - timedelta → datetimedatetime - 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
- NumPy timedelta64 docs: https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-and-timedelta-arithmetic
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.