Skip to content

Commit 0e87f77

Browse files
committed
BUG: series from dict of Timedelta scalar drops nanoseconds
reworking
1 parent d5f67b2 commit 0e87f77

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pandas/core/dtypes/cast.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@
8888
ABCSeries,
8989
)
9090
from pandas.core.dtypes.inference import is_list_like
91-
from pandas.core.dtypes.missing import isna, na_value_for_dtype, notna
91+
from pandas.core.dtypes.missing import (
92+
is_valid_nat_for_dtype,
93+
isna,
94+
na_value_for_dtype,
95+
notna,
96+
)
9297

9398
if TYPE_CHECKING:
9499
from pandas import Series
@@ -1693,9 +1698,13 @@ def construct_1d_arraylike_from_scalar(
16931698
if not isna(value):
16941699
value = ensure_str(value)
16951700
elif dtype.kind in ["M", "m"]:
1696-
# GH36541: can't fill array directly with pd.NaT -> ValueError
1697-
# GH38032: filling in pd.Timedelta loses nanoseconds
1698-
value = convert_scalar_for_putitemlike(value, dtype)
1701+
# GH38032: filling in Timedelta/Timestamp drops nanoseconds
1702+
if isinstance(value, (Timedelta, Timestamp)):
1703+
value = value.to_numpy()
1704+
# GH36541: filling datetime-like array directly with pd.NaT
1705+
# raises ValueError: cannot convert float NaN to integer
1706+
elif is_valid_nat_for_dtype(value, dtype):
1707+
value = np.datetime64("NaT")
16991708

17001709
subarr = np.empty(length, dtype=dtype)
17011710
subarr.fill(value)

0 commit comments

Comments
 (0)