From a262f3037b9802722a9de194d69575fb3f78caa9 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Sun, 11 Mar 2012 13:00:53 -0400 Subject: [PATCH] ENH: implement fill_value on SparseDataFrame._combine_frame, close #887 --- pandas/sparse/frame.py | 18 ++++++++++++++---- pandas/sparse/tests/test_sparse.py | 11 +++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pandas/sparse/frame.py b/pandas/sparse/frame.py index 3e9649dbd859c..c86d24771d977 100644 --- a/pandas/sparse/frame.py +++ b/pandas/sparse/frame.py @@ -412,16 +412,26 @@ def _combine_frame(self, other, func, fill_value=None, level=None): copy=False) new_index, new_columns = this.index, this.columns - if fill_value is not None or level is not None: + if level is not None: raise NotImplementedError if not self and not other: return SparseDataFrame(index=new_index) new_data = {} - for col in new_columns: - if col in this and col in other: - new_data[col] = func(this[col], other[col]) + if fill_value is not None: + # TODO: be a bit more intelligent here + for col in new_columns: + if col in this and col in other: + dleft = this[col].to_dense() + dright = other[col].to_dense() + result = dleft._binop(dright, func, fill_value=fill_value) + result = result.to_sparse(fill_value=this[col].fill_value) + new_data[col] = result + else: + for col in new_columns: + if col in this and col in other: + new_data[col] = func(this[col], other[col]) return self._constructor(data=new_data, index=new_index, columns=new_columns) diff --git a/pandas/sparse/tests/test_sparse.py b/pandas/sparse/tests/test_sparse.py index 86a94c6a8abf4..32c6ec4efebcc 100644 --- a/pandas/sparse/tests/test_sparse.py +++ b/pandas/sparse/tests/test_sparse.py @@ -1250,6 +1250,17 @@ def test_combine_first(self): assert_sp_frame_equal(result, result2) assert_sp_frame_equal(result, expected) + def test_combine_add(self): + df = self.frame.to_dense() + df2 = df.copy() + df2['C'][:3] = np.nan + df['A'][:3] = 5.7 + + result = df.to_sparse().add(df2.to_sparse(), fill_value=0) + expected = df.add(df2, fill_value=0).to_sparse() + assert_sp_frame_equal(result, expected) + + def _dense_series_compare(s, f): result = f(s) assert(isinstance(result, SparseSeries))