From d4c29ac7dab2e6d8d49ca6e9600ae03ece458e6e Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Mon, 5 Dec 2011 18:42:08 -0500 Subject: [PATCH] BUG: lookup locations in SparseSeries.get, start adding get_value/set_value re: #437 --- pandas/core/frame.py | 2 +- pandas/core/index.py | 4 ++-- pandas/core/sparse.py | 21 ++++++++++++++++++++- pandas/tests/test_frame.py | 7 +++++++ pandas/tests/test_sparse.py | 15 ++++++++++++++- 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c01f4a05faf06..c0b2bef5cd8a4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 ---------- diff --git a/pandas/core/index.py b/pandas/core/index.py index e1e88a3f9fef0..b885a8f52762a 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -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): """ diff --git a/pandas/core/sparse.py b/pandas/core/sparse.py index fa98bf5e5d0b2..e935df526f0db 100644 --- a/pandas/core/sparse.py +++ b/pandas/core/sparse.py @@ -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 @@ -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 @@ -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] diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index e115d7ce9edd3..46a72caebc1c2 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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 diff --git a/pandas/tests/test_sparse.py b/pandas/tests/test_sparse.py index c42102f213927..ed0da86eb069c 100644 --- a/pandas/tests/test_sparse.py +++ b/pandas/tests/test_sparse.py @@ -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] @@ -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:, :]