Skip to content

Commit a2a3443

Browse files
jbrockmendelukarroum
authored andcommitted
BUG: IntervalIndex.take without fill_value (pandas-dev#37330)
1 parent ff611f6 commit a2a3443

File tree

8 files changed

+24
-13
lines changed

8 files changed

+24
-13
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ Strings
434434

435435
Interval
436436
^^^^^^^^
437-
437+
- Bug in :meth:`IntervalIndex.take` with negative indices and ``fill_value=None`` (:issue:`37330`)
438438
-
439439
-
440440

pandas/core/arrays/_mixins.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ def take(
5555
indices: Sequence[int],
5656
allow_fill: bool = False,
5757
fill_value: Any = None,
58+
axis: int = 0,
5859
) -> _T:
5960
if allow_fill:
6061
fill_value = self._validate_fill_value(fill_value)
6162

6263
new_data = take(
63-
self._ndarray, indices, allow_fill=allow_fill, fill_value=fill_value
64+
self._ndarray,
65+
indices,
66+
allow_fill=allow_fill,
67+
fill_value=fill_value,
68+
axis=axis,
6469
)
6570
return self._from_backing_data(new_data)
6671

pandas/core/arrays/interval.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ def __eq__(self, other):
544544
if is_interval_dtype(other_dtype):
545545
if self.closed != other.categories.closed:
546546
return np.zeros(len(self), dtype=bool)
547-
other = other.categories.take(other.codes)
547+
other = other.categories.take(
548+
other.codes, allow_fill=True, fill_value=other.categories._na_value
549+
)
548550

549551
# interval-like -> need same closed and matching endpoints
550552
if is_interval_dtype(other_dtype):

pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def value_counts(
714714
# lab is a Categorical with categories an IntervalIndex
715715
lab = cut(Series(val), bins, include_lowest=True)
716716
lev = lab.cat.categories
717-
lab = lev.take(lab.cat.codes)
717+
lab = lev.take(lab.cat.codes, allow_fill=True, fill_value=lev._na_value)
718718
llab = lambda lab, inc: lab[inc]._multiindex.codes[-1]
719719

720720
if is_interval_dtype(lab.dtype):

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def _assert_take_fillable(
769769
values, indices, allow_fill=allow_fill, fill_value=na_value
770770
)
771771
else:
772-
taken = values.take(indices)
772+
taken = algos.take(values, indices, allow_fill=False, fill_value=na_value)
773773
return taken
774774

775775
_index_shared_docs[

pandas/core/indexes/interval.py

-7
Original file line numberDiff line numberDiff line change
@@ -909,13 +909,6 @@ def insert(self, loc, item):
909909
result = IntervalArray.from_arrays(new_left, new_right, closed=self.closed)
910910
return self._shallow_copy(result)
911911

912-
@Appender(_index_shared_docs["take"] % _index_doc_kwargs)
913-
def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
914-
result = self._data.take(
915-
indices, axis=axis, allow_fill=allow_fill, fill_value=fill_value, **kwargs
916-
)
917-
return self._shallow_copy(result)
918-
919912
# --------------------------------------------------------------------
920913
# Rendering Methods
921914
# __repr__ associated methods are based on MultiIndex

pandas/tests/indexes/categorical/test_astype.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_astype(self):
2525
)
2626

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

3131
result = IntervalIndex(result.values)

pandas/tests/indexes/common.py

+11
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,17 @@ def test_take_invalid_kwargs(self):
407407
with pytest.raises(ValueError, match=msg):
408408
idx.take(indices, mode="clip")
409409

410+
def test_take_minus1_without_fill(self, index):
411+
# -1 does not get treated as NA unless allow_fill=True is passed
412+
if len(index) == 0:
413+
# Test is not applicable
414+
return
415+
416+
result = index.take([0, 0, -1])
417+
418+
expected = index.take([0, 0, len(index) - 1])
419+
tm.assert_index_equal(result, expected)
420+
410421
def test_repeat(self):
411422
rep = 2
412423
i = self.create_index()

0 commit comments

Comments
 (0)