Skip to content

Commit 50322e6

Browse files
authored
Merge pull request #1 from mdhaber/gh16238
MAINT: stats._contains_nan: only detect numeric np.nan, not string 'nan'
2 parents 2ffda61 + d5eb000 commit 50322e6

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

scipy/stats/_stats_py.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ def _contains_nan(a, nan_policy='propagate'):
103103
try:
104104
# This can happen when attempting to check nan with np.isnan
105105
# for string array (e.g. as in the function `rankdata`).
106-
contains_nan = np.any(a == "nan")
106+
contains_nan = False
107+
for el in a.ravel():
108+
# isnan doesn't work on elements of string arrays
109+
if np.issubdtype(type(el), np.number) and np.isnan(el):
110+
contains_nan = True
111+
break
107112
except TypeError:
108113
# Don't know what to do. Fall back to omitting nan values and
109114
# issue a warning.

scipy/stats/tests/test_stats.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7550,8 +7550,11 @@ def test_contains_nan_1d(self):
75507550
data3 = np.array([np.nan, 2, 3, np.nan])
75517551
assert _contains_nan(data3)[0]
75527552

7553-
data4 = np.array([1, 2, "3", np.nan])
7554-
assert _contains_nan(data4)[0]
7553+
data4 = np.array([1, 2, "3", np.nan]) # converted to string "nan"
7554+
assert not _contains_nan(data4)[0]
7555+
7556+
data5 = np.array([1, 2, "3", np.nan], dtype='object')
7557+
assert _contains_nan(data5)[0]
75557558

75567559
def test_contains_nan_2d(self):
75577560
data1 = np.array([[1, 2], [3, 4]])
@@ -7560,5 +7563,8 @@ def test_contains_nan_2d(self):
75607563
data2 = np.array([[1, 2], [3, np.nan]])
75617564
assert _contains_nan(data2)[0]
75627565

7563-
data3 = np.array([["1", 2], [3, np.nan]])
7564-
assert _contains_nan(data3)[0]
7566+
data3 = np.array([["1", 2], [3, np.nan]]) # converted to string "nan"
7567+
assert not _contains_nan(data3)[0]
7568+
7569+
data4 = np.array([["1", 2], [3, np.nan]], dtype='object')
7570+
assert _contains_nan(data4)[0]

0 commit comments

Comments
 (0)