Skip to content

Commit

Permalink
Fix to allow sparse dataframes to have nan column labels
Browse files Browse the repository at this point in the history
Support for nan columns

Fix

Trigger Travis CI
  • Loading branch information
Artemy Kolchinsky committed Mar 26, 2015
1 parent a477202 commit c776988
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self, data=None, index=None, columns=None,
mgr = self._init_mgr(
data, axes=dict(index=index, columns=columns), dtype=dtype, copy=copy)
elif data is None:
data = {}
data = DataFrame()

if index is None:
index = Index([])
Expand All @@ -115,7 +115,7 @@ def __init__(self, data=None, index=None, columns=None,
index=index,
kind=self._default_kind,
fill_value=self._default_fill_value)
mgr = dict_to_manager(data, columns, index)
mgr = df_to_manager(data, columns, index)
if dtype is not None:
mgr = mgr.astype(dtype)

Expand Down Expand Up @@ -155,7 +155,7 @@ def _init_dict(self, data, index, columns, dtype=None):
kind=self._default_kind,
fill_value=self._default_fill_value,
copy=True)
sdict = {}
sdict = DataFrame()
for k, v in compat.iteritems(data):
if isinstance(v, Series):
# Force alignment, no copy necessary
Expand All @@ -181,7 +181,7 @@ def _init_dict(self, data, index, columns, dtype=None):
if c not in sdict:
sdict[c] = sp_maker(nan_vec)

return dict_to_manager(sdict, columns, index)
return df_to_manager(sdict, columns, index)

def _init_matrix(self, data, index, columns, dtype=None):
data = _prep_ndarray(data, copy=False)
Expand Down Expand Up @@ -228,12 +228,12 @@ def _unpickle_sparse_frame_compat(self, state):
else:
index = idx

series_dict = {}
series_dict = DataFrame()
for col, (sp_index, sp_values) in compat.iteritems(series):
series_dict[col] = SparseSeries(sp_values, sparse_index=sp_index,
fill_value=fv)

self._data = dict_to_manager(series_dict, columns, index)
self._data = df_to_manager(series_dict, columns, index)
self._default_fill_value = fv
self._default_kind = kind

Expand Down Expand Up @@ -737,13 +737,13 @@ def applymap(self, func):
"""
return self.apply(lambda x: lmap(func, x))

def dict_to_manager(sdict, columns, index):
""" create and return the block manager from a dict of series, columns, index """
def df_to_manager(sdf, columns, index):
""" create and return the block manager from a dataframe of series, columns, index """

# from BlockManager perspective
axes = [_ensure_index(columns), _ensure_index(index)]

return create_block_manager_from_arrays([sdict[c] for c in columns], columns, axes)
return create_block_manager_from_arrays([sdf[c] for c in columns], columns, axes)


def stack_sparse_frame(frame):
Expand Down
5 changes: 5 additions & 0 deletions pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,11 @@ def test_as_blocks(self):
self.assertEqual(list(df_blocks.keys()), ['float64'])
assert_frame_equal(df_blocks['float64'], df)

def test_nan_columnname(self):
nan_colname = DataFrame(Series(1.0,index=[0]),columns=[nan])
nan_colname_sparse = nan_colname.to_sparse()
assert(np.isnan(nan_colname_sparse.columns[0]))


def _dense_series_compare(s, f):
result = f(s)
Expand Down

0 comments on commit c776988

Please sign in to comment.