Skip to content

Commit

Permalink
added missing Series.ix case for completeness, optimized DataFrame.ix…
Browse files Browse the repository at this point in the history
…[...] to reduce unnecessary computation in some cases
  • Loading branch information
wesm committed Jun 23, 2011
1 parent ee748aa commit 4e86748
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _need_slice(obj):
return obj.start is not None or obj.stop is not None

def _check_step(obj):
if obj.step is not None:
if obj.step is not None and obj.step != 1:
raise Exception('steps other than 1 are not supported')

def _ensure_index(index_like):
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2478,12 +2478,20 @@ def _fancy_getitem(self, key, axis=0):
return self.reindex(**{axis_name : key})

def _fancy_getitem_tuple(self, rowkey, colkey):
result = self._fancy_getitem_axis(colkey, axis=1)
def _is_label_like(key):
# select a label or row
return not isinstance(key, slice) and not _is_list_like(key)

# to avoid wasted computation
# df.ix[d1:d2, 0] -> columns first (True)
# df.ix[0, ['C', 'B', A']] -> rows first (False)
if _is_label_like(colkey):
return self._fancy_getitem_axis(colkey, axis=1).ix[rowkey]
elif _is_label_like(rowkey):
return self._fancy_getitem_axis(rowkey, axis=0).ix[colkey]

if isinstance(result, Series):
result = result[rowkey]
else:
result = result._fancy_getitem_axis(rowkey, axis=0)
result = self._fancy_getitem_axis(colkey, axis=1)
result = result._fancy_getitem_axis(rowkey, axis=0)

return result

Expand Down
16 changes: 10 additions & 6 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

# pylint: disable=E1101,E1103
# pylint: disable=W0703,W0622
# pylint: disable=W0703,W0622,W0613

import itertools
import operator
Expand All @@ -14,12 +14,12 @@
import numpy as np

from pandas.core.common import isnull, notnull
from pandas.core.common import (_check_step, _need_slice, _is_label_slice,
_ensure_index)
from pandas.core.common import (_need_slice, _is_label_slice,
_is_list_like, _ensure_index)

from pandas.core.daterange import DateRange
from pandas.core.generic import PandasGeneric
from pandas.core.index import Index, NULL_INDEX
from pandas.core.index import Index
import pandas.core.datetools as datetools
import pandas.lib.tseries as tseries

Expand Down Expand Up @@ -1376,8 +1376,10 @@ def _fancy_getitem(self, key):
return self[i:j]
else:
return self[key]
else:
elif _is_list_like(key):
return self.reindex(key)
else:
return self[key]

def _fancy_setitem(self, key, value):
if _isboolarr(key):
Expand All @@ -1392,11 +1394,13 @@ def _fancy_setitem(self, key, value):
self[i:j] = value
else:
self[key] = value
else:
elif _is_list_like(key):
inds, mask = self.index.get_indexer(key)
if not mask.all():
raise Exception('Indices %s not found' % key[-mask])
self.put(inds, value)
else:
self[key] = value

class TimeSeries(Series):
pass
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def test_getitem_fancy_2d(self):

assert_frame_equal(ix[:, ['B', 'A']], f.reindex(columns=['B', 'A']))

subidx = self.frame.index[[5, 4, 1]]
assert_frame_equal(ix[subidx, ['B', 'A']],
f.reindex(index=subidx, columns=['B', 'A']))

# slicing rows, etc.
assert_frame_equal(ix[5:10], f[5:10])
assert_frame_equal(ix[5:10, :], f[5:10])
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ def test_ix_getitem(self):
mask = self.series > self.series.median()
assert_series_equal(self.series.ix[mask], self.series[mask])

# ask for index value
self.assertEquals(self.series.ix[d1], self.series[d1])
self.assertEquals(self.series.ix[d2], self.series[d2])

def test_ix_setitem(self):
inds = self.series.index[[3,4,7]]

Expand All @@ -268,6 +272,12 @@ def test_ix_setitem(self):
expected[5:16] = 6 # because it's inclusive
assert_series_equal(result, expected)

# set index value
self.series.ix[d1] = 4
self.series.ix[d2] = 6
self.assertEquals(self.series[d1], 4)
self.assertEquals(self.series[d2], 6)

def test_ix_setitem_boolean(self):
mask = self.series > self.series.median()

Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
coverage erase
# nosetests -w pandas --with-coverage --cover-package=pandas --pdb-failure --pdb
nosetests -w pandas/io --with-coverage --cover-package=pandas.io --pdb-failure --pdb
# nosetests -w pandas/core --with-coverage --cover-package=pandas.core --pdb-failure --pdb
# nosetests -w pandas/io --with-coverage --cover-package=pandas.io --pdb-failure --pdb
nosetests -w pandas/core --with-coverage --cover-package=pandas.core --pdb-failure --pdb
# nosetests -w pandas/stats --with-coverage --cover-package=pandas.stats
# coverage run runtests.py

0 comments on commit 4e86748

Please sign in to comment.