Skip to content

Bug cov nat #60898

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

Merged
merged 13 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11239,6 +11239,12 @@ def cov(
c -0.150812 0.191417 0.895202
"""
data = self._get_numeric_data() if numeric_only else self
if data.select_dtypes(include=[np.datetime64, np.timedelta64]).shape[1] > 0:
msg = (
"DataFrame contains columns with dtype datetime64[ns] "
"or timedelta64[ns], which are not supported for cov."
)
raise TypeError(msg)
cols = data.columns
idx = cols.copy()
mat = data.to_numpy(dtype=float, na_value=np.nan, copy=False)
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,39 @@ def test_df_empty_nullable_min_count_1(self, opname, dtype, exp_dtype):
expected = Series([pd.NA, pd.NA], dtype=exp_dtype, index=Index([0, 1]))
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"data",
[
{"a": [0, 1, 2], "b": [pd.NaT, pd.NaT, pd.NaT]},
{"a": [0, 1, 2], "b": [Timestamp("1990-01-01"), pd.NaT, pd.NaT]},
{
"a": [0, 1, 2],
"b": [
Timestamp("1990-01-01"),
Timestamp("1991-01-01"),
Timestamp("1992-01-01"),
],
},
{
"a": [0, 1, 2],
"b": [pd.Timedelta("1 days"), pd.Timedelta("2 days"), pd.NaT],
},
{
"a": [0, 1, 2],
"b": [
pd.Timedelta("1 days"),
pd.Timedelta("2 days"),
pd.Timedelta("3 days"),
],
},
],
)
def test_df_cov_pd_nat(self, data):
# GH #53115
df = DataFrame(data)
with pytest.raises(TypeError, match="not supported for cov"):
df.cov()


def test_sum_timedelta64_skipna_false():
# GH#17235
Expand Down
Loading