Skip to content

Commit ccac772

Browse files
authored
REF: ensure _sanitize_column returns ArrayLike (#39962)
1 parent a7402c1 commit ccac772

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

pandas/core/arrays/categorical.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,9 @@ def __init__(
399399
# sanitize_array coerces np.nan to a string under certain versions
400400
# of numpy
401401
values = maybe_infer_to_datetimelike(values)
402-
if not isinstance(values, (np.ndarray, ExtensionArray)):
402+
if isinstance(values, np.ndarray):
403+
values = sanitize_to_nanoseconds(values)
404+
elif not isinstance(values, ExtensionArray):
403405
values = com.convert_to_list_like(values)
404406

405407
# By convention, empty lists result in object dtype:
@@ -409,9 +411,6 @@ def __init__(
409411
values = [values[idx] for idx in np.where(~null_mask)[0]]
410412
values = sanitize_array(values, None, dtype=sanitize_dtype)
411413

412-
else:
413-
values = sanitize_to_nanoseconds(values)
414-
415414
if dtype.categories is None:
416415
try:
417416
codes, categories = factorize(values, sort=True)

pandas/core/dtypes/cast.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ def maybe_infer_to_datetimelike(
14321432
if not len(v):
14331433
return value
14341434

1435-
def try_datetime(v):
1435+
def try_datetime(v: np.ndarray) -> ArrayLike:
14361436
# safe coerce to datetime64
14371437
try:
14381438
# GH19671
@@ -1451,14 +1451,15 @@ def try_datetime(v):
14511451
except (ValueError, TypeError):
14521452
pass
14531453
else:
1454-
return DatetimeIndex(values).tz_localize("UTC").tz_convert(tz=tz)
1454+
dti = DatetimeIndex(values).tz_localize("UTC").tz_convert(tz=tz)
1455+
return dti._data
14551456
except TypeError:
14561457
# e.g. <class 'numpy.timedelta64'> is not convertible to datetime
14571458
pass
14581459

14591460
return v.reshape(shape)
14601461

1461-
def try_timedelta(v):
1462+
def try_timedelta(v: np.ndarray) -> np.ndarray:
14621463
# safe coerce to timedelta64
14631464

14641465
# will try first with a string & object conversion

pandas/core/frame.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,7 +4000,7 @@ def assign(self, **kwargs) -> DataFrame:
40004000
data[k] = com.apply_if_callable(v, data)
40014001
return data
40024002

4003-
def _sanitize_column(self, value):
4003+
def _sanitize_column(self, value) -> ArrayLike:
40044004
"""
40054005
Ensures new columns (which go into the BlockManager as new blocks) are
40064006
always copied and converted into an array.
@@ -4011,7 +4011,7 @@ def _sanitize_column(self, value):
40114011
40124012
Returns
40134013
-------
4014-
numpy.ndarray
4014+
numpy.ndarray or ExtensionArray
40154015
"""
40164016
self._ensure_valid_index(value)
40174017

@@ -4025,7 +4025,7 @@ def _sanitize_column(self, value):
40254025
value = value.copy()
40264026
value = sanitize_index(value, self.index)
40274027

4028-
elif isinstance(value, Index) or is_sequence(value):
4028+
elif is_sequence(value):
40294029

40304030
# turn me into an ndarray
40314031
value = sanitize_index(value, self.index)
@@ -4035,7 +4035,7 @@ def _sanitize_column(self, value):
40354035
else:
40364036
value = com.asarray_tuplesafe(value)
40374037
elif isinstance(value, Index):
4038-
value = value.copy(deep=True)
4038+
value = value.copy(deep=True)._values
40394039
else:
40404040
value = value.copy()
40414041

0 commit comments

Comments
 (0)