Skip to content

Commit a74f0a9

Browse files
BUG: Timestamp(pd.NA) raising TypeError (#54102)
* BUG: Timestamp(pd.NA) raising TypeError * Update pandas/tests/scalar/timestamp/test_constructors.py * move check to conversion.pyx --------- Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent 394af8e commit a74f0a9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ Datetimelike
439439
- Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
440440
- Bug in constructing a :class:`Series` or :class:`DataFrame` from a datetime or timedelta scalar always inferring nanosecond resolution instead of inferring from the input (:issue:`52212`)
441441
- Bug in constructing a :class:`Timestamp` from a string representing a time without a date inferring an incorrect unit (:issue:`54097`)
442+
- Bug in constructing a :class:`Timestamp` with ``ts_input=pd.NA`` raising ``TypeError`` (:issue:`45481`)
442443
- Bug in parsing datetime strings with weekday but no day e.g. "2023 Sept Thu" incorrectly raising ``AttributeError`` instead of ``ValueError`` (:issue:`52659`)
443444

444445
Timedelta

pandas/_libs/tslibs/conversion.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from cpython.datetime cimport (
2525

2626
import_datetime()
2727

28+
from pandas._libs.missing cimport checknull_with_nat_and_na
2829
from pandas._libs.tslibs.base cimport ABCTimestamp
2930
from pandas._libs.tslibs.dtypes cimport (
3031
abbrev_to_npy_unit,
@@ -57,7 +58,6 @@ from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
5758

5859
from pandas._libs.tslibs.nattype cimport (
5960
NPY_NAT,
60-
c_NaT as NaT,
6161
c_nat_strings as nat_strings,
6262
)
6363
from pandas._libs.tslibs.parsing cimport parse_datetime_string
@@ -268,7 +268,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
268268
if isinstance(ts, str):
269269
return convert_str_to_tsobject(ts, tz, unit, dayfirst, yearfirst)
270270

271-
if ts is None or ts is NaT:
271+
if checknull_with_nat_and_na(ts):
272272
obj.value = NPY_NAT
273273
elif is_datetime64_object(ts):
274274
reso = get_supported_reso(get_datetime64_unit(ts))

pandas/tests/scalar/timestamp/test_constructors.py

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from pandas.errors import OutOfBoundsDatetime
1919

2020
from pandas import (
21+
NA,
22+
NaT,
2123
Period,
2224
Timedelta,
2325
Timestamp,
@@ -898,3 +900,11 @@ def test_timestamp_constructor_adjust_value_for_fold(tz, ts_input, fold, value_o
898900
result = ts._value
899901
expected = value_out
900902
assert result == expected
903+
904+
905+
@pytest.mark.parametrize("na_value", [None, np.nan, np.datetime64("NaT"), NaT, NA])
906+
def test_timestamp_constructor_na_value(na_value):
907+
# GH45481
908+
result = Timestamp(na_value)
909+
expected = NaT
910+
assert result is expected

0 commit comments

Comments
 (0)