Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcfe54bd

Browse files
authored
Bug cov nat (#60898)
* ENH: Add TypeError for unsupported datetime64 and timedelta64 dtypes in DataFrame.cov* TST: Add test for TypeError in DataFrame.cov with NaT and Timedelta inputs* BUG: Improve error message for unsupported datetime and timedelta dtypes in cov()* BUG: Handle NaN values for datetime and timedelta dtypes in BlockManager* BUG: Add test for to_numpy() handling of NaT and NaN values* REF: Refactor imports in frame.py and update test imports in test_to_numpy.py* update dtype check* refactor* update test
1 parente2bd8e6 commitcfe54bd

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

‎pandas/core/frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11434,6 +11434,12 @@ def cov(
1143411434
c -0.150812 0.191417 0.895202
1143511435
"""
1143611436
data=self._get_numeric_data()ifnumeric_onlyelseself
11437+
ifany(blk.dtype.kindin"mM"forblkinself._mgr.blocks):
11438+
msg= (
11439+
"DataFrame contains columns with dtype datetime64 "
11440+
"or timedelta64, which are not supported for cov."
11441+
)
11442+
raiseTypeError(msg)
1143711443
cols=data.columns
1143811444
idx=cols.copy()
1143911445
mat=data.to_numpy(dtype=float,na_value=np.nan,copy=False)

‎pandas/core/internals/managers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,8 @@ def as_array(
18001800
arr=np.asarray(blk.values,dtype=dtype)
18011801
else:
18021802
arr=np.array(blk.values,dtype=dtype,copy=copy)
1803+
ifpassed_nanandblk.dtype.kindin"mM":
1804+
arr[isna(blk.values)]=na_value
18031805

18041806
ifnotcopy:
18051807
arr=arr.view()
@@ -1865,6 +1867,8 @@ def _interleave(
18651867
else:
18661868
arr=blk.get_values(dtype)
18671869
result[rl.indexer]=arr
1870+
ifna_valueisnotlib.no_defaultandblk.dtype.kindin"mM":
1871+
result[rl.indexer][isna(arr)]=na_value
18681872
itemmask[rl.indexer]=1
18691873

18701874
ifnotitemmask.all():

‎pandas/tests/frame/methods/test_to_numpy.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
frompandasimport (
55
DataFrame,
6+
NaT,
67
Timestamp,
8+
date_range,
79
)
810
importpandas._testingastm
911

@@ -41,3 +43,37 @@ def test_to_numpy_mixed_dtype_to_str(self):
4143
result=df.to_numpy(dtype=str)
4244
expected=np.array([["2020-01-01 00:00:00","100.0"]],dtype=str)
4345
tm.assert_numpy_array_equal(result,expected)
46+
47+
deftest_to_numpy_datetime_with_na(self):
48+
# GH #53115
49+
dti=date_range("2016-01-01",periods=3)
50+
df=DataFrame(dti)
51+
df.iloc[0,0]=NaT
52+
expected=np.array([[np.nan], [1.45169280e18], [1.45177920e18]])
53+
result=df.to_numpy(float,na_value=np.nan)
54+
tm.assert_numpy_array_equal(result,expected)
55+
56+
df=DataFrame(
57+
{
58+
"a": [Timestamp("1970-01-01"),Timestamp("1970-01-02"),NaT],
59+
"b": [
60+
Timestamp("1970-01-01"),
61+
np.nan,
62+
Timestamp("1970-01-02"),
63+
],
64+
"c": [
65+
1,
66+
np.nan,
67+
2,
68+
],
69+
}
70+
)
71+
expected=np.array(
72+
[
73+
[0.00e00,0.00e00,1.00e00],
74+
[8.64e04,np.nan,np.nan],
75+
[np.nan,8.64e04,2.00e00],
76+
]
77+
)
78+
result=df.to_numpy(float,na_value=np.nan)
79+
tm.assert_numpy_array_equal(result,expected)

‎pandas/tests/frame/test_reductions.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,39 @@ def test_df_empty_nullable_min_count_1(self, opname, dtype, exp_dtype):
19171917
expected=Series([pd.NA,pd.NA],dtype=exp_dtype,index=Index([0,1]))
19181918
tm.assert_series_equal(result,expected)
19191919

1920+
@pytest.mark.parametrize(
1921+
"data",
1922+
[
1923+
{"a": [0,1,2],"b": [pd.NaT,pd.NaT,pd.NaT]},
1924+
{"a": [0,1,2],"b": [Timestamp("1990-01-01"),pd.NaT,pd.NaT]},
1925+
{
1926+
"a": [0,1,2],
1927+
"b": [
1928+
Timestamp("1990-01-01"),
1929+
Timestamp("1991-01-01"),
1930+
Timestamp("1992-01-01"),
1931+
],
1932+
},
1933+
{
1934+
"a": [0,1,2],
1935+
"b": [pd.Timedelta("1 days"),pd.Timedelta("2 days"),pd.NaT],
1936+
},
1937+
{
1938+
"a": [0,1,2],
1939+
"b": [
1940+
pd.Timedelta("1 days"),
1941+
pd.Timedelta("2 days"),
1942+
pd.Timedelta("3 days"),
1943+
],
1944+
},
1945+
],
1946+
)
1947+
deftest_df_cov_pd_nat(self,data):
1948+
# GH #53115
1949+
df=DataFrame(data)
1950+
withpytest.raises(TypeError,match="not supported for cov"):
1951+
df.cov()
1952+
19201953

19211954
deftest_sum_timedelta64_skipna_false():
19221955
# GH#17235

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp