Skip to content

Commit

Permalink
ENH: implement fill_value on SparseDataFrame._combine_frame, close #887
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Mar 11, 2012
1 parent dc86dba commit a262f30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a262f30

Please sign in to comment.