-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: consistent __array__ for datetime-like ExtensionArrays #23593
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
Changes from 3 commits
8ebe597
6e8eaff
9506fef
bade9e4
8c68e2a
d8f24fd
39bae20
3a9c368
55c6cb9
b60c81e
eb6c8bd
c5404d7
d9ab5bf
b51c4b3
79908d1
1112420
e284aad
4120586
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 |
---|---|---|
|
@@ -132,6 +132,12 @@ def asi8(self): | |
# ------------------------------------------------------------------ | ||
# Array-like Methods | ||
|
||
def __array__(self, dtype=None): | ||
# used for Timedelta/DatetimeArray, overwritten by PeriodArray | ||
if is_object_dtype(dtype): | ||
return np.array(list(self), dtype=object) | ||
return self._data | ||
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. I would raise if dtype not in [None, object] |
||
|
||
@property | ||
def shape(self): | ||
return (len(self),) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,24 @@ def test_int_properties(self, datetime_index, propname): | |
|
||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
def test_array(self, datetime_index): | ||
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. while you're at it, can you make sure there are analogous tests for the DTI/TDI/PI index classes? 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. OK, I only have to see then a bit how that overlaps with your PR then 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. Do you know if there are any existing tests on this somewhere (for the index classes)? Not directly finding something |
||
arr = DatetimeArrayMixin(datetime_index) | ||
|
||
result = np.asarray(arr) | ||
expected = arr._data | ||
assert result is expected | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = np.asarray(arr, dtype=object) | ||
expected = np.array(list(arr), dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = np.asarray(arr, dtype='int64') | ||
assert result is not arr.asi8 | ||
assert not np.may_share_memory(arr, result) | ||
expected = arr.asi8.copy() | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
|
||
class TestTimedeltaArray(object): | ||
def test_from_tdi(self): | ||
|
@@ -174,6 +192,24 @@ def test_int_properties(self, timedelta_index, propname): | |
|
||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
def test_array(self, timedelta_index): | ||
arr = TimedeltaArrayMixin(timedelta_index) | ||
|
||
result = np.asarray(arr) | ||
expected = arr._data | ||
assert result is expected | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = np.asarray(arr, dtype=object) | ||
expected = np.array(list(arr), dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = np.asarray(arr, dtype='int64') | ||
assert result is not arr.asi8 | ||
assert not np.may_share_memory(arr, result) | ||
expected = arr.asi8.copy() | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
|
||
class TestPeriodArray(object): | ||
|
||
|
@@ -228,3 +264,16 @@ def test_int_properties(self, period_index, propname): | |
expected = np.array(getattr(pi, propname)) | ||
|
||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
def test_array(self, period_index): | ||
arr = PeriodArray(period_index) | ||
|
||
result = np.asarray(arr) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected = np.array(list(arr), dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = np.asarray(arr, dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
with pytest.raises(TypeError): | ||
np.asarray(arr, dtype='int64') |
Uh oh!
There was an error while loading. Please reload this page.