Skip to content

Commit 04432f5

Browse files
authored
BUG: fix DataFrame(data=[None, 1], dtype='timedelta64[ns]') raising ValueError (#60081)
1 parent feaa963 commit 04432f5

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

Diff for: doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ Categorical
613613
Datetimelike
614614
^^^^^^^^^^^^
615615
- Bug in :attr:`is_year_start` where a DateTimeIndex constructed via a date_range with frequency 'MS' wouldn't have the correct year or quarter start attributes (:issue:`57377`)
616+
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
616617
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
617618
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
618619
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56147`)

Diff for: pandas/core/construction.py

+6
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ def _try_cast(
807807
)
808808

809809
elif dtype.kind in "mM":
810+
if is_ndarray:
811+
arr = cast(np.ndarray, arr)
812+
if arr.ndim == 2 and arr.shape[1] == 1:
813+
# GH#60081: DataFrame Constructor converts 1D data to array of
814+
# shape (N, 1), but maybe_cast_to_datetime assumes 1D input
815+
return maybe_cast_to_datetime(arr[:, 0], dtype).reshape(arr.shape)
810816
return maybe_cast_to_datetime(arr, dtype)
811817

812818
# GH#15832: Check if we are requesting a numeric dtype and

Diff for: pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ def maybe_infer_to_datetimelike(
12051205

12061206
def maybe_cast_to_datetime(
12071207
value: np.ndarray | list, dtype: np.dtype
1208-
) -> ExtensionArray | np.ndarray:
1208+
) -> DatetimeArray | TimedeltaArray | np.ndarray:
12091209
"""
12101210
try to cast the array/value to a datetimelike dtype, converting float
12111211
nan to iNaT

Diff for: pandas/tests/frame/test_constructors.py

+8
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,14 @@ def test_construction_datetime_resolution_inference(self, cons):
27722772
res_dtype2 = tm.get_dtype(obj2)
27732773
assert res_dtype2 == "M8[us, US/Pacific]", res_dtype2
27742774

2775+
def test_construction_nan_value_timedelta64_dtype(self):
2776+
# GH#60064
2777+
result = DataFrame([None, 1], dtype="timedelta64[ns]")
2778+
expected = DataFrame(
2779+
["NaT", "0 days 00:00:00.000000001"], dtype="timedelta64[ns]"
2780+
)
2781+
tm.assert_frame_equal(result, expected)
2782+
27752783

27762784
class TestDataFrameConstructorIndexInference:
27772785
def test_frame_from_dict_of_series_overlapping_monthly_period_indexes(self):

0 commit comments

Comments
 (0)