Skip to content

Commit 35537dd

Browse files
authored
BUG: Cast pd.NA to pd.NaT in to_datetime (#32214)
1 parent 27f0000 commit 35537dd

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Bug fixes
6363
**Datetimelike**
6464

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

6768
**Categorical**
6869

pandas/_libs/tslibs/nattype.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ from pandas._libs.tslibs.util cimport (
2222
get_nat, is_integer_object, is_float_object, is_datetime64_object,
2323
is_timedelta64_object)
2424

25+
from pandas._libs.missing cimport C_NA
26+
2527

2628
# ----------------------------------------------------------------------
2729
# Constants
@@ -763,7 +765,7 @@ NaT = c_NaT # Python-visible
763765

764766
cdef inline bint checknull_with_nat(object val):
765767
""" utility to check if a value is a nat or not """
766-
return val is None or util.is_nan(val) or val is c_NaT
768+
return val is None or util.is_nan(val) or val is c_NaT or val is C_NA
767769

768770

769771
cpdef bint is_null_datetimelike(object val, bint inat_is_null=True):

pandas/tests/indexes/datetimes/test_tools.py

+7
Original file line numberDiff line numberDiff line change
@@ -2315,3 +2315,10 @@ def test_nullable_integer_to_datetime():
23152315
tm.assert_series_equal(res, expected)
23162316
# Check that ser isn't mutated
23172317
tm.assert_series_equal(ser, ser_copy)
2318+
2319+
2320+
@pytest.mark.parametrize("klass", [np.array, list])
2321+
def test_na_to_datetime(nulls_fixture, klass):
2322+
result = pd.to_datetime(klass([nulls_fixture]))
2323+
2324+
assert result[0] is pd.NaT

0 commit comments

Comments
 (0)