Skip to content

Commit

Permalink
BUG: lookup locations in SparseSeries.get, start adding get_value/set…
Browse files Browse the repository at this point in the history
…_value re: #437
  • Loading branch information
wesm committed Dec 5, 2011
1 parent 7394761 commit d4c29ac
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ def __array_wrap__(self, result):

def get_value(self, index, col):
"""
Retrieve single value at passed column and index
Quickly retrieve single value at passed column and index
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,12 @@ def get_value(self, arr, key):
"""
return self._engine.get_value(arr, key)

def put_value(self, arr, key, value):
def set_value(self, arr, key, value):
"""
Fast lookup of value from 1-dimensional ndarray. Only use this if you
know what you're doing
"""
self._engine.put_value(arr, key, value)
self._engine.set_value(arr, key, value)

def get_indexer(self, target, method=None):
"""
Expand Down
21 changes: 20 additions & 1 deletion pandas/core/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ def get(self, label, default=None):
y : scalar
"""
if label in self.index:
return self._get_val_at(label)
loc = self.index.get_loc(label)
return self._get_val_at(loc)
else:
return default

Expand All @@ -398,6 +399,13 @@ def _get_val_at(self, loc):
else:
return ndarray.__getitem__(self, sp_loc)

def get_value(self, label):
loc = self.index.get_loc(label)
return self._get_val_at(loc)

def set_value(self, label, value):
raise Exception('SparseSeries is immutable')

def take(self, indices):
"""
Sparse-compatible version of ndarray.take
Expand Down Expand Up @@ -932,6 +940,17 @@ def __getitem__(self, item):
else: # pragma: no cover
raise

def get_value(self, index, col):
s = self._series[col]
return s.get_value(index)
if __debug__: get_value.__doc__ = DataFrame.get_value.__doc__

def set_value(self, index, col, value):
"""
Not implemented for SparseDataFrame
"""
raise Exception('Values in SparseDataFrame are immutable')

def _slice(self, slobj, axis=0):
if axis == 0:
new_index = self.index[slobj]
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,13 @@ def test_getitem_pop_assign_name(self):
s2 = s.ix[:]
self.assertEqual(s2.name, 'B')

def test_get_value(self):
for idx in self.frame.index:
for col in self.frame.columns:
result = self.frame.get_value(idx, col)
expected = self.frame[col][idx]
assert_almost_equal(result, expected)

def test_join_index(self):
# left / right

Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,20 @@ def _check_getitem(sp, dense):
self.assertRaises(Exception, self.btseries.__getitem__,
self.btseries.index[-1] + BDay())

def test_get(self):
def test_get_get_value(self):
assert_almost_equal(self.bseries.get(10), self.bseries[10])
self.assert_(self.bseries.get(len(self.bseries) + 1) is None)

dt = self.btseries.index[10]
result = self.btseries.get(dt)
expected = self.btseries.to_dense()[dt]
assert_almost_equal(result, expected)

assert_almost_equal(self.bseries.get_value(10), self.bseries[10])

def test_set_value(self):
self.assertRaises(Exception, self.bseries.set_value, 10, 0)

def test_getitem_fancy_index(self):
idx = self.bseries.index
res = self.bseries[::2]
Expand Down Expand Up @@ -819,6 +829,9 @@ def test_scalar_ops(self):
def test_getitem(self):
pass

def test_set_value(self):
self.assertRaises(Exception, self.frame.set_value, 10, 0)

def test_fancy_index_misc(self):
# axis = 0
sliced = self.frame.ix[-2:, :]
Expand Down

0 comments on commit d4c29ac

Please sign in to comment.