Skip to content

BUG: fix DataFrame(data=[None, 1], dtype='timedelta64[ns]') raising ValueError #60081

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 15 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- 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`)
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56147`)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,9 @@ def maybe_cast_to_datetime(
_ensure_nanosecond_dtype(dtype)

if lib.is_np_dtype(dtype, "m"):
if getattr(value, "ndim", 1) == 2 and value.shape[1] == 1:
res = TimedeltaArray._from_sequence(value.ravel(), dtype=dtype)
return res.reshape(value.shape)
res = TimedeltaArray._from_sequence(value, dtype=dtype)
return res
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we raise error here for other situations, e.g. when the shape is not (N, 1) or (1, )?

Or is a less strict check appropriate, like if getattr(value, "ndim", 1) > 1:? @rhshadrach

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should ever be reaching here with ndim 3 or higher. E.g.

arr = np.zeros((3, 3, 3))
pd.DataFrame(arr)
# ValueError: Must pass 2-d input. shape=(3, 3, 3)

else:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,14 @@ def test_construction_datetime_resolution_inference(self, cons):
res_dtype2 = tm.get_dtype(obj2)
assert res_dtype2 == "M8[us, US/Pacific]", res_dtype2

def test_construction_nan_value_timedelta64_dtype(self):
# GH#60064
result = DataFrame([None, 1], dtype="timedelta64[ns]")
expected = DataFrame(
["NaT", "0 days 00:00:00.000000001"], dtype="timedelta64[ns]"
)
tm.assert_frame_equal(result, expected)


class TestDataFrameConstructorIndexInference:
def test_frame_from_dict_of_series_overlapping_monthly_period_indexes(self):
Expand Down
Loading