Skip to content

BUG: Cast pd.NA to pd.NaT in to_datetime #32214

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 7 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Bug fixes
**Datetimelike**

- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`)
- Bug where :func:`to_datetime` would raise when passed ``pd.NA`` (:issue:`32213`)

**Categorical**

Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ from pandas._libs.tslibs.util cimport (
get_nat, is_integer_object, is_float_object, is_datetime64_object,
is_timedelta64_object)

from pandas._libs.missing cimport C_NA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel is this a problem? (the dependency)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid it if possible. The property "tslibs doesnt rely on anything except _config" is really nice for reasoning-about-code purposes.

@dsaxton is it feasible to put pd.NA into a higher-level check than checknull_with_nat?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it may be but I think it would have to be before https://github.com/pandas-dev/pandas/blob/master/pandas/core/arrays/datetimes.py#L1844, do you think that looks possible? There's also the "trick" of not importing NA at all and checking for (val == val) is (val != val) in checknull_with_nat

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, going to merge this as-is. @jbrockmendel or @dsaxton if you can figure a way to decouple this a bit would be great (though since missing is pretty dependency free i actually don't mind this)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dsaxton im having trouble figuring out which usage of checknull_with_nat is the relevant one here, can you point it out? The only ones I see when grepping are in libmissing.checknull (which already checks for C_NA, which this renders redundant), and in Period.__new__, which seems like it shouldn't be relevant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like i was confusing checknull_with_nat with is_null_datetimelike, never mind

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this suggests we may have too many null-checking-like functions)



# ----------------------------------------------------------------------
# Constants
Expand Down Expand Up @@ -763,7 +765,7 @@ NaT = c_NaT # Python-visible

cdef inline bint checknull_with_nat(object val):
""" utility to check if a value is a nat or not """
return val is None or util.is_nan(val) or val is c_NaT
return val is None or util.is_nan(val) or val is c_NaT or val is C_NA


cpdef bint is_null_datetimelike(object val, bint inat_is_null=True):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2315,3 +2315,10 @@ def test_nullable_integer_to_datetime():
tm.assert_series_equal(res, expected)
# Check that ser isn't mutated
tm.assert_series_equal(ser, ser_copy)


@pytest.mark.parametrize("klass", [np.array, list])
def test_na_to_datetime(nulls_fixture, klass):
result = pd.to_datetime(klass([nulls_fixture]))

assert result[0] is pd.NaT