Skip to content

Commit 967c674

Browse files
committed
API: decimal nan is na
1 parent 2f28638 commit 967c674

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,7 @@ Missing
12271227
- Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`)
12281228
- :func:`Series.isin` now treats all NaN-floats as equal also for `np.object`-dtype. This behavior is consistent with the behavior for float64 (:issue:`22119`)
12291229
- :func:`unique` no longer mangles NaN-floats and the ``NaT``-object for `np.object`-dtype, i.e. ``NaT`` is no longer coerced to a NaN-value and is treated as a different entity. (:issue:`22295`)
1230+
- :meth:`isna` now considers ``decimal.Decimal('NaN')`` a missing value (:issue:`23284`).
12301231

12311232

12321233
MultiIndex

pandas/_libs/missing.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import cython
4+
import decimal
45
from cython import Py_ssize_t
56

67
import numpy as np
@@ -33,6 +34,8 @@ cdef inline bint _check_all_nulls(object val):
3334
res = get_datetime64_value(val) == NPY_NAT
3435
elif util.is_timedelta64_object(val):
3536
res = get_timedelta64_value(val) == NPY_NAT
37+
elif isinstance(val, decimal.Decimal):
38+
return val.is_nan()
3639
else:
3740
res = 0
3841
return res
@@ -71,6 +74,8 @@ cpdef bint checknull(object val):
7174
return get_timedelta64_value(val) == NPY_NAT
7275
elif util.is_array(val):
7376
return False
77+
elif isinstance(val, decimal.Decimal):
78+
return val.is_nan()
7479
else:
7580
return val is None or util.is_nan(val)
7681

pandas/tests/dtypes/test_missing.py

+38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
import decimal
34
import pytest
45
from warnings import catch_warnings, simplefilter
56
import numpy as np
@@ -248,6 +249,43 @@ def test_period(self):
248249
tm.assert_series_equal(isna(s), exp)
249250
tm.assert_series_equal(notna(s), ~exp)
250251

252+
def test_decimal(self):
253+
# scalars
254+
a = decimal.Decimal(1.0)
255+
assert pd.isna(a) is False
256+
assert pd.notna(a) is True
257+
258+
b = decimal.Decimal('NaN')
259+
assert pd.isna(b) is True
260+
assert pd.notna(b) is False
261+
262+
# array
263+
arr = np.array([a, b])
264+
expected = np.array([False, True])
265+
result = pd.isna(arr)
266+
tm.assert_numpy_array_equal(result, expected)
267+
268+
result = pd.notna(arr)
269+
tm.assert_numpy_array_equal(result, ~expected)
270+
271+
# series
272+
ser = pd.Series(arr)
273+
expected = pd.Series(expected)
274+
result = pd.isna(ser)
275+
tm.assert_series_equal(result, expected)
276+
277+
result = pd.notna(ser)
278+
tm.assert_series_equal(result, ~expected)
279+
280+
# index
281+
idx = pd.Index(arr)
282+
expected = np.array([False, True])
283+
result = pd.isna(idx)
284+
tm.assert_numpy_array_equal(result, expected)
285+
286+
result = pd.notna(idx)
287+
tm.assert_numpy_array_equal(result, ~expected)
288+
251289

252290
def test_array_equivalent():
253291
assert array_equivalent(np.array([np.nan, np.nan]),

0 commit comments

Comments
 (0)