Skip to content

Commit 4f18821

Browse files
[ArrayManager] Ensure to store datetimelike data as DatetimeArray/TimedeltaArray (and not ndarray) (#40147)
1 parent b835ca2 commit 4f18821

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

pandas/core/internals/array_manager.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
)
3535
from pandas.core.dtypes.common import (
3636
is_bool_dtype,
37+
is_datetime64_ns_dtype,
3738
is_dtype_equal,
3839
is_extension_array_dtype,
3940
is_numeric_dtype,
@@ -54,7 +55,11 @@
5455
)
5556

5657
import pandas.core.algorithms as algos
57-
from pandas.core.arrays import ExtensionArray
58+
from pandas.core.arrays import (
59+
DatetimeArray,
60+
ExtensionArray,
61+
TimedeltaArray,
62+
)
5863
from pandas.core.arrays.sparse import SparseDtype
5964
from pandas.core.construction import (
6065
ensure_wrapped_if_datetimelike,
@@ -114,6 +119,7 @@ def __init__(
114119

115120
if verify_integrity:
116121
self._axes = [ensure_index(ax) for ax in axes]
122+
self.arrays = [ensure_wrapped_if_datetimelike(arr) for arr in arrays]
117123
self._verify_integrity()
118124

119125
def make_empty(self: T, axes=None) -> T:
@@ -716,20 +722,16 @@ def fast_xs(self, loc: int) -> ArrayLike:
716722
"""
717723
dtype = _interleaved_dtype(self.arrays)
718724

719-
if isinstance(dtype, SparseDtype):
720-
temp_dtype = dtype.subtype
721-
elif isinstance(dtype, PandasDtype):
722-
temp_dtype = dtype.numpy_dtype
723-
elif is_extension_array_dtype(dtype):
724-
temp_dtype = "object"
725-
elif is_dtype_equal(dtype, str):
726-
temp_dtype = "object"
727-
else:
728-
temp_dtype = dtype
729-
730-
result = np.array([arr[loc] for arr in self.arrays], dtype=temp_dtype)
725+
values = [arr[loc] for arr in self.arrays]
731726
if isinstance(dtype, ExtensionDtype):
732-
result = dtype.construct_array_type()._from_sequence(result, dtype=dtype)
727+
result = dtype.construct_array_type()._from_sequence(values, dtype=dtype)
728+
# for datetime64/timedelta64, the np.ndarray constructor cannot handle pd.NaT
729+
elif is_datetime64_ns_dtype(dtype):
730+
result = DatetimeArray._from_sequence(values, dtype=dtype)._data
731+
elif is_timedelta64_ns_dtype(dtype):
732+
result = TimedeltaArray._from_sequence(values, dtype=dtype)._data
733+
else:
734+
result = np.array(values, dtype=dtype)
733735
return result
734736

735737
def iget(self, i: int) -> SingleBlockManager:

pandas/tests/frame/methods/test_rename.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def test_rename_multiindex(self):
170170
renamed = df.rename(index={"foo1": "foo3", "bar2": "bar3"}, level=0)
171171
tm.assert_index_equal(renamed.index, new_index)
172172

173+
@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) setitem copy/view
173174
def test_rename_nocopy(self, float_frame):
174175
renamed = float_frame.rename(columns={"C": "foo"}, copy=False)
175176
renamed["foo"] = 1.0

0 commit comments

Comments
 (0)