Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -612,6 +612,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
2 changes: 2 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ def _try_cast(
)

elif dtype.kind in "mM":
if is_ndarray and arr.ndim == 2 and arr.shape[1] == 1:
Copy link
Member

Choose a reason for hiding this comment

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

can you add a comment about why you are special-casing the arr.shape[1] == 1 case?

return maybe_cast_to_datetime(arr.ravel(), dtype).reshape(arr.shape)
Copy link
Member

Choose a reason for hiding this comment

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

instead of arr.ravel() (which can make a copy), can you do arr[:, 0]?

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 have added a comment and applied this change.

return maybe_cast_to_datetime(arr, dtype)

# GH#15832: Check if we are requesting a numeric dtype and
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