Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group DateTime by an arbitrary time interval

Tags:

c#

linq

group-by

I have an IEnumerable of an item class defined like this:

public class Item {
    public DateTime Date { get; private set; }
    public decimal? Value { get; private set; }

    public Item(DateTime date, decimal? value) {
     Date = date;
     Value = value;
    }
}

These items are in a specific time interval (5 minutes for exemple). I need to group them by the date, but changing the interval. For example, if the items are in the following order:

2010-08-24 00:05
2010-08-24 00:10
2010-08-24 00:15
2010-08-24 00:20
2010-08-24 00:25
2010-08-24 00:30

and I want to group them into a 15 minutes interval, the result should look like this:

2010-08-24 00:15
2010-08-24 00:30

The interval is provided by another class, but I can get the milliseconds that represent that interval (for example, Interval.FromMinutes(5).GetMilliseconds() should return 300000). The question is how can I write a grouping function that allows me to do something like this : data = items.GroupBy(t => GroupingFunction(t.DateTime, interval)) and obtain that result?

Update: the interval will not be necessarily in minutes. It could be in hours, minutes or even days.

like image 687
Fernando Avatar asked Aug 24 '10 17:08

Fernando


People also ask

What is grouping data by time intervals?

Grouping data by time intervals is very obvious when you come across Time-Series Analysis. A time series is a series of data points indexed (or listed or graphed) in time order. Most commonly, a time series is a sequence taken at successive equally spaced points in time.

What is time-stamped data?

Time -stamped is data collected at different points in time. These data points typically consist of successive measurements made from the same source over a time interval and are used to track change over time. What analysis do we need for Time-series Data?

What are the problems of time interval analysis?

Aggregating data in the time interval like if you are dealing with price data then problems like total amount added in an hour, or a day. Finding patterns for other features in the dataset based on a time interval.

What is time series data and how is it used?

Time series data also referred to as time -stamped data, is a sequence of data points indexed in time order. Time -stamped is data collected at different points in time. These data points typically consist of successive measurements made from the same source over a time interval and are used to track change over time.


1 Answers

Something like this ?

        DateTime[] dateTimes = new[]
                                   {
                                       new DateTime(2010, 8, 24, 0, 5, 0),
                                       new DateTime(2010, 8, 24, 0, 10, 0),
                                       new DateTime(2010, 8, 24, 0, 15, 0),
                                       new DateTime(2010, 8, 24, 0, 20, 0),
                                       new DateTime(2010, 8, 24, 0, 25, 0),
                                       new DateTime(2010, 8, 24, 0, 30, 0)
                                   };
        TimeSpan interval = new TimeSpan(0, 15, 0);     // 15 minutes.

        var groupedTimes = from dt in dateTimes
                           group dt by dt.Ticks/interval.Ticks
                           into g
                           select new {Begin = new DateTime(g.Key*interval.Ticks), Values = g.ToList()};

        foreach (var value in groupedTimes)
        {
            Console.WriteLine(value.Begin);
            Console.WriteLine("\t{0}", String.Join(", ", value.Values));
        }
like image 176
driis Avatar answered Oct 17 '22 16:10

driis