Skip to content

[Repo Assist] Add Series.diffDate and Series.diffDateOffset for DateTime/DateTimeOffset differences#592

Merged
dsyme merged 5 commits intomasterfrom
repo-assist/fix-issue-565-datetime-diff-2e409f9139c83012
Mar 9, 2026
Merged

[Repo Assist] Add Series.diffDate and Series.diffDateOffset for DateTime/DateTimeOffset differences#592
dsyme merged 5 commits intomasterfrom
repo-assist/fix-issue-565-datetime-diff-2e409f9139c83012

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot commented Mar 9, 2026

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Adds Series.diffDate and Series.diffDateOffset functions that compute the TimeSpan difference between consecutive DateTime / DateTimeOffset values in a series — addressing the feature request in #565.

Motivation

The existing Series.diff uses an F# inline type-constrained implementation requiring homogeneous subtraction (T - T = T). DateTime - DateTime = TimeSpan is heterogeneous, so Series.diff cannot be used for timestamp series and would raise a runtime type error if attempted.

Implementation

The new functions use shift + zipInto internally:

let diffDate offset (series:Series<'K, DateTime>) : Series<'K, TimeSpan> =
    (series, series |> shift offset) ||> zipInto (fun curr prev -> curr - prev)
```

This produces exactly the same semantics as numeric `diff`:

```
result[k] = series[k] - series[k - offset]

For offset = 1, this gives the elapsed time since the previous observation — useful for detecting periodicity, irregular intervals, gaps, etc.

New API surface

F# C# Returns
Series.diffDate offset series series.Diff(offset) Series<'K, TimeSpan>
Series.diffDateOffset offset series series.Diff(offset) Series<'K, TimeSpan>

Usage example

// Detect time intervals between observations
let timestamps =
    series [ 1 => DateTime(2024,1,1,0,0,0)
             2 => DateTime(2024,1,1,1,0,0)   // +1h
             3 => DateTime(2024,1,1,1,30,0)  // +30m
             4 => DateTime(2024,1,1,3,0,0) ] // +1.5h

timestamps |> Series.diffDate 1
// Series(int, TimeSpan):
//   2 -> 01:00:00
//   3 -> 00:30:00
//   4 -> 01:30:00

Trade-offs

  • Adds two new exported functions; naming (diffDate / diffDateOffset) mirrors the existing diff naming convention.
  • Implementation is slightly less efficient than the low-level index-shift path used by numeric diff, but the difference is negligible for typical use cases and the clarity is worth it.

Test Status

Build: ✅ dotnet build src/Deedle/Deedle.fsproj -c Release — succeeded, 0 errors.

Tests: ✅ 4 new tests added in tests/Deedle.Tests/Series.fs; all pass. Full suite: 467 passing (1 pre-existing failure in "Can expand properties based on runtime type information" — unrelated to this PR).

Note: dotnet fake build has an infrastructure issue in this environment (FAKE TypeInitializationException); the dotnet build / dotnet test paths were used instead.

Closes #565

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@30f2254f2a7a944da1224df45d181a3f8faefd0d

…fset differences

Adds two new F# module functions and corresponding C# extension methods
for computing TimeSpan differences between consecutive DateTime and
DateTimeOffset values in a series.

- Series.diffDate: Series<'K, DateTime> -> Series<'K, TimeSpan>
- Series.diffDateOffset: Series<'K, DateTimeOffset> -> Series<'K, TimeSpan>
- SeriesExtensions.Diff overloads for C# consumers

The existing Series.diff uses an inline type-constrained implementation
that requires the subtraction operator to be homogeneous (T - T = T).
DateTime - DateTime = TimeSpan is heterogeneous, so a separate
implementation using shift + zipInto is required.

Closes #565

Co-authored-by: Copilot <[email protected]>
@dsyme dsyme marked this pull request as ready for review March 9, 2026 13:06
@dsyme dsyme merged commit 388a0ff into master Mar 9, 2026
2 checks passed
@dsyme dsyme deleted the repo-assist/fix-issue-565-datetime-diff-2e409f9139c83012 branch March 9, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Series.Diff method that operates on DateTime to return series of TimeSpan

1 participant