Skip to content

Commit 7879205

Browse files
author
Artemy Kolchinsky
committed
Fix to allow sparse dataframes to have nan column labels
Support for nan columns Fix Trigger Travis CI jreback fixes Release note update
1 parent 5dff7df commit 7879205

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

doc/source/whatsnew/v0.16.1.txt

+3
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,6 @@ Bug Fixes
9898
- Bug in ``read_csv`` and ``read_table`` when using ``skip_rows`` parameter if blank lines are present. (:issue:`9832`)
9999

100100
- Bug in ``read_csv()`` interprets ``index_col=True`` as ``1`` (:issue:`9798`)
101+
102+
- Bug in which ``SparseDataFrame`` could not take `nan` as a column name (:issue:`8822`)
103+

pandas/sparse/frame.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(self, data=None, index=None, columns=None,
100100
mgr = self._init_mgr(
101101
data, axes=dict(index=index, columns=columns), dtype=dtype, copy=copy)
102102
elif data is None:
103-
data = {}
103+
data = DataFrame()
104104

105105
if index is None:
106106
index = Index([])
@@ -115,7 +115,7 @@ def __init__(self, data=None, index=None, columns=None,
115115
index=index,
116116
kind=self._default_kind,
117117
fill_value=self._default_fill_value)
118-
mgr = dict_to_manager(data, columns, index)
118+
mgr = df_to_manager(data, columns, index)
119119
if dtype is not None:
120120
mgr = mgr.astype(dtype)
121121

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

184-
return dict_to_manager(sdict, columns, index)
184+
return df_to_manager(sdict, columns, index)
185185

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

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

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

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

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

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

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

748748

749749
def stack_sparse_frame(frame):

pandas/sparse/tests/test_sparse.py

+6
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,12 @@ def test_as_blocks(self):
16631663
self.assertEqual(list(df_blocks.keys()), ['float64'])
16641664
assert_frame_equal(df_blocks['float64'], df)
16651665

1666+
def test_nan_columnname(self):
1667+
# GH 8822
1668+
nan_colname = DataFrame(Series(1.0,index=[0]),columns=[nan])
1669+
nan_colname_sparse = nan_colname.to_sparse()
1670+
self.assertTrue(np.isnan(nan_colname_sparse.columns[0]))
1671+
16661672

16671673
def _dense_series_compare(s, f):
16681674
result = f(s)

0 commit comments

Comments
 (0)