Skip to content

Instantly share code, notes, and snippets.

@jamesattard
Created August 30, 2017 07:45
Show Gist options
  • Select an option

  • Save jamesattard/07ad6a706281c73a399595450b89eee2 to your computer and use it in GitHub Desktop.

Select an option

Save jamesattard/07ad6a706281c73a399595450b89eee2 to your computer and use it in GitHub Desktop.
Pandas binning over timeseries
from datetime import datetime
from datetime import date
import numpy as np
import pandas as pd
data = {
'date': ['2014-05-01', '2014-05-01', '2014-05-02', '2014-05-02', '2014-05-02', '2014-05-03', '2014-05-03', '2014-05-04', '2014-05-04', '2014-05-08'],
'score': [34, 25, 26, 15, 15, 14, 26, 25, 62, 41]
}
bins = [np.datetime64(date(2014,4,30)), np.datetime64(date(2014,5,5)), np.datetime64(date(2014,5,10))]
group_names = ['Alpha', 'Omega']
df = pd.DataFrame(data, columns = ['date', 'score'])
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)
df['categories'] = pd.cut(df.index, bins, labels=group_names)
print(df)
df = df.groupby("categories").sum()
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment