Andrea Cuneo
Andrea Cuneo

Reputation: 69

Resample time serie with Deedle

I want to resample a TimeSerie to a greater time granularity. I'm not sure which is the best way in Deedle to do so under the condition below.

Let's assume I've a daily time serie from 2014-03-15 to 2014-09-15 generated as follows:

var startDate = DateTime.Parse('2014-03-15');
var daily = Enumerable.Range(0, 60)
            .Select(x => KeyValue.Create(startDate.AddDays(x), 15.03))
            .Concat(Enumerable.Range(120, 60)
                    .Select(x => KeyValue.Create(startDate.AddDays(x), 15.03)))
            .ToSeries();

This (more or less) translate to a daily time serie continuos from 2014-03-15 to 2014-05-15, then missing, and then continuos from 2014-07-15 to 2014-09-15.

I want to aggregate it as a monthly time serie, from 2014-03 to 2014-09 included, as the mean of underlining. Considering it is a sorted series, I'd like to avoid the costly GroupBy and using a Sampling function.

My best solution is:

var monthly = daily.ResampleEquivalence(x => x.FirstDayOfTheMonth(), s => s.Mean());

This return a serie, correctly, but missing data for key 2014-06.

I'd like to use ResampleUniform but in c# doesn't provides an overload with a callback function for computing the mean...

Any suggestion on how to emulate the F# resampleUniformInto in C#?

Upvotes: 0

Views: 1217

Answers (1)

casbby
casbby

Reputation: 896

Not exactly emulating F# resampleUniformInto but as a workaround:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace resample2
{
    class Program
    {
        static void Main(string[] args)
        {
            var startDate = DateTime.ParseExact("2014-03-15", "yyyy-MM-dd", CultureInfo.InvariantCulture);
            var daily = Enumerable.Range(0, 60)
                        .Select(x => KeyValue.Create(startDate.AddDays(x), 15.03))
                        .Concat(Enumerable.Range(120, 60)
                        .Select(x => KeyValue.Create(startDate.AddDays(x), 15.03)))
                        .ToSeries();

            var monthly = daily.ResampleEquivalence(x => x.ToString("yyyy-MM") , s => s.Mean());

            monthly.Print();

            var keys = Enumerable.Range(0, 360)
                      .Select(x => startDate.AddDays(x).Date)
                      .Where(x => x.Day == 1)
                      .Select(x=> x.ToString("yyyy-MM"));


            var monthly2 = daily.ResampleEquivalence(x => x.ToString("yyyy-MM"), s => s.Mean()).Realign(keys);



            monthly2.Print();

            Console.ReadKey();
        }

    }
}

enter image description here

Upvotes: 1

Related Questions