diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 9dfa4a9486558..f584c0ff9f614 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -391,10 +391,11 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None): # clip `covxy / divisor` to ensure coeff is within bounds if divisor != 0: val = covxy / divisor - if val > 1.0: - val = 1.0 - elif val < -1.0: - val = -1.0 + if not cov: + if val > 1.0: + val = 1.0 + elif val < -1.0: + val = -1.0 result[xi, yi] = result[yi, xi] = val else: result[xi, yi] = result[yi, xi] = NaN diff --git a/pandas/tests/frame/methods/test_cov_corr.py b/pandas/tests/frame/methods/test_cov_corr.py index 6dfbc325aafa4..304638a3a7dcf 100644 --- a/pandas/tests/frame/methods/test_cov_corr.py +++ b/pandas/tests/frame/methods/test_cov_corr.py @@ -497,3 +497,13 @@ def test_corr_within_bounds(self): corr_matrix = df2.corr() assert corr_matrix.min().min() >= -1.0 assert corr_matrix.max().max() <= 1.0 + + def test_cov_with_missing_values(self): + df = DataFrame({"A": [1, 2, None, 4], "B": [2, 4, None, 9]}) + expected = DataFrame( + {"A": [2.333333, 5.500000], "B": [5.5, 13.0]}, index=["A", "B"] + ) + result1 = df.cov() + result2 = df.dropna().cov() + tm.assert_frame_equal(result1, expected) + tm.assert_frame_equal(result2, expected)