Error with CORR function in Pandas

半城伤御伤魂 提交于 2020-01-06 23:49:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!