Skip to content
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

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

Merged
merged 15 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -613,6 +613,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
6 changes: 6 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,12 @@ def _try_cast(
)

elif dtype.kind in "mM":
if is_ndarray:
arr = cast(np.ndarray, arr)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This cast is to make mypy happy.

if arr.ndim == 2 and arr.shape[1] == 1:
# GH#60081: DataFrame Constructor converts 1D data to array of
# shape (N, 1), but maybe_cast_to_datetime assumes 1D input
return maybe_cast_to_datetime(arr[:, 0], dtype).reshape(arr.shape)
return maybe_cast_to_datetime(arr, dtype)

# GH#15832: Check if we are requesting a numeric dtype and
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ def maybe_infer_to_datetimelike(

def maybe_cast_to_datetime(
value: np.ndarray | list, dtype: np.dtype
) -> ExtensionArray | np.ndarray:
) -> DatetimeArray | TimedeltaArray | np.ndarray:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To avoid mypy error: Item "ExtensionArray" of "ExtensionArray | ndarray[Any, Any]" has no attribute "reshape".

"""
try to cast the array/value to a datetimelike dtype, converting float
nan to iNaT
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