问题
I had a CSV file of 2 stock prices data which I did some native python coding to create 2 single lists of prices (in Decimals) of 2 stock counters.
I then converted them to 2 pandas dataframe, used the .pct_change() function then applied the a.corr(b) function in an attempt to get the correlation.
This is the error msg that I got. Anyone can advise on the issue here?
Traceback (most recent call last):
File "D:/python/NQ_MSFT regression.py", line 71, in <module>
print(nqpct.corr(mspct))
File "C:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4431, in corr
if method == 'pearson':
File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 731, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
回答1:
Ok I realised it works if I parse the list into a pandas series rather than a dataframe & change my initial prices from string to float rather than to decimals. Why doesn't the correlation function work with decimal data types though?
Traceback (most recent call last):
File "D:/python/NQ_MSFT regression.py", line 64, in <module>
nqpct = nq_df.pct_change()
File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 4573, in pct_change
axis=axis, **kwargs)) - 1)
File "C:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 866, in flex_wrapper
return self._binop(other, op, level=level, fill_value=fill_value)
File "C:\Anaconda3\lib\site-packages\pandas\core\series.py", line 1536, in _binop
result = func(this_vals, other_vals)
TypeError: unsupported operand type(s) for /: 'decimal.Decimal' and 'float'
来源:https://stackoverflow.com/questions/34503835/error-with-corr-function-in-pandas