Title: Pandas Time Series Basics
Slug: pandas_time_series_basics
Summary: Pandas Time Series Basics
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [15]:
from datetime import datetime
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as pyplot
In [16]:
data = {'date': ['2014-05-01 18:47:05.069722', '2014-05-01 18:47:05.119994', '2014-05-02 18:47:05.178768', '2014-05-02 18:47:05.230071', '2014-05-02 18:47:05.230071', '2014-05-02 18:47:05.280592', '2014-05-03 18:47:05.332662', '2014-05-03 18:47:05.385109', '2014-05-04 18:47:05.436523', '2014-05-04 18:47:05.486877'],
'battle_deaths': [34, 25, 26, 15, 15, 14, 26, 25, 62, 41]}
df = pd.DataFrame(data, columns = ['date', 'battle_deaths'])
print(df)
In [17]:
df['date'] = pd.to_datetime(df['date'])
In [18]:
df.index = df['date']
del df['date']
df
Out[18]:
In [19]:
df['2014']
Out[19]:
In [20]:
df['2014-05']
Out[20]:
In [21]:
df[datetime(2014, 5, 3):]
Out[21]:
In [22]:
df['5/3/2014':'5/4/2014']
Out[22]:
In [23]:
df.truncate(after='5/3/2014')
Out[23]:
In [24]:
df.ix['5-2014']
Out[24]:
In [25]:
df.groupby(level=0).count()
Out[25]:
In [26]:
df.resample('D').mean()
Out[26]:
In [27]:
df.resample('D').sum()
Out[27]:
In [28]:
df.resample('D').sum().plot()
Out[28]: