Q:

Categorical aggregation in Histogrammar

This is a followup question of SO Two-dimensional aggregation in Histogrammar (Jim Pivarski created this entry from a private email question):
From: 
data = [{"item": 'ball', "qty": 3.0}, 
        {"item": 'whistle', "qty": 2.0}, 
        {"item": 'ball', "qty": 5.0}]

I want to obtain a sum aggregation using Histogrammar, i.e.:
ball: 8.0
whistle: 20

Following http://histogrammar.org/docs/tutorials/python-numpy/#histogrammar-in-numpy, and Jim's advice in mentioned SO , I try:
import histogrammar as hg
data = [{"item": 'ball', "qty": 3.0}, {"item": 'whistle', "qty": 2.0}, {"item": 'ball', "qty": 5.0}]

h = hg.Categorize(quantity=lambda d: d.item, value=hg.Sum(lambda d: d.qty))
for datum in data:
   h.fill(datum)

 print h.toJson()

I get:
AttributeError: 'dict' object has no attribute 'item'

A:

This is just a Python issue: since each datum in your example has the form
{"item": X, "qty": Y}

the way to access it is with d["item"] and d["qty"], rather than d.item and d.qty.
So
h = hg.Categorize(quantity=lambda d: d["item"], value=hg.Sum(lambda d: d["qty"]))
for datum in data:
    h.fill(datum)

print h.toJsonString()

results in
{"data": {"bins:type": "Sum", "bins": {"whistle": {"sum": 2.0, "entries": 1.0},
 "ball": {"sum": 8.0, "entries": 2.0}}, "entries": 3.0}, "version": "1.0",
 "type": "Categorize"}

If you change the way your data are represented, you'd have to change the way they're extracted from each datum.
Incidentally, Histogrammar-Python has a string-based shortcut that extracts fields as attributes (as you were trying to do) or as items (as I did above). The following would work with either kind of data:
h = hg.Categorize("item", hg.Sum("qty"))

This string-based method will also work if data is a dictionary of 1D Numpy arrays (or equivalently, a Numpy Record Array; I don't remember if there's a Pandas hook in there, too). In that case, you'd declare the histogram exactly as above but fill it like this:
h.fill.numpy(data)

It's the different fill method that interprets the strings differently.

