Hi I'm currently using the following function to get complaints on certain drugs over time. However the output gives me the number of reported complaints on a daily basis. It would help me a great deal if i could get the number of reported complaints on a monthly basis.


If anyone can help it would be much appreciated.




Here is a small Python snippet that takes your URL and produces a output that you can load into Excel.

import requests
from collections import defaultdict
monthly = defaultdict(int)

url = 'https://api.fda.gov/drug/event.json?search=receivedate:[20110101+TO+20150101]+AND+brand_name:lantus&count=receivedate'
r = requests.get(url)
r = r.json()
data = r.get(u'results',u'')

for item in data:
    count = item.get(u'count',u'')
    ddate = item.get(u'time',u'')
    if ddate != u'' and count != u'':
        dkey = ''.join(ddate[0:4])+'-'+''.join(ddate[4:6])
        monthly[dkey] += int(count)

for item in monthly:
    print item, monthly.get(item)

Gives as an output:

2011-08 569
2011-09 505
2011-02 432
2011-03 462
2011-01 439
2011-06 560

Which is in an Excel-importable format.

| improve this answer | |
  • I really have to switch to python 3 – philshem Aug 21 '14 at 18:16

If you're just looking to see stats by month we have graphs on ResearchAE.com

Disclosure: I created ResearchAE.com

| improve this answer | |
  • @Skram : do you have evidence that Brian created ResearchAE? There's nothing in his profile to indicate so. 'We' can mean many different things. (eg, he might assist with the website, but not have been involved in the creation). It's better to point people to the posting guidelines, and give them a chance to edit their post, rather than you taking it upon yourself to do it. – Joe Aug 15 '14 at 14:14
  • see opendata.stackexchange.com/help/behavior ... I personally wouldn't consider this 'overt self-promotion' with only one post, personally. – Joe Aug 15 '14 at 14:16
  • @Joe I know Brian personally and it looks like he answered without logging into his SE account (opendata.stackexchange.com/users/3137/brian) – Mark Silverberg Aug 15 '14 at 14:18
  • @Brian : see meta.stackexchange.com/q/18232 for how to merge your two accounts. – Joe Aug 15 '14 at 14:24

There is no API endpoint or query to aggregate by month. You can submit a feature request to https://github.com/fda/openfda/issues however.

We had this same question/issue and identified two potential solutions:

  1. Like @phileshem proposed in his answer to your question, you can aggregate in your application code. Phil provided an example in Python. I have Ruby code somewhere if you need/prefer.
  2. Much less optimally for both you and the openFDA web services is to do many receivedate:[X to Y] API calls. I would suggest against this but it is an option.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.