Python / Numpy AttributeError: 'float' object has no attribute 'sin'
This fails for the same reason as:
import numpy as nparr = np.array([1.0, 2.0, 3.0], dtype=object)np.sin(arr)# AttributeError: 'float' object has no attribute 'sin'
When np.sin
is called on an object array, it tries to call the sin
method of each element.
If you know the dtype of θr.values
, you can fix this with:
arr = np.array(θr.values).astype(np.float64) # assuming the type is float64np.sin(arr) # ok!
Quick fix if you know the possible data types of θr:
xr = xw + L*np.sin(θr.astype(float))
Better solution. Get the data type right from the beginning when creating the dataframe.
Instead of:
self.data = pd.DataFrame(index=range(n), columns=columns)...data.iloc[i, :] = new_data_dict
(Which I was using)
Use:
data = pd.DataFrame(index=range(n), columns=columns, dtype=float)
or:
data = pd.DataFrame(data=np.empty((n, len(columns)), columns=columns)
or:
data = pd.DataFrame(np.full((n, len(columns), np.nan), columns=columns)