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

fix(python): support corr() for single-column DataFrames #13728

Merged
merged 2 commits into from
Jan 15, 2024

Conversation

Wainberg
Copy link
Contributor

I also took the liberty of adding rowvar=False in np.corrcoef(), to avoid having to transpose the array before correlating.

Before:

>>> pl.DataFrame([1, 2, 3]).corr()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "polars/dataframe/frame.py", line 10223, in corr
    return DataFrame(np.corrcoef(self.to_numpy().T, **kwargs), schema=self.columns)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "polars/dataframe/frame.py", line 430, in __init__
    raise TypeError(msg)
TypeError: DataFrame constructor called with unsupported type 'float64' for the `data` parameter

After:

>>> pl.DataFrame([1, 2, 3]).corr()
shape: (1, 1)
┌──────────┐
│ column_0 │
│ ---      │
│ f64      │
╞══════════╡
│ 1.0      │
└──────────┘

@ritchie46
Copy link
Member

Good fix, thanks! Can you add a test?

@Wainberg
Copy link
Contributor Author

Just added a test :)

@ritchie46 ritchie46 merged commit f21e5c7 into pola-rs:main Jan 15, 2024
11 checks passed
@Wainberg Wainberg deleted the corr-cleanup branch January 15, 2024 23:16
r-brink pushed a commit to r-brink/polars that referenced this pull request Jan 24, 2024
Co-authored-by: Wainberg <m.wainberg@utoronto.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants