Skip to content

Commit

Permalink
fix(python): support corr() for single-column DataFrames (#13728)
Browse files Browse the repository at this point in the history
Co-authored-by: Wainberg <m.wainberg@utoronto.ca>
  • Loading branch information
Wainberg and Wainberg committed Jan 15, 2024
1 parent da3ae10 commit f21e5c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10220,7 +10220,10 @@ def corr(self, **kwargs: Any) -> DataFrame:
│ 1.0 ┆ -1.0 ┆ 1.0 │
└──────┴──────┴──────┘
"""
return DataFrame(np.corrcoef(self.to_numpy().T, **kwargs), schema=self.columns)
correlation_matrix = np.corrcoef(self.to_numpy(), rowvar=False, **kwargs)
if self.width == 1:
correlation_matrix = np.array([correlation_matrix])
return DataFrame(correlation_matrix, schema=self.columns)

def merge_sorted(self, other: DataFrame, key: str) -> DataFrame:
"""
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/unit/operations/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@


def test_corr() -> None:
df = pl.DataFrame({"a": [1, 2, 3]})
result = df.corr()
expected = pl.DataFrame({"a": [1.0]})
assert_frame_equal(result, expected)

df = pl.DataFrame(
{
"a": [1, 2, 4],
Expand Down

0 comments on commit f21e5c7

Please sign in to comment.