Skip to content

PERF: calling get_indexer twice when a specialized indexer (e.g. pad) could be called instead #5178

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 1 commit into from
Oct 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/v0.13.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ Enhancements
mask
dfi[mask.any(1)]

:ref:`See the docs<indexing.basics.indexing_isin>` for more.
:ref:`See the docs<indexing.basics.indexing_isin>` for more.

- ``Series`` now supports a ``to_frame`` method to convert it to a single-column DataFrame (:issue:`5164`)

Expand Down
15 changes: 13 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,8 +1241,19 @@ def _reindex_axes(self, axes, level, limit, method, fill_value, copy, takeable=F
labels = _ensure_index(labels)

axis = self._get_axis_number(a)
new_index, indexer = self._get_axis(a).reindex(
labels, level=level, limit=limit, takeable=takeable)
ax = self._get_axis(a)
try:
new_index, indexer = ax.reindex(labels, level=level,
limit=limit, method=method, takeable=takeable)
except (ValueError):

# catch trying to reindex a non-monotonic index with a specialized indexer
# e.g. pad, so fallback to the regular indexer
# this will show up on reindexing a not-naturally ordering series, e.g.
# Series([1,2,3,4],index=['a','b','c','d']).reindex(['c','b','g'],method='pad')
new_index, indexer = ax.reindex(labels, level=level,
limit=limit, method=None, takeable=takeable)

obj = obj._reindex_with_indexers(
{axis: [new_index, indexer]}, method=method, fill_value=fill_value,
limit=limit, copy=copy)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def reindex_items_from(self, new_ref_items, indexer=None, method=None, fill_valu
if indexer is None:
new_ref_items, indexer = self.items.reindex(new_ref_items, limit=limit)

needs_fill = method is not None and limit is None
if fill_value is None:
fill_value = self.fill_value

Expand All @@ -245,14 +246,13 @@ def reindex_items_from(self, new_ref_items, indexer=None, method=None, fill_valu
new_items = self.items.take(masked_idx)

# fill if needed
fill_method = method is not None or limit is not None
if fill_method:
if needs_fill:
new_values = com.interpolate_2d(new_values, method=method, limit=limit, fill_value=fill_value)

block = make_block(new_values, new_items, new_ref_items, ndim=self.ndim, fastpath=True)

# down cast if needed
if not self.is_float and (fill_method or notnull(fill_value)):
if not self.is_float and (needs_fill or notnull(fill_value)):
block = block.downcast()

return block
Expand Down