Skip to content

Commit 66a4714

Browse files
author
Artemy Kolchinsky
committed
Fix to allow sparse dataframes to have nan column labels
Support for nan columns Fix
1 parent c03e92f commit 66a4714

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

pandas/sparse/frame.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __init__(self, data=None, index=None, columns=None,
101101
mgr = self._init_mgr(
102102
data, axes=dict(index=index, columns=columns), dtype=dtype, copy=copy)
103103
elif data is None:
104-
data = {}
104+
data = DataFrame()
105105

106106
if index is None:
107107
index = Index([])
@@ -116,7 +116,7 @@ def __init__(self, data=None, index=None, columns=None,
116116
index=index,
117117
kind=self._default_kind,
118118
fill_value=self._default_fill_value)
119-
mgr = dict_to_manager(data, columns, index)
119+
mgr = df_to_manager(data, columns, index)
120120
if dtype is not None:
121121
mgr = mgr.astype(dtype)
122122

@@ -156,7 +156,7 @@ def _init_dict(self, data, index, columns, dtype=None):
156156
kind=self._default_kind,
157157
fill_value=self._default_fill_value,
158158
copy=True)
159-
sdict = {}
159+
sdict = DataFrame()
160160
for k, v in compat.iteritems(data):
161161
if isinstance(v, Series):
162162
# Force alignment, no copy necessary
@@ -182,7 +182,7 @@ def _init_dict(self, data, index, columns, dtype=None):
182182
if c not in sdict:
183183
sdict[c] = sp_maker(nan_vec)
184184

185-
return dict_to_manager(sdict, columns, index)
185+
return df_to_manager(sdict, columns, index)
186186

187187
def _init_matrix(self, data, index, columns, dtype=None):
188188
data = _prep_ndarray(data, copy=False)
@@ -229,12 +229,12 @@ def _unpickle_sparse_frame_compat(self, state):
229229
else:
230230
index = idx
231231

232-
series_dict = {}
232+
series_dict = DataFrame()
233233
for col, (sp_index, sp_values) in compat.iteritems(series):
234234
series_dict[col] = SparseSeries(sp_values, sparse_index=sp_index,
235235
fill_value=fv)
236236

237-
self._data = dict_to_manager(series_dict, columns, index)
237+
self._data = df_to_manager(series_dict, columns, index)
238238
self._default_fill_value = fv
239239
self._default_kind = kind
240240

@@ -738,13 +738,13 @@ def applymap(self, func):
738738
"""
739739
return self.apply(lambda x: lmap(func, x))
740740

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

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

747-
return create_block_manager_from_arrays([sdict[c] for c in columns], columns, axes)
747+
return create_block_manager_from_arrays([sdf[c] for c in columns], columns, axes)
748748

749749

750750
def stack_sparse_frame(frame):

pandas/sparse/tests/test_sparse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,11 @@ def test_as_blocks(self):
15271527
self.assertEqual(list(df_blocks.keys()), ['float64'])
15281528
assert_frame_equal(df_blocks['float64'], df)
15291529

1530+
def test_nan_columnname(self):
1531+
nan_colname = DataFrame(Series(1.0,index=[0]),columns=[nan])
1532+
nan_colname_sparse = nan_colname.to_sparse()
1533+
assert(np.isnan(nan_colname_sparse.columns[0]))
1534+
15301535

15311536
def _dense_series_compare(s, f):
15321537
result = f(s)

0 commit comments

Comments
 (0)