Skip to content

API: dont-special-case datetimelike in setting new column #40084

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
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
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
maybe_box_native,
maybe_convert_platform,
maybe_downcast_to_dtype,
maybe_infer_to_datetimelike,
validate_numeric_casting,
)
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -147,6 +146,7 @@
from pandas.core.arrays.sparse import SparseFrameAccessor
from pandas.core.construction import (
extract_array,
sanitize_array,
sanitize_masked_array,
)
from pandas.core.generic import (
Expand Down Expand Up @@ -4045,7 +4045,7 @@ def _sanitize_column(self, value) -> ArrayLike:

# possibly infer to datetimelike
if is_object_dtype(value.dtype):
value = maybe_infer_to_datetimelike(value)
value = sanitize_array(value, None)

else:
value = construct_1d_arraylike_from_scalar(value, len(self), dtype=None)
Expand Down
13 changes: 10 additions & 3 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,26 @@ def test_setitem_intervals(self):

# B & D end up as Categoricals
# the remainer are converted to in-line objects
# contining an IntervalIndex.values
# containing an IntervalIndex.values
df["B"] = ser
df["C"] = np.array(ser)
df["D"] = ser.values
df["E"] = np.array(ser.values)
df["F"] = ser.astype(object)

assert is_categorical_dtype(df["B"].dtype)
assert is_interval_dtype(df["B"].cat.categories)
assert is_categorical_dtype(df["D"].dtype)
assert is_interval_dtype(df["D"].cat.categories)

assert is_object_dtype(df["C"])
assert is_object_dtype(df["E"])
# Thes goes through the Series constructor and so get inferred back
# to IntervalDtype
assert is_interval_dtype(df["C"])
assert is_interval_dtype(df["E"])

# But the Series constructor doesn't do inference on Series objects,
# so setting df["F"] doesnt get cast back to IntervalDtype
assert is_object_dtype(df["F"])

# they compare equal as Index
# when converted to numpy objects
Expand Down