I have the following timeseries list in python:
list = [(datetime.datetime(2008, 7, 15, 15, 0), 0.134),
(datetime.datetime(2008, 7, 15, 16, 0), 0.0),
(datetime.datetime(2008, 7, 15, 17, 0), 0.0),
(datetime.datetime(2008, 7, 15, 18, 0), 0.0),
(datetime.datetime(2008, 7, 15, 19, 0), 0.0),
(datetime.datetime(2008, 7, 15, 20, 0), 0.0),
(datetime.datetime(2008, 7, 15, 21, 0), 0.0),
(datetime.datetime(2008, 7, 15, 22, 0), 0.0),
(datetime.datetime(2008, 7, 15, 23, 0), 0.0),
(datetime.datetime(2008, 7, 16, 0, 0), 0.0)]
This list is a key value pair where key is datetime and value is the one after that separated by comma. I want to create pandas series from keys (datetime) and values (decimal value). Anyone can help me to split the above list of time series value into two list (list1 and list2) so I can creare the pandas Series object for further analysis from the following code?
import pandas as pd
ts = pd.Series(list1, list2)
Pandas Series can be created in different ways, here we will see how to create a pandas Series object with a python list. To create a pandas series we have pandas. Series() function from pandas functionalities. Let's take an example and create a simple pandas Series using a python list.
To convert a pandas Series to a list, simply call the tolist() method on the series which you wish to convert.
We use series() function of pandas library to convert a dictionary into series by passing the dictionary as an argument. Let's see some examples: Example 1: We pass the name of dictionary as an argument in series() function. The order of output will be same as of dictionary.
To make a series from a dictionary, simply pass the dictionary to the command pandas. Series method. The keys of the dictionary form the index values of the series and the values of the dictionary form the values of the series.
In [34]: pd.Series(*zip(*((b,a) for a,b in data)))
Out[34]:
2008-07-15 15:00:00 0.134
2008-07-15 16:00:00 0.000
2008-07-15 17:00:00 0.000
2008-07-15 18:00:00 0.000
2008-07-15 19:00:00 0.000
2008-07-15 20:00:00 0.000
2008-07-15 21:00:00 0.000
2008-07-15 22:00:00 0.000
2008-07-15 23:00:00 0.000
2008-07-16 00:00:00 0.000
dtype: float64
Or, eschewing the insane desire to make one-liners:
dates, vals = zip(*data)
s = pd.Series(vals, index=dates)
If the data is extremely long, you can avoid creating the intermediate tuples by using itertools.izip:
import itertools as IT
dates, vals = IT.izip(*data)
s = pd.Series(vals, index=dates)
You can use zip
and splat
to unpack your arguments as below.
import pandas as pd
my_list = [(datetime.datetime(2008, 7, 15, 15, 0), 0.134),
(datetime.datetime(2008, 7, 15, 16, 0), 0.0),
(datetime.datetime(2008, 7, 15, 17, 0), 0.0),
(datetime.datetime(2008, 7, 15, 18, 0), 0.0),
(datetime.datetime(2008, 7, 15, 19, 0), 0.0),
(datetime.datetime(2008, 7, 15, 20, 0), 0.0),
(datetime.datetime(2008, 7, 15, 21, 0), 0.0),
(datetime.datetime(2008, 7, 15, 22, 0), 0.0),
(datetime.datetime(2008, 7, 15, 23, 0), 0.0),
(datetime.datetime(2008, 7, 16, 0, 0), 0.0)]
ts = pd.Series(zip(*my_list))
zip(*my_list)
effectively creates two tuples out of your data, one is a tuple of your datetime
objects, one is your values. These two are then passed as the arguments to pd.Series
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With