Error when trying to apply log method to pandas data frame column in Python Error when trying to apply log method to pandas data frame column in Python numpy numpy

Error when trying to apply log method to pandas data frame column in Python


This happens when the datatype of the column is not numeric. Try

arr['retlog'] = log(arr['close'].astype('float64')/arr['close'].astype('float64').shift(1))

I suspect that the numbers are stored as generic 'object' types, which I know causes log to throw that error. Here is a simple illustration of the problem:

In [15]: np.log(Series([1,2,3,4], dtype='object'))---------------------------------------------------------------------------AttributeError                            Traceback (most recent call last)<ipython-input-15-25deca6462b7> in <module>()----> 1 np.log(Series([1,2,3,4], dtype='object'))AttributeError: logIn [16]: np.log(Series([1,2,3,4], dtype='float64'))Out[16]: 0    0.0000001    0.6931472    1.0986123    1.386294dtype: float64

Your attempt with math.log did not work because that function is designed for single numbers (scalars) only, not lists or arrays.

For what it's worth, I think this is a confusing error message; it once stumped me for awhile, anyway. I wonder if it can be improved.