-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: DatetimeIndex._data should return an ndarray #20912
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
92416a6
a6a038b
917917e
cc6ab25
f841afd
4233f6f
c20bb44
c207289
e18d996
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,7 +12,6 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
from pandas.core.dtypes.common import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
_INT64_DTYPE, | ||||||||||||||||||||||||||||||||||||||||||||||||||
_NS_DTYPE, | ||||||||||||||||||||||||||||||||||||||||||||||||||
is_object_dtype, | ||||||||||||||||||||||||||||||||||||||||||||||||||
is_datetime64_dtype, | ||||||||||||||||||||||||||||||||||||||||||||||||||
is_datetimetz, | ||||||||||||||||||||||||||||||||||||||||||||||||||
is_dtype_equal, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -551,10 +550,11 @@ def _generate(cls, start, end, periods, name, freq, | |||||||||||||||||||||||||||||||||||||||||||||||||
index = _generate_regular_range(start, end, periods, freq) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if tz is not None and getattr(index, 'tz', None) is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||
index = conversion.tz_localize_to_utc(_ensure_int64(index), | ||||||||||||||||||||||||||||||||||||||||||||||||||
tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||
ambiguous=ambiguous) | ||||||||||||||||||||||||||||||||||||||||||||||||||
index = index.view(_NS_DTYPE) | ||||||||||||||||||||||||||||||||||||||||||||||||||
arr = conversion.tz_localize_to_utc(_ensure_int64(index), | ||||||||||||||||||||||||||||||||||||||||||||||||||
tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||
ambiguous=ambiguous) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
index = DatetimeIndex(arr) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
# index is localized datetime64 array -> have to convert | ||||||||||||||||||||||||||||||||||||||||||||||||||
# start/end as well to compare | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -575,7 +575,9 @@ def _generate(cls, start, end, periods, name, freq, | |||||||||||||||||||||||||||||||||||||||||||||||||
index = index[1:] | ||||||||||||||||||||||||||||||||||||||||||||||||||
if not right_closed and len(index) and index[-1] == end: | ||||||||||||||||||||||||||||||||||||||||||||||||||
index = index[:-1] | ||||||||||||||||||||||||||||||||||||||||||||||||||
index = cls._simple_new(index, name=name, freq=freq, tz=tz) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
index = cls._simple_new(index.values, name=name, freq=freq, tz=tz) | ||||||||||||||||||||||||||||||||||||||||||||||||||
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 like having 2 different construction paths generally: eg we always need to be an ndarray or already converted to a DTI by the time _simple_new gets called what i would do is run all of the index tests and see what the current state is 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. Which 2 different construction paths do you mean? 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. my point is that we need an assertion to validate this. it may be that everything is fixed, but we should actually test this. |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return index | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
def _convert_for_op(self, value): | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -606,12 +608,14 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, | |||||||||||||||||||||||||||||||||||||||||||||||||
dtype=dtype, **kwargs) | ||||||||||||||||||||||||||||||||||||||||||||||||||
values = np.array(values, copy=False) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if is_object_dtype(values): | ||||||||||||||||||||||||||||||||||||||||||||||||||
return cls(values, name=name, freq=freq, tz=tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. was this just never hit? 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 I think it's never hit |
||||||||||||||||||||||||||||||||||||||||||||||||||
dtype=dtype, **kwargs).values | ||||||||||||||||||||||||||||||||||||||||||||||||||
elif not is_datetime64_dtype(values): | ||||||||||||||||||||||||||||||||||||||||||||||||||
if not is_datetime64_dtype(values): | ||||||||||||||||||||||||||||||||||||||||||||||||||
values = _ensure_int64(values).view(_NS_DTYPE) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
values = getattr(values, 'values', values) | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. Why is this one still needed? I thought the idea was now to ensure ndarrays are passed to 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. Well-spotted! I inserted this when I was trying to investigate why some tests were failing and I meant to move it before pushing the commit but forgot. I think we can move this line to just before the call to pandas/pandas/core/arrays/datetimelike.py Lines 43 to 53 in 1dd05cc
Does this make sense? I did the same thing here: pandas/pandas/core/indexes/base.py Lines 501 to 513 in 1dd05cc
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
assert isinstance(values, np.ndarray), "values is not an np.ndarray" | ||||||||||||||||||||||||||||||||||||||||||||||||||
assert is_datetime64_dtype(values) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
result = super(DatetimeIndex, cls)._simple_new(values, freq, tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||
**kwargs) | ||||||||||||||||||||||||||||||||||||||||||||||||||
result.name = name | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1000,7 +1004,7 @@ def unique(self, level=None): | |||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
naive = self | ||||||||||||||||||||||||||||||||||||||||||||||||||
result = super(DatetimeIndex, naive).unique(level=level) | ||||||||||||||||||||||||||||||||||||||||||||||||||
return self._simple_new(result, name=self.name, tz=self.tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||
return self._simple_new(result.values, name=self.name, tz=self.tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||
freq=self.freq) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
def union(self, other): | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1855,7 +1859,7 @@ def _generate_regular_range(start, end, periods, freq): | |||||||||||||||||||||||||||||||||||||||||||||||||
"if a 'period' is given.") | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
data = np.arange(b, e, stride, dtype=np.int64) | ||||||||||||||||||||||||||||||||||||||||||||||||||
data = DatetimeIndex._simple_new(data, None, tz=tz) | ||||||||||||||||||||||||||||||||||||||||||||||||||
data = DatetimeIndex._simple_new(data.view(_NS_DTYPE), None, tz=tz) | ||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||
if isinstance(start, Timestamp): | ||||||||||||||||||||||||||||||||||||||||||||||||||
start = start.to_pydatetime() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
tz_localize_to_utc
returns an array and not aDatetimeIndex
so I then convert this array to aDatetimeIndex
calledindex
so I can passindex.values
to_simple_new
below