-
Notifications
You must be signed in to change notification settings - Fork 205
Open
Labels
coreInternal engine: Shape, Storage, TensorEngine, iteratorsInternal engine: Shape, Storage, TensorEngine, iteratorsdocumentation-neededFeature requires documentation after implementation or depiction of lack of documentationFeature requires documentation after implementation or depiction of lack of documentationenhancementNew feature or requestNew feature or requestmissing feature/sNumPy function not yet implemented in NumSharpNumPy function not yet implemented in NumSharp
Milestone
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: [NEP50 Core] Type promotion diverges from NumPy 2.x NEP 50 for unsigned int array + signed int scalar #529
- Will benefit from DynamicMethod IL emission: [Core] Replace ~636K lines of generated math code with DynamicMethod IL emission #544
- Blocked by: datetime64 support ([Types] Add datetime64 support #570) should be implemented together
References
- NumPy timedelta64 docs: https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-and-timedelta-arithmetic
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
coreInternal engine: Shape, Storage, TensorEngine, iteratorsInternal engine: Shape, Storage, TensorEngine, iteratorsdocumentation-neededFeature requires documentation after implementation or depiction of lack of documentationFeature requires documentation after implementation or depiction of lack of documentationenhancementNew feature or requestNew feature or requestmissing feature/sNumPy function not yet implemented in NumSharpNumPy function not yet implemented in NumSharp