Skip to content

Instantly share code, notes, and snippets.

@mikeywaites
Last active August 29, 2015 13:56
Show Gist options
  • Save mikeywaites/8934034 to your computer and use it in GitHub Desktop.
Save mikeywaites/8934034 to your computer and use it in GitHub Desktop.
ipdb> dataframe = pd.DataFrame.from_records(
Donation.objects.values('amount', 'created'), index='created')
ipdb> df = dataframe.resample('D', how='sum')
ipdb> df.head()
amount
created
2014-02-08 00:00:00+00:00 2000
2014-02-09 00:00:00+00:00 NaN
2014-02-10 00:00:00+00:00 NaN
2014-02-11 00:00:00+00:00 NaN
2014-02-12 00:00:00+00:00 4000
ipdb> df = df.reset_index()
ipdb> df = df.fillna(0)
ipdb> df.head()
created amount
0 2014-02-08 00:00:00 2000
1 2014-02-09 00:00:00 0
2 2014-02-10 00:00:00 0
3 2014-02-11 00:00:00 0
4 2014-02-12 00:00:00 4000
ipdb> df.to_json(orient='values', date_format='iso')
'[["2014-02-08T00:00:00.000Z",2000.0],["2014-02-09T00:00:00.000Z",0.0],["2014-02-10T00:00:00.000Z",0.0],["2014-02-11T00:00:00.000Z",0.0],["2014-02-12T00:00:00.000Z",4000.0]]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment