-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: recognize Decimal("NaN") in pd.isna #39409
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
Changes from all commits
1a3b757
cfea335
f66022c
b18a7d3
7bd6d83
4dde6df
a1b34e5
335d599
ee93754
15bcccd
733e754
d0e2a87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
missing types & inference | ||
""" | ||
from decimal import Decimal | ||
from functools import partial | ||
|
||
import numpy as np | ||
|
@@ -610,20 +611,24 @@ def is_valid_na_for_dtype(obj, dtype: DtypeObj) -> bool: | |
""" | ||
if not lib.is_scalar(obj) or not isna(obj): | ||
return False | ||
if dtype.kind == "M": | ||
elif dtype.kind == "M": | ||
if isinstance(dtype, np.dtype): | ||
# i.e. not tzaware | ||
return not isinstance(obj, np.timedelta64) | ||
return not isinstance(obj, (np.timedelta64, Decimal)) | ||
# we have to rule out tznaive dt64("NaT") | ||
return not isinstance(obj, (np.timedelta64, np.datetime64)) | ||
if dtype.kind == "m": | ||
return not isinstance(obj, np.datetime64) | ||
if dtype.kind in ["i", "u", "f", "c"]: | ||
return not isinstance(obj, (np.timedelta64, np.datetime64, Decimal)) | ||
elif dtype.kind == "m": | ||
return not isinstance(obj, (np.datetime64, Decimal)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
elif dtype.kind in ["i", "u", "f", "c"]: | ||
# Numeric | ||
return obj is not NaT and not isinstance(obj, (np.datetime64, np.timedelta64)) | ||
|
||
elif dtype == np.dtype(object): | ||
# This is needed for Categorical, but is kind of weird | ||
return True | ||
|
||
# must be PeriodDType | ||
return not isinstance(obj, (np.datetime64, np.timedelta64)) | ||
return not isinstance(obj, (np.datetime64, np.timedelta64, Decimal)) | ||
|
||
|
||
def isna_all(arr: ArrayLike) -> bool: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
""" | ||
Tests for the Index constructor conducting inference. | ||
""" | ||
from decimal import Decimal | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
|
@@ -89,6 +91,10 @@ def test_constructor_infer_periodindex(self): | |
def test_constructor_infer_nat_dt_like( | ||
self, pos, klass, dtype, ctor, nulls_fixture, request | ||
): | ||
if isinstance(nulls_fixture, Decimal): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe should have a nulls_fixture_compatible_datetimelike ? |
||
# We dont cast these to datetime64/timedelta64 | ||
return | ||
|
||
expected = klass([NaT, NaT]) | ||
assert expected.dtype == dtype | ||
data = [ctor] | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||
import datetime | ||||
from datetime import timedelta | ||||
from decimal import Decimal | ||||
from io import StringIO | ||||
import json | ||||
import os | ||||
|
@@ -1742,8 +1743,12 @@ def test_json_pandas_na(self): | |||
result = DataFrame([[pd.NA]]).to_json() | ||||
assert result == '{"0":{"0":null}}' | ||||
|
||||
def test_json_pandas_nulls(self, nulls_fixture): | ||||
def test_json_pandas_nulls(self, nulls_fixture, request): | ||||
# GH 31615 | ||||
if isinstance(nulls_fixture, Decimal): | ||||
mark = pytest.mark.xfail(reason="not implemented") | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you wanted to fix this here it would just require adding the same condition in the ujson code that we have for checking floats. pandas/pandas/_libs/src/ujson/python/objToJSON.c Line 1552 in 421fb8d
The decimal check is only a few branches below that |
||||
request.node.add_marker(mark) | ||||
|
||||
result = DataFrame([[nulls_fixture]]).to_json() | ||||
assert result == '{"0":{"0":null}}' | ||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the list of what is considered null in docstring maybe could be updated.
also for consistency when using pandas.options.mode.use_inf_as_na, what about checknull_old?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just added this to my next "collected misc" branch
i guess you're referring to
Decimal("inf")
? my inclination is to let that sleeping dog lie