Skip to content

Conversation

Copy link

Copilot AI commented Jul 2, 2025

Implements mutable time series operations by adding an optional inPlace parameter (similar to pandas DataFrame) to avoid performance issues with large time series transformations.

Changes Made

Core Infrastructure

  • Modified IndexedSeries base class to support controlled mutability during inPlace operations
  • Added EnableMutation()/DisableMutation() and UpdateInPlace() protected methods
  • Properly handles Start/End/IsEmpty property updates during mutations

New inPlace Method Overloads

All methods maintain backwards compatibility (inPlace defaults to false):

// Transform operations
ts.Apply(x => x * 2, inPlace: true);              // Modify in place
ts.ApplyValues(x => x + 5, inPlace: true);        // Transform non-null values

// Join operations  
ts.JoinLeft(other, (l, r) => l + r, inPlace: true);     // 3 overloads supported
ts.JoinLeft(other, JoinOperation.Add, inPlace: true);

// Slice operations
ts.Slice(startIndex: 2, count: 5, inPlace: true);

Convenience Methods

Added fluent API methods for common operations with method chaining:

// Scalar operations
ts.AddInPlace(10).MultiplyInPlace(2).SubtractInPlace(5);

// Time series operations  
ts.AddInPlace(otherSeries).DivideInPlace(anotherSeries);

Comprehensive Testing

  • Created TimeSeriesInPlaceTest with 13 test methods covering all scenarios
  • Tests verify reference equality, immutable behavior preservation, null handling, and edge cases

Usage Examples

Before (creates new instances):

var ts = TimeSeries.Factory.FromValue(100, startDate, 1000000, Period.Day);
ts = ts.Apply(x => x * 2);        // Creates new 1M element series
ts = ts.Apply(x => x + 10);       // Creates another new 1M element series  
ts = ts.JoinLeft(other, op);      // Creates yet another new series

After (modifies in place):

var ts = TimeSeries.Factory.FromValue(100, startDate, 1000000, Period.Day);
ts.Apply(x => x * 2, inPlace: true)      // Modifies existing series
  .Apply(x => x + 10, inPlace: true)     // No new allocations
  .JoinLeft(other, op, inPlace: true);   // Still the same instance

Benefits

  • Performance: Eliminates object allocations for large time series operations
  • Memory Efficiency: Reduces memory pressure and GC overhead
  • Backwards Compatibility: All existing code continues to work unchanged
  • Pandas-like API: Familiar interface for users coming from pandas DataFrames
  • Type Safety: Maintains all existing validation and type safety

Fixes #1.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Make time sries objects mutable via additional "inPlace" flag. Add inPlace flag to time series operations for mutable transformations Jul 2, 2025
Copilot AI requested a review from JanDotNet July 2, 2025 16:22
…arp.TimeFlow/Thinksharp.TimeFlow/Thinksharp.TimeFlow.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make time sries objects mutable via additional "inPlace" flag.

2 participants