Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.corr results in ValueError: could not convert string to float

I'm getting this very strange error when trying to follow the following exercise on using the corr() method in Python

https://www.geeksforgeeks.org/python-pandas-dataframe-corr/

Specifically, when I try to run the following code: df.corr(method ='pearson')

The error message offers no clue. I thought the corr() method was supposed to automatically ignore strings and empty values etc.

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    df.corr(method='pearson')
  File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 10059, in corr
    mat = data.to_numpy(dtype=float, na_value=np.nan, copy=False)
  File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\frame.py", line 1838, in to_numpy
    result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
  File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\internals\managers.py", line 1732, in as_array
    arr = self._interleave(dtype=dtype, na_value=na_value)
  File "C:\Users\d.o\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\internals\managers.py", line 1794, in _interleave
    result[rl.indexer] = arr
ValueError: could not convert string to float: 'Avery Bradley'
like image 557
Shaye Avatar asked Sep 11 '25 07:09

Shaye


1 Answers

since pandas version 2.0.0 now you need to add numeric_only=True param to avoid the issue

like image 195
German Evangelisti Avatar answered Sep 12 '25 21:09

German Evangelisti