Skip to content

PERF: optimize iloc for unique case #4070

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
Jun 28, 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
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1981,7 +1981,8 @@ def _ixs(self, i, axis=0, copy=False):
if isinstance(label, Index):

# a location index by definition
return self.reindex(label, takeable=True)
i = _maybe_convert_indices(i, len(self._get_axis(axis)))
return self.reindex(i, takeable=True)
else:
try:
new_values = self._data.fast_2d_xs(i, copy=copy)
Expand Down
16 changes: 10 additions & 6 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,27 +938,31 @@ def reindex(self, target, method=None, level=None, limit=None,
_, indexer, _ = self._join_level(target, level, how='right',
return_indexers=True)
else:

if self.equals(target):
indexer = None

# to avoid aliasing an existing index
if copy_if_needed and target.name != self.name and self.name is not None:
if target.name is None:
target = self.copy()

else:

if takeable:
if method is not None or limit is not None:
raise ValueError("cannot do a takeable reindex with "
"with a method or limit")
return self[target], target

if self.is_unique:
indexer = self.get_indexer(target, method=method,
limit=limit)
else:
if method is not None or limit is not None:
raise ValueError("cannot reindex a non-unique index "
"with a method or limit")
if takeable:
indexer = target
missing = (target>=len(target)).nonzero()[0]
else:
indexer, missing = self.get_indexer_non_unique(target)
indexer, _ = self.get_indexer_non_unique(target)

return target, indexer

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
_NS_DTYPE, _TD_DTYPE)
from pandas.core.index import (Index, MultiIndex, InvalidIndexError,
_ensure_index, _handle_legacy_indexes)
from pandas.core.indexing import _SeriesIndexer, _check_bool_indexer, _check_slice_bounds
from pandas.core.indexing import (_SeriesIndexer, _check_bool_indexer,
_check_slice_bounds, _maybe_convert_indices)
from pandas.tseries.index import DatetimeIndex
from pandas.tseries.period import PeriodIndex, Period
from pandas.util import py3compat
Expand Down Expand Up @@ -598,7 +599,8 @@ def _ixs(self, i, axis=0):
else:
label = self.index[i]
if isinstance(label, Index):
return self.reindex(label, takeable=True)
i = _maybe_convert_indices(i, len(self))
return self.reindex(i, takeable=True)
else:
return _index.get_value_at(self, i)

Expand Down