Skip to content

BUG: IntervalIndex.take without fill_value #37330

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 17 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9095693
BUG: IntervalIndex.take without fill_value
jbrockmendel Oct 22, 2020
0bbd54c
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 22, 2020
ac57e0e
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 23, 2020
c8b99a1
whatsnew
jbrockmendel Oct 23, 2020
28ff38a
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 23, 2020
00e4205
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 23, 2020
51e2619
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 24, 2020
8530e61
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 24, 2020
0738fc9
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 25, 2020
9d51684
dummy commit to force CI
jbrockmendel Oct 25, 2020
2e907c5
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 26, 2020
bdf5a5c
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 26, 2020
3aac8e5
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 27, 2020
d841c9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 30, 2020
655983f
fix slow tests
jbrockmendel Oct 30, 2020
b5fab50
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 30, 2020
23b472b
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Oct 31, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ Strings

Interval
^^^^^^^^

- Bug in :meth:`IntervalIndex.take` with negative indices and ``fill_value=None`` (:issue:`37330`)
-
-

Expand Down
7 changes: 6 additions & 1 deletion pandas/core/arrays/_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ def take(
indices: Sequence[int],
allow_fill: bool = False,
fill_value: Any = None,
axis: int = 0,
) -> _T:
if allow_fill:
fill_value = self._validate_fill_value(fill_value)

new_data = take(
self._ndarray, indices, allow_fill=allow_fill, fill_value=fill_value
self._ndarray,
indices,
allow_fill=allow_fill,
fill_value=fill_value,
axis=axis,
)
return self._from_backing_data(new_data)

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,9 @@ def __eq__(self, other):
if is_interval_dtype(other_dtype):
if self.closed != other.categories.closed:
return np.zeros(len(self), dtype=bool)
other = other.categories.take(other.codes)
other = other.categories.take(
other.codes, allow_fill=True, fill_value=other.categories._na_value
)

# interval-like -> need same closed and matching endpoints
if is_interval_dtype(other_dtype):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def value_counts(
# lab is a Categorical with categories an IntervalIndex
lab = cut(Series(val), bins, include_lowest=True)
lev = lab.cat.categories
lab = lev.take(lab.cat.codes)
lab = lev.take(lab.cat.codes, allow_fill=True, fill_value=lev._na_value)
llab = lambda lab, inc: lab[inc]._multiindex.codes[-1]

if is_interval_dtype(lab.dtype):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ def _assert_take_fillable(
values, indices, allow_fill=allow_fill, fill_value=na_value
)
else:
taken = values.take(indices)
taken = algos.take(values, indices, allow_fill=False, fill_value=na_value)
return taken

_index_shared_docs[
Expand Down
7 changes: 0 additions & 7 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,6 @@ def insert(self, loc, item):
result = IntervalArray.from_arrays(new_left, new_right, closed=self.closed)
return self._shallow_copy(result)

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

# --------------------------------------------------------------------
# Rendering Methods
# __repr__ associated methods are based on MultiIndex
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/categorical/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_astype(self):
)

result = ci.astype("interval")
expected = ii.take([0, 1, -1])
expected = ii.take([0, 1, -1], allow_fill=True, fill_value=np.nan)
tm.assert_index_equal(result, expected)

result = IntervalIndex(result.values)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,17 @@ def test_take_invalid_kwargs(self):
with pytest.raises(ValueError, match=msg):
idx.take(indices, mode="clip")

def test_take_minus1_without_fill(self, index):
# -1 does not get treated as NA unless allow_fill=True is passed
if len(index) == 0:
# Test is not applicable
return

result = index.take([0, 0, -1])

expected = index.take([0, 0, len(index) - 1])
tm.assert_index_equal(result, expected)

def test_repeat(self):
rep = 2
i = self.create_index()
Expand Down