'I want to transform data frame with date to time series

I've got a pandas.DataFrame named "Prices" with the following data:

DATE            heating.oil     gasoline
2020-02-28      54.700           1.458
2020-03-02      1.521            1.523
2020-03-03      1.515            1.51
2020-03-04      1.523            1.542
...

Now I want to transform them to a time series.

How can I do it?



Solution 1:[1]

Use set_index() to make the date be the data frames (or series) index, then select the columns using Prices['...'].

Prices.set_index("DATE", inplace=True)

heating_oil = Prices['heating.oil']
gasoline = Prices['gasoline']

Output:

DATE
2020-02-28    1.458
2020-03-02    1.523
2020-03-03    1.510
2020-03-04    1.542
Name: gasoline, dtype: float64

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KarelZe