Create weekly time series with DARTS from dataframe

Question:

How to create a time series with darts having weekly frequency?

When having weekly data as follows:

import pandas as pd

df = pd.DataFrame({
    'Date' : [
        pd.to_datetime('2022-12-05'),
        pd.to_datetime('2022-12-12'),
        pd.to_datetime('2022-12-19'),
        pd.to_datetime('2022-12-26')],
    'Variable': [10,21,23,24]}) 

I am struggling to get the correct freq as weekly:

from darts import TimeSeries

# I use freq = day in order to not to get an error.
TimeSeries.from_dataframe(df, 'Date', "Variable", freq="d")
Asked By: PeCaDe

||

Answers:

I’m not sure about your issue. Your data are already on a weekly basis. It seems there is a typo with that date here: pd.to_datetime('2022-12-02'), which should probably be pd.to_datetime('2022-12-02').

I don’t know if that was the purpose, but personally, I would handle the data (missing data, frequency, etc.) with either numpy or pandas before creating a TimeSeries object with darts. Numpy and/or pandas are larger libraries that have functionalities to do that.
It’s also a good practice to check the quality of your data upfront.

Going back to your question, that should do the trick :

    df = pd.DataFrame({
        'Date': [
            pd.to_datetime('2022-12-05'),
            pd.to_datetime('2022-12-12'),
            pd.to_datetime('2022-12-19'),
            pd.to_datetime('2022-12-26')],
        'Variable': [10, 21, 23, 24]})

    df.set_index('Date', inplace=True)  # set Date as index
    ts_object = TimeSeries.from_times_and_values(times=df.index,values=df['Variable'])

I assume you meant 2022-12-12 and not 2022-12-02

Answered By: Philippe Ostiguy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.