Title: Group A Time Series With pandas
Slug: pandas_group_by_time
Summary: Group A Time Series With pandas
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon
In [24]:
import pandas as pd
import numpy as np
In [25]:
df = pd.DataFrame()
df['german_army'] = np.random.randint(low=20000, high=30000, size=100)
df['allied_army'] = np.random.randint(low=20000, high=40000, size=100)
df.index = pd.date_range('1/1/2014', periods=100, freq='H')
df.head()
Out[25]:
In [26]:
df.truncate(before='1/2/2014', after='1/3/2014')
Out[26]:
In [27]:
df.index = df.index + pd.DateOffset(months=4, days=5)
In [28]:
df.head()
Out[28]:
In [29]:
df.shift(1).head()
Out[29]:
In [30]:
df.shift(-1).tail()
Out[30]:
In [31]:
df.resample('D').sum()
Out[31]:
In [32]:
df.resample('D').mean()
Out[32]:
In [33]:
df.resample('D').median()
Out[33]:
In [34]:
df.resample('D').median()
Out[34]:
In [35]:
df.resample('D').first()
Out[35]:
In [36]:
df.resample('D').last()
Out[36]:
In [37]:
df.resample('D').ohlc()
Out[37]: