time series from list of dates-python -


i have list of dates (lista) each entry in represents occurrence. how make time series out of list in python3? sequence of dates on x axis, , frequency of each date on y

lista = [2016-04-05, 2016-04-05, 2016-04-07, 2016-09-10,           2016-03-05, 2016-07-11, 2017-01-01] 

desired output:

[2016-04-05, 2], [2016-04-06, 0], [2016-04-07, 1], [2016-04-08, 0], ……………… .., [2017-01-01, 1] 

desired format of output:

[[date, frequency],....,*] 

i have date code as:

date=pd.date_range('2016-04-05', '2017-01-01', freq='d') print(date) 

which gives:

[2016-04-05, 2016-04-06, 2016-04-07,....,]

i need code below step through date above frequency each date.

for item in lista:     if item>=date[0] , item<date[1]:         print(lista.count(item)) 

using counter collections module straight forward:

code:

dates = [     '2016-04-05',     '2016-04-05',     '2016-04-07',     '2016-09-10',     '2016-03-05',     '2016-07-11',     '2017-01-01' ]  collections import counter counts = counter(dates) print(sorted(counts.items())) 

results:

[('2016-03-05', 1), ('2016-04-05', 2),   ('2016-04-07', 1), ('2016-07-11', 1),   ('2016-09-10', 1), ('2017-01-01', 1)] 

build list on pandas.datetimeindex:

to build list of lists on range of dates easy enough because counter return 0 when indexed value count zero.

# pandas date range dates = pd.date_range('2016-04-05', '2017-01-01', freq='d')  # counter date need counted counts = counter(pd.to_datetime(dates))  # build list using list comprehension of counts @ dates in range date_occurence_sequence = [[d, counts[d]] d in dates] 

add per day dataframe:

and since seem using pandas let's insert occurrence counts data frame indexed per day.

import pandas pd index = pd.date_range('2016-04-05', '2017-01-01', freq='d') df = pd.dataframe([0] * len(index), index=index) df.update(pd.dataframe.from_dict(counter(pd.to_datetime(dates)), 'index'))  print(df.head()) 

results:

              0 2016-04-05  2.0 2016-04-06  0.0 2016-04-07  1.0 2016-04-08  0.0 2016-04-09  0.0 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -