Skip to content

Commit 6fcc188

Browse files
authored
API: dont-special-case datetimelike in setting new column (#40084)
1 parent a11dc79 commit 6fcc188

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
maybe_box_native,
100100
maybe_convert_platform,
101101
maybe_downcast_to_dtype,
102-
maybe_infer_to_datetimelike,
103102
validate_numeric_casting,
104103
)
105104
from pandas.core.dtypes.common import (
@@ -147,6 +146,7 @@
147146
from pandas.core.arrays.sparse import SparseFrameAccessor
148147
from pandas.core.construction import (
149148
extract_array,
149+
sanitize_array,
150150
sanitize_masked_array,
151151
)
152152
from pandas.core.generic import (
@@ -4226,7 +4226,7 @@ def _sanitize_column(self, value) -> ArrayLike:
42264226

42274227
# possibly infer to datetimelike
42284228
if is_object_dtype(value.dtype):
4229-
value = maybe_infer_to_datetimelike(value)
4229+
value = sanitize_array(value, None)
42304230

42314231
else:
42324232
value = construct_1d_arraylike_from_scalar(value, len(self), dtype=None)

pandas/tests/frame/indexing/test_setitem.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,26 @@ def test_setitem_intervals(self):
421421

422422
# B & D end up as Categoricals
423423
# the remainer are converted to in-line objects
424-
# contining an IntervalIndex.values
424+
# containing an IntervalIndex.values
425425
df["B"] = ser
426426
df["C"] = np.array(ser)
427427
df["D"] = ser.values
428428
df["E"] = np.array(ser.values)
429+
df["F"] = ser.astype(object)
429430

430431
assert is_categorical_dtype(df["B"].dtype)
431432
assert is_interval_dtype(df["B"].cat.categories)
432433
assert is_categorical_dtype(df["D"].dtype)
433434
assert is_interval_dtype(df["D"].cat.categories)
434435

435-
assert is_object_dtype(df["C"])
436-
assert is_object_dtype(df["E"])
436+
# Thes goes through the Series constructor and so get inferred back
437+
# to IntervalDtype
438+
assert is_interval_dtype(df["C"])
439+
assert is_interval_dtype(df["E"])
440+
441+
# But the Series constructor doesn't do inference on Series objects,
442+
# so setting df["F"] doesnt get cast back to IntervalDtype
443+
assert is_object_dtype(df["F"])
437444

438445
# they compare equal as Index
439446
# when converted to numpy objects

0 commit comments

Comments
 (0)