Skip to content

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

Merged
merged 18 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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),)
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ def freq(self):
"""Return the frequency object for this PeriodArray."""
return self.dtype.freq

def __array__(self, dtype=None):
# overriding DatetimelikeArray
return np.array(list(self), dtype=object)

# --------------------------------------------------------------------
# Vectorized analogues of Period properties

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ class DatetimeIndexOpsMixin(DatetimeLikeArrayMixin):
_resolution = cache_readonly(DatetimeLikeArrayMixin._resolution.fget)
resolution = cache_readonly(DatetimeLikeArrayMixin.resolution.fget)

def __array__(self, dtype=None):
# TODO properly dispatch to EA
return Index.__array__(self)

def equals(self, other):
"""
Determines if two Index objects contain the same elements.
Expand Down
49 changes: 49 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Member Author

@jorisvandenbossche jorisvandenbossche Nov 9, 2018

Choose a reason for hiding this comment

The 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):
Expand Down Expand Up @@ -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)

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):

Expand Down Expand Up @@ -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)
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')
5 changes: 5 additions & 0 deletions pandas/tests/extension/base/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pandas.core.dtypes.dtypes import ExtensionDtype

import pandas as pd
import pandas.util.testing as tm

from .base import BaseExtensionTests

Expand Down Expand Up @@ -35,6 +36,10 @@ def test_array_interface(self, data):
result = np.array(data)
assert result[0] == data[0]

result = np.array(data, dtype=object)
expected = np.array(list(data), dtype=object)
tm.assert_numpy_array_equal(result, expected)

def test_repr(self, data):
ser = pd.Series(data)
assert data.dtype.name in repr(ser)
Expand Down