Skip to content

ENH: bfill for interval indexing added GH#51503 #51503 #52103

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

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
c2651eb
ENH: bfill for interval indexing added GH#51503
hamedgibago Mar 11, 2023
60afcf4
CLN: pylint error R1714 debugged
hamedgibago Mar 11, 2023
3e21bb7
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 12, 2023
3bb8291
ENH: bfill for interval indexing added GH#51503
hamedgibago Mar 15, 2023
d3ea64d
Merge branch 'main' of https://github.com/hamedgibago/pandas
hamedgibago Mar 15, 2023
74d6141
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 16, 2023
2d1d037
BUG: .any() .all() for numpy
hamedgibago Mar 18, 2023
a6faa02
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 18, 2023
a5e9bf7
Merge branch 'main' of https://github.com/hamedgibago/pandas
hamedgibago Mar 18, 2023
5263362
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 19, 2023
790b195
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 23, 2023
4c95092
GH#51503 for loops improved
hamedgibago Mar 26, 2023
67d569f
Merge branch 'main' into main
hamedgibago Mar 26, 2023
83741c4
Merge branch 'main' into main
hamedgibago Mar 27, 2023
808f777
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 28, 2023
2c2e57c
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 30, 2023
e49e132
BUG: debuged is_categorical_dtype is not defined
hamedgibago Mar 30, 2023
87c5118
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 30, 2023
d16cd06
Merge branch 'main' into main
hamedgibago Mar 30, 2023
8e2b520
Merge branch 'pandas-dev:main' into main
hamedgibago Mar 31, 2023
0234477
Merge branch 'main' into main
hamedgibago Mar 31, 2023
3aec7f7
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 2, 2023
3e359d1
Merge branch 'main' into main
hamedgibago Apr 2, 2023
8d635d1
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 4, 2023
d8f3797
Merge branch 'main' into main
hamedgibago Apr 4, 2023
6289857
Merge branch 'main' into main
hamedgibago Apr 6, 2023
c302322
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 10, 2023
4c02bb1
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 10, 2023
64943a1
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 11, 2023
bc6ce78
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 12, 2023
8222f25
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 13, 2023
4c503f7
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 15, 2023
103790b
Merge branch 'pandas-dev:main' into main
hamedgibago Apr 19, 2023
e28eabd
Merge branch 'main' into main
hamedgibago Apr 22, 2023
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
5 changes: 3 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3900,9 +3900,10 @@ def _check_indexing_method(
"tolerance not implemented yet for MultiIndex"
)

if isinstance(self.dtype, (IntervalDtype, CategoricalDtype)):
if isinstance(self.dtype, (CategoricalDtype)):
# GH#37871 for now this is only for IntervalIndex and CategoricalIndex
if method is not None:
if method is not None: # and method not in ("bfill", "backfill")
# and not is_interval_dtype(self.dtype):
raise NotImplementedError(
f"method {method} not yet implemented for {type(self).__name__}"
)
Expand Down
25 changes: 25 additions & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,38 @@ def _get_indexer(
# we should always have self._should_partial_index(target) here
target = self._maybe_convert_i8(target)
indexer = self._engine.get_indexer(target.values)

# if indexer is int:
if (indexer == [-1]).all() and method in ("bfill", "backfill"):
# GH#51503
indexer = self._get_indexer_ffill(target, method)

else:
# heterogeneous scalar index: defer elementwise to get_loc
# we should always have self._should_partial_index(target) here
return self._get_indexer_pointwise(target)[0]

return ensure_platform_int(indexer)

def _get_indexer_ffill(self, target, method):
# GH#51503
if method in ("bfill", "backfill"):
temp = []
_len_self = len(self)
for i in range(_len_self):
# not sure to call this method for every value
t = self._maybe_cast_listlike_indexer([self[i]])
temp.append(self._maybe_convert_i8(t))

for i in range(_len_self):
# TODO: Comparing is not written professionally. Needs more review
if target[0] < temp[i].values[0].left:
return [i]

return -1
else:
return -1

@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
def get_indexer_non_unique(
self, target: Index
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexes/interval/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,24 @@ def test_get_indexer_interval_index(self, box):
expected = np.array([-1, -1, -1], dtype=np.intp)
tm.assert_numpy_array_equal(actual, expected)

def test_get_indexer_with_backfill(self):
# GH#51503
dt = Timestamp.now()
h = Timedelta("1h")

# 3 simple interval ranges, with a big gap between 2nd and 3rd
intervals = IntervalIndex.from_arrays(
[dt, dt + h, dt + 10 * h], [dt + h, dt + 2 * h, dt + 11 * h], closed="left"
)

result = intervals.get_indexer([dt + 5 * h], method="bfill")
# result = intervals.get_indexer([dt + 5 * h])
# result = intervals.get_indexer([dt + 10 * h], method="nearest")

expected = np.array([2], dtype=np.intp)

tm.assert_numpy_array_equal(result, expected)


class TestSliceLocs:
def test_slice_locs_with_interval(self):
Expand Down