-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[ArrayManager] Ensure to store datetimelike data as DatetimeArray/TimedeltaArray (and not ndarray) #40147
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
[ArrayManager] Ensure to store datetimelike data as DatetimeArray/TimedeltaArray (and not ndarray) #40147
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,6 +33,7 @@ | |||||||||||
) | ||||||||||||
from pandas.core.dtypes.common import ( | ||||||||||||
is_bool_dtype, | ||||||||||||
is_datetime64_ns_dtype, | ||||||||||||
is_dtype_equal, | ||||||||||||
is_extension_array_dtype, | ||||||||||||
is_numeric_dtype, | ||||||||||||
|
@@ -53,7 +54,11 @@ | |||||||||||
) | ||||||||||||
|
||||||||||||
import pandas.core.algorithms as algos | ||||||||||||
from pandas.core.arrays import ExtensionArray | ||||||||||||
from pandas.core.arrays import ( | ||||||||||||
DatetimeArray, | ||||||||||||
ExtensionArray, | ||||||||||||
TimedeltaArray, | ||||||||||||
) | ||||||||||||
from pandas.core.arrays.sparse import SparseDtype | ||||||||||||
from pandas.core.construction import ( | ||||||||||||
ensure_wrapped_if_datetimelike, | ||||||||||||
|
@@ -113,6 +118,7 @@ def __init__( | |||||||||||
|
||||||||||||
if verify_integrity: | ||||||||||||
self._axes = [ensure_index(ax) for ax in axes] | ||||||||||||
self.arrays = [ensure_wrapped_if_datetimelike(arr) for arr in arrays] | ||||||||||||
self._verify_integrity() | ||||||||||||
|
||||||||||||
def make_empty(self: T, axes=None) -> T: | ||||||||||||
|
@@ -715,20 +721,16 @@ def fast_xs(self, loc: int) -> ArrayLike: | |||||||||||
""" | ||||||||||||
dtype = _interleaved_dtype(self.arrays) | ||||||||||||
|
||||||||||||
if isinstance(dtype, SparseDtype): | ||||||||||||
temp_dtype = dtype.subtype | ||||||||||||
elif isinstance(dtype, PandasDtype): | ||||||||||||
temp_dtype = dtype.numpy_dtype | ||||||||||||
elif is_extension_array_dtype(dtype): | ||||||||||||
temp_dtype = "object" | ||||||||||||
elif is_dtype_equal(dtype, str): | ||||||||||||
temp_dtype = "object" | ||||||||||||
else: | ||||||||||||
temp_dtype = dtype | ||||||||||||
|
||||||||||||
result = np.array([arr[loc] for arr in self.arrays], dtype=temp_dtype) | ||||||||||||
values = [arr[loc] for arr in self.arrays] | ||||||||||||
if isinstance(dtype, ExtensionDtype): | ||||||||||||
result = dtype.construct_array_type()._from_sequence(result, dtype=dtype) | ||||||||||||
result = dtype.construct_array_type()._from_sequence(values, dtype=dtype) | ||||||||||||
# for datetime64/timedelta64, the np.ndarray constructor cannot handle pd.NaT | ||||||||||||
elif is_datetime64_ns_dtype(dtype): | ||||||||||||
result = DatetimeArray._from_sequence(values, dtype=dtype)._data | ||||||||||||
elif is_timedelta64_ns_dtype(dtype): | ||||||||||||
result = TimedeltaArray._from_sequence(values, dtype=dtype)._data | ||||||||||||
else: | ||||||||||||
result = np.array(values, dtype=dtype) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you check if this is relevant for the BlockManager case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, see my (somewhat) answer at #40147 (comment). pandas/pandas/core/internals/managers.py Lines 978 to 982 in b835ca2
So that's quite different as the code here, and the idea of first keeping it in a list doesn't really apply. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||||||||||||
return result | ||||||||||||
|
||||||||||||
def iget(self, i: int) -> SingleBlockManager: | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have a little-used is_ea_or_datetimelike_dtype, could use an analogous helper to get DatetimeArray/TimedeltaArray in these cases (not for this PR)