wesm/pandas — Issue #687: SparseSeries.combine_first(Series) fails when combining with a Series
Author: creeson
Created: 2012-01-25 22:05:17+00:00
State: open

Contrary to the documentation, the SparseSeries code, when doing a combine_first, actually assumes the other series is a SparseSeries. It performs a other.to_dense(), which fails on a Series.

def combine_first(self, other):
"""
Combine Series values, choosing the calling Series's values
first. Result index will be the union of the two indexes

Parameters
----------
other : Series

Returns
-------
y : Series
"""
dense_combined = self.to_dense().combine_first(other.to_dense())
return dense_combined.to_sparse(fill_value=self.fill_value)

I changed my code locally to the following, which seems to work (though I'm sure there is a more elegant way):
if isinstance(other, SparseSeries):
dense_combined = self.to_dense().combine_first(other.to_dense())
elif isinstance(other, Series):
dense_combined = self.to_dense().combine_first(other)
return dense_combined.to_sparse(fill_value=self.fill_value)