Annie
Annie

Reputation: 11

How to convert Darts TimeSeries to a Series or List?

I'm a big fan of darts (https://github.com/unit8co/darts). But I don't know how to change TimeSeries into the list / dataframe / Series.

I want to extract [771426.]:

I want to extract [771426.]

Until now, I used 2 ways. Make a new csv file from TimeSeries, and just use regex. If you know how to convert darts TimeSeries to a list(or anyother type), please help me!

Upvotes: 0

Views: 1630

Answers (3)

Nor.Z
Nor.Z

Reputation: 1419

besides ts.pd_series()

if you have multiple components, try ts.data_array().to_series()

eg, for difference:

ts = TimeSeries.from_dataframe(pd.DataFrame([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], index=pd.date_range(start="1970-01-01 00:00:00", periods=10, freq="s")))
ts2 = TimeSeries.from_dataframe(pd.DataFrame([20, 21, 22, 23, 24, 25, 26, 27, 28, 29], index=pd.date_range(start="1970-01-01 00:00:00", periods=10, freq="s")))

...


display_inGrid(
    [
        ts.pd_dataframe(),
        ts.data_array().to_series(),
        ts.pd_series(),
    ]
)

display_inGrid(
    [
        # ts.stack(ts2).pd_dataframe(),
        # (ts.stack(ts2)).data_array().to_series(),
        # (ts.stack(ts2)).pd_series() # ERROR:darts.timeseries:AssertionError: Only univariate TimeSeries instances support this method
        darts.concatenate([ts, ts2], axis=1).pd_dataframe(),
        darts.concatenate([ts, ts2], axis=1).data_array().to_series(),
        # darts.concatenate([ts, ts2], axis=1).pd_series(),  # ERROR:darts.timeseries:AssertionError: Only univariate TimeSeries instances support this method
    ]
)

enter image description here

Upvotes: 0

Bogdan Tesileanu
Bogdan Tesileanu

Reputation: 126

In addition to converting it into an array using univariate_values() you can do the following:

To convert to a list: your_timeseries.pd_series().tolist()

To convert to a Pandas Series: your_timeseries.pd_series()

To convert to a Pandas Dataframe: your_timeseries.pd_dataframe()

To convert to an array when you have more than one sample (for stochastic series): your_timeseries.all_values()

For more information, you can find the documentation for TimeSeries here: https://unit8co.github.io/darts/generated_api/darts.timeseries.html

Upvotes: 2

Diego Luiz
Diego Luiz

Reputation: 11

I had the same question, and after reading the documentation, I managed to do it using the method univariate_values, as shown in the example below:

your_timeseries.univariate_values()

I hope it helps you too.

Upvotes: 1

Related Questions