Skip to content

BUG: icol() should propegate fill_value for sparse data frames #2249 #2250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1734,8 +1734,15 @@ def icol(self, i):
return self.ix[:, i]

values = self._data.iget(i)
return self._col_klass.from_array(values, index=self.index,
name=label)
if hasattr(self,'default_fill_value'):
s = self._col_klass.from_array(values, index=self.index,
name=label,
fill_value= self.default_fill_value)
else:
s = self._col_klass.from_array(values, index=self.index,
name=label)

return s

def _ixs(self, i, axis=0):
if axis == 0:
Expand Down
4 changes: 2 additions & 2 deletions pandas/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ def __new__(cls, data, index=None, sparse_index=None, kind='block',
return output

@classmethod
def from_array(cls, arr, index=None, name=None, copy=False):
def from_array(cls, arr, index=None, name=None, copy=False,fill_value=None):
"""
Simplified alternate constructor
"""
return SparseSeries(arr, index=index, name=name, copy=copy)
return SparseSeries(arr, index=index, name=name, copy=copy,fill_value=fill_value)

def __init__(self, data, index=None, sparse_index=None, kind='block',
fill_value=None, name=None, copy=False):
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,11 @@ def test_irow_icol_duplicates(self):
xp = df.ix[:, [0]]
assert_frame_equal(rs, xp)

def test_icol_sparse_propegate_fill_value(self):
from pandas.sparse.api import SparseDataFrame
df=SparseDataFrame({'A' : [999,1]},default_fill_value=999)
self.assertTrue( len(df['A'].sp_values) == len(df.icol(0).sp_values))

def test_iget_value(self):
for i, row in enumerate(self.frame.index):
for j, col in enumerate(self.frame.columns):
Expand Down