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 4 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 @@ -758,6 +758,7 @@ Styler

Other
^^^^^
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
- Bug in :func:`eval` on :class:`ExtensionArray` on including division ``/`` failed with a ``TypeError``. (:issue:`58748`)
- Bug in :func:`eval` where the names of the :class:`Series` were not preserved when using ``engine="numexpr"``. (:issue:`10239`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ def sequence_to_td64ns(
else:
mask = np.isnan(data)

data = cast_from_unit_vectorized(data, unit or "ns")
data = cast_from_unit_vectorized(data.ravel(), unit or "ns").reshape(data.shape)
Copy link
Member

Choose a reason for hiding this comment

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

It seems to me we shouldn't be doing this in array code, that should assume the data is already 1d. Can you move this to maybe_cast_to_datetime and put it behind a check (that it's 2d with shape (N, 1)).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Moved.

data[mask] = iNaT
data = data.view("m8[ns]")
copy = False
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