Skip to content

BUG: indexing with a range , #11652 #11653

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
Nov 20, 2015
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/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Bug Fixes




- Bug in indexing with a ``range``, (:issue:`11652`)


- Bug in ``to_sql`` using unicode column names giving UnicodeEncodeError with (:issue:`11431`).
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,8 @@ def get_loc(self, key, method=None, tolerance=None):
if tolerance is not None:
raise ValueError('tolerance argument only valid if using pad, '
'backfill or nearest lookups')
return self._engine.get_loc(_values_from_object(key))
key = _values_from_object(key)
return self._engine.get_loc(key)

indexer = self.get_indexer([key], method=method,
tolerance=tolerance)
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def _get_setitem_indexer(self, key):

if isinstance(key, tuple) and not self.ndim < len(key):
return self._convert_tuple(key, is_setter=True)
if isinstance(key, range):
return self._convert_range(key, is_setter=True)

try:
return self._convert_to_indexer(key, is_setter=True)
Expand Down Expand Up @@ -156,6 +158,10 @@ def _convert_tuple(self, key, is_setter=False):
keyidx.append(idx)
return tuple(keyidx)

def _convert_range(self, key, is_setter=False):
""" convert a range argument """
return list(key)

def _convert_scalar_indexer(self, key, axis):
# if we are accessing via lowered dim, use the last dim
ax = self.obj._get_axis(min(axis, self.ndim - 1))
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4726,6 +4726,17 @@ def test_indexing_dtypes_on_empty(self):
assert_series_equal(df2.loc[:,'a'], df2.iloc[:,0])
assert_series_equal(df2.loc[:,'a'], df2.ix[:,0])

def test_range_in_series_indexing(self):
# range can cause an indexing error
# GH 11652
for x in [5, 999999, 1000000]:
s = pd.Series(index=range(x))
s.loc[range(1)] = 42
assert_series_equal(s.loc[range(1)],Series(42.0,index=[0]))

s.loc[range(2)] = 43
assert_series_equal(s.loc[range(2)],Series(43.0,index=[0,1]))

@slow
def test_large_dataframe_indexing(self):
#GH10692
Expand Down