-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Casting tz-aware DatetimeIndex to object-dtype ndarray/Index #23524
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 all commits
61ad510
937541e
ca9e8af
9d0fbd7
dbf145f
ec696e5
8853b6a
9add480
cfd8e71
bc06a61
c9a2c22
b4ecfbb
756574f
ebada17
bf1677a
0a5dbed
cc7f5cd
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
from pandas.core.dtypes.common import ( | ||
_NS_DTYPE, | ||
is_object_dtype, | ||
is_int64_dtype, | ||
is_datetime64tz_dtype, | ||
is_datetime64_dtype, | ||
ensure_int64) | ||
|
@@ -388,6 +389,15 @@ def _resolution(self): | |
# ---------------------------------------------------------------- | ||
# Array-like Methods | ||
|
||
def __array__(self, dtype=None): | ||
if is_object_dtype(dtype): | ||
return np.array(list(self), dtype=object) | ||
elif is_int64_dtype(dtype): | ||
return self.asi8 | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
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 think we can remove this elif branch. Numpy will afterwards convert the M8[ns] data to int, and in that way ensure the semantics of 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. @jbrockmendel I opened #23593, a PR doing the 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. Great, I'll take a look at 23593. |
||
|
||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# TODO: warn that conversion may be lossy? | ||
return self._data.view(np.ndarray) # follow Index.__array__ | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
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. Other question: what is the 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. It can probably be removed; this is taken directly from the |
||
|
||
def __iter__(self): | ||
""" | ||
Return an iterator over the boxed values | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,54 @@ def timedelta_index(request): | |
|
||
class TestDatetimeArray(object): | ||
|
||
def test_array_object_dtype(self, tz_naive_fixture): | ||
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. It think it would be good to add such a test to the base extension tests as well? 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 don't know those tests well enough to have an informed opinion. AFAIK ExtensionArray doesn't implement 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. EA implements This test would be slightly opinionated for a base test, in case an EA wants to be converted to a specific NumPy type, but I think it's OK. 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 guess we have def test_array_interface(self, data):
result = np.array(data)
assert result[0] == data[0] 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. Ah, yes, that's already a generic test. OK, since that does not actually test the return dtype, it's good to have more explicit tests here. Should we expect from EA that |
||
# GH#23524 | ||
tz = tz_naive_fixture | ||
dti = pd.date_range('2016-01-01', periods=3, tz=tz) | ||
arr = DatetimeArrayMixin(dti) | ||
|
||
expected = np.array(list(dti)) | ||
|
||
result = np.array(arr, dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
# also test the DatetimeIndex method while we're at it | ||
result = np.array(dti, dtype=object) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
def test_array(self, tz_naive_fixture): | ||
# GH#23524 | ||
tz = tz_naive_fixture | ||
dti = pd.date_range('2016-01-01', periods=3, tz=tz) | ||
arr = DatetimeArrayMixin(dti) | ||
|
||
expected = dti.asi8.view('M8[ns]') | ||
result = np.array(arr) | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
# check that we are not making copies when setting copy=False | ||
result = np.array(arr, copy=False) | ||
assert result.base is expected.base | ||
assert result.base is not None | ||
|
||
def test_array_i8_dtype(self, tz_naive_fixture): | ||
# GH#23524 | ||
tz = tz_naive_fixture | ||
dti = pd.date_range('2016-01-01', periods=3, tz=tz) | ||
arr = DatetimeArrayMixin(dti) | ||
|
||
expected = dti.asi8 | ||
result = np.array(arr, dtype='i8') | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
result = np.array(arr, dtype=np.int64) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
# check that we are not making copies when setting copy=False | ||
result = np.array(arr, dtype='i8', copy=False) | ||
assert result.base is expected.base | ||
assert result.base is not None | ||
|
||
def test_from_dti(self, tz_naive_fixture): | ||
tz = tz_naive_fixture | ||
dti = pd.date_range('2016-01-01', periods=3, tz=tz) | ||
|
Uh oh!
There was an error while loading. Please reload this page.