Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: spearman correlation doesn't work on non-numeric data #60306

Open
1 task done
Desiment opened this issue Nov 13, 2024 · 3 comments · May be fixed by #60493
Open
1 task done

BUG: spearman correlation doesn't work on non-numeric data #60306

Desiment opened this issue Nov 13, 2024 · 3 comments · May be fixed by #60493

Comments

@Desiment
Copy link

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.corr.html#pandas.DataFrame.corr

Documentation problem

Since rank correlation coefficients make sense also for ordinal data, documentation for option numeric_true is a little confusing.

Currently, it's saying the following:

numeric_only : bool, default False
    Include only `float`, `int` or `boolean` data.

When ordinal data (categorical dtype with defined order) presented in dataframe, it seems natural to expect that rank correlation still be computed when using numeric_only = False. For example, something like this should work:

import pandas as pd
df = pd.DataFrame({'workweek' : [4, 5, 6, 4], 'income' : ['low', 'middle', 'high', 'low']})
df['income'] = df['income'].astype('category').cat.set_categories(['low', 'middle', 'high'], ordered=True)
df.corr(method='spearman')

Yet it throws ValueError, since it cannot convert string low to float. Moreover, using numerical categories with the specific order results in incorrect behavior. For example,

import pandas as pd
df = pd.DataFrame({'a' : [1, 2, 3, 4], 'b' : [4, 3, 2, 1]})
df['b'] = df['b'].astype('category').cat.set_categories([4, 3, 2, 1], ordered=True)
df.corr(method='spearman')

returns that Spearman's correlation between a and b is equal to -1, while real value is equal to 1.

Suggested fix for documentation

I believe, that following additional sentence makes things a bit less confusing.

numeric_only : bool, default False
     Include only `float`, `int` or `boolean` data. If value is False, method will try cast non-numerical 
     columns to float (note that ordinal data, if possible, will be converted ignoring specified order)
@Desiment Desiment added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 13, 2024
@rhshadrach
Copy link
Member

Thanks for the report. It appears to me that these (spearman on non-numeric data and categorical order not being respected) are bugs. I think the latter is covered in #33245, so we can have this issue be above the former.

@rhshadrach rhshadrach added Bug cov/corr and removed Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 14, 2024
@rhshadrach rhshadrach changed the title DOC: numeric_only option for DataFrame.corr is unclear BUG: spearman correlation doesn't work on non-numeric data Nov 14, 2024
@micheleuap
Copy link

micheleuap commented Nov 29, 2024

Supporting (ordered) categorical variables should be relatively simple -
i.e. just a matter of replacing them with their codes/orders before converting them into a numpy array

Let me know if I should go ahead and make a PR to this effect and add some tests

@rhshadrach
Copy link
Member

@micheleuap - PRs to fix are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants