TypeError: 'type' object has no attribute '__getitem__'

ayeomans picture ayeomans · Jan 26, 2013 · Viewed 9.7k times · Source

I keep getting an error when trying to run the rmakeprofile command. I get an error saying that 'type' object has no attribute '__getitem__'.

from array import array
from ROOT import gROOT, TCanvas, TProfile, TGraph

class Data(object):
    def __init__(self, s):
        self.p = TProfile()
        self.data = []
        for line in s:
            if not line.startswith("#"):    #Removes Commented lines
                columns = line.split(',')   #Splits into Columns
                if columns:
                    datum = {
                          "threshold" : float(columns[1]),
                          "count" : float(columns[2]),
                          "rate" : float(columns[2]) /float(columns[0]),
                          "scantime" : float(columns[0])
                          }
                    self.data.append(datum)
                    print columns[1], float(columns[2])/float(columns[0])

    def rmakeprofile(self, data, xval, yval, noBins):
        self.a = array('d')
        for datum in data:
            self.a.append(float(datum[xval]))
        self.p = TProfile('p','',noBins,min(self.a),max(self.a))
        for datum in  data:
            self.p.Fill(datum[xval],datum[yval])
        return self.p

Here is the traceback:

p = d.rmakeprofile(data,"threshold","rate",13)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ray.py", line 27, in rmakeprofile
self.a = array('d')
TypeError: 'type' object has no attribute '__getitem__'

Answer

surendraprasad baskaran picture surendraprasad baskaran · Feb 22, 2016

Try using the following replacements.

    import numpy as np
    self.a = np.asarray(['d'])

or even this also works

    import numpy as np
    self.a = np.asarray('d')