Skip to content

REF: implement ExtensionIndex._concat_same_dtype, use for IntervalIndex #31635

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 23 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b08bc6d
Use IntervalArray for IntervalIndex._concat_same_dtype
jbrockmendel Jan 27, 2020
49ba586
get IntervalIndex.__getitem__ from ExtensionIndex
jbrockmendel Jan 28, 2020
251d321
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jan 28, 2020
086c349
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jan 28, 2020
f06d25a
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jan 29, 2020
67dc67a
implement ExtensionIndex._concat_same_dtype
jbrockmendel Jan 29, 2020
84ee9fa
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jan 31, 2020
29c55a4
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jan 31, 2020
e36ddcb
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Jan 31, 2020
aeeecd0
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 2, 2020
4f076fa
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 3, 2020
7882360
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 3, 2020
3e09ce7
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 3, 2020
caa334f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 3, 2020
785b957
remove defunct docstring
jbrockmendel Feb 3, 2020
ab3200f
flake8 fixup
jbrockmendel Feb 3, 2020
20e9006
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 3, 2020
18d7829
typing fixup
jbrockmendel Feb 3, 2020
92a17df
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 4, 2020
50d1cec
remove extra methods
jbrockmendel Feb 4, 2020
4b42448
comments
jbrockmendel Feb 4, 2020
677a40c
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 8, 2020
792c59d
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Feb 8, 2020
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
11 changes: 1 addition & 10 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,7 @@ def wrapper(left, right):
cache=True,
)
@inherit_names(
[
"__iter__",
"mean",
"freq",
"freqstr",
"_ndarray_values",
"asi8",
"_box_values",
"_box_func",
],
["mean", "freq", "freqstr", "asi8", "_box_values", "_box_func"],
DatetimeLikeArrayMixin,
)
class DatetimeIndexOpsMixin(ExtensionIndex):
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/indexes/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ class ExtensionIndex(Index):
Index subclass for indexes backed by ExtensionArray.
"""

# The base class already passes through to _data:
# size, __len__, dtype

_data: ExtensionArray

__eq__ = _make_wrapped_comparison_op("__eq__")
Expand All @@ -205,6 +208,9 @@ class ExtensionIndex(Index):
__le__ = _make_wrapped_comparison_op("__le__")
__ge__ = _make_wrapped_comparison_op("__ge__")

# ---------------------------------------------------------------------
# NDarray-Like Methods

def __getitem__(self, key):
result = self._data[key]
if isinstance(result, type(self._data)):
Expand All @@ -217,6 +223,8 @@ def __getitem__(self, key):
def __iter__(self):
return self._data.__iter__()

# ---------------------------------------------------------------------

@property
def _ndarray_values(self) -> np.ndarray:
return self._data._ndarray_values
Expand All @@ -235,6 +243,10 @@ def repeat(self, repeats, axis=None):
result = self._data.repeat(repeats, axis=axis)
return self._shallow_copy(result)

def _concat_same_dtype(self, to_concat, name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can type at some point (doc-string inherited)?

arr = type(self._data)._concat_same_type(to_concat)
return type(self)._simple_new(arr, name=name)

@Appender(Index.take.__doc__)
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
nv.validate_take(tuple(), kwargs)
Expand Down
37 changes: 2 additions & 35 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,10 @@ def func(intvidx_self, other, sort=False):
)
@inherit_names(["set_closed", "to_tuples"], IntervalArray, wrap=True)
@inherit_names(
[
"__len__",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we need the deleted ones?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh i c, by reading comments.

"__array__",
"overlaps",
"contains",
"size",
"dtype",
"left",
"right",
"length",
],
IntervalArray,
["__array__", "overlaps", "contains", "left", "right", "length"], IntervalArray,
)
@inherit_names(
["is_non_overlapping_monotonic", "mid", "_ndarray_values", "closed"],
IntervalArray,
cache=True,
["is_non_overlapping_monotonic", "mid", "closed"], IntervalArray, cache=True,
)
class IntervalIndex(IntervalMixin, ExtensionIndex):
_typ = "intervalindex"
Expand Down Expand Up @@ -943,33 +930,13 @@ def insert(self, loc, item):
new_right = self.right.insert(loc, right_insert)
return self._shallow_copy(new_left, new_right)

def _concat_same_dtype(self, to_concat, name):
"""
assert that we all have the same .closed
we allow a 0-len index here as well
"""
if not len({i.closed for i in to_concat if len(i)}) == 1:
raise ValueError(
"can only append two IntervalIndex objects "
"that are closed on the same side"
)
return super()._concat_same_dtype(to_concat, name)

@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
result = self._data.take(
indices, axis=axis, allow_fill=allow_fill, fill_value=fill_value, **kwargs
)
return self._shallow_copy(result)

def __getitem__(self, value):
result = self._data[value]
if isinstance(result, IntervalArray):
return self._shallow_copy(result)
else:
# scalar
return result

# --------------------------------------------------------------------
# Rendering Methods
# __repr__ associated methods are based on MultiIndex
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,7 @@ def test_append(self, closed):
)
tm.assert_index_equal(result, expected)

msg = (
"can only append two IntervalIndex objects that are closed "
"on the same side"
)
msg = "Intervals must all be closed on the same side"
for other_closed in {"left", "right", "both", "neither"} - {closed}:
index_other_closed = IntervalIndex.from_arrays(
[0, 1], [1, 2], closed=other_closed
Expand Down