These are chat archives for data-8/datascience

21st
Dec 2015
Carl Boettiger
@cboettig
Dec 21 2015 23:18
@choldgraf @SamLau95 Hmm, somehow I can no longer do an apply with a function that takes multiple arguments:
letter = ['a', 'b', 'c', 'z']
count = [9, 3, 3, 1]
points = [1, 2, 2, 10]

t = Table([letter, count, points], ['letter', 'count', 'points'])

# Works
t.apply(lambda x: x * x, 'count')

## throws error
t.apply(lambda x, y: x * y, ['count', 'points'])
Error is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-8fe518b79c55> in <module>()
      7 t.apply(lambda x: x * x, 'count')
      8 
----> 9 t.apply(lambda x, y: x * y, ['count', 'points'])
     10 
     11 #array([ 9,  6,  6, 10])

/usr/local/lib/python3.4/dist-packages/datascience/tables.py in apply(self, fn, column_label)
    447         """Returns an array where fn is applied to each element
    448         of a specified column."""
--> 449         return np.array([fn(v) for v in self[column_label]])
    450 
    451     ############

/usr/local/lib/python3.4/dist-packages/datascience/tables.py in __getitem__(self, label)
    352 
    353     def __getitem__(self, label):
--> 354         return self.values(label)
    355 
    356     def __setitem__(self, label, values):

/usr/local/lib/python3.4/dist-packages/datascience/tables.py in values(self, label)
    438             An instance of ``numpy.array``.
    439         """
--> 440         return self._columns[label]
    441 
    442     def column_index(self, column_label):

TypeError: unhashable type: 'list'