Skip to content
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

BUG: Error when creating sparse dataframe with nan column label #8822

Merged
merged 1 commit into from
Apr 11, 2015
Merged
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
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.16.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ Bug Fixes
- Bug in ``read_csv`` and ``read_table`` when using ``skip_rows`` parameter if blank lines are present. (:issue:`9832`)

- Bug in ``read_csv()`` interprets ``index_col=True`` as ``1`` (:issue:`9798`)

- Bug in which ``SparseDataFrame`` could not take `nan` as a column name (:issue:`8822`)

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
6 changes: 6 additions & 0 deletions pandas/sparse/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,12 @@ def test_as_blocks(self):
self.assertEqual(list(df_blocks.keys()), ['float64'])
assert_frame_equal(df_blocks['float64'], df)

def test_nan_columnname(self):
# GH 8822
nan_colname = DataFrame(Series(1.0,index=[0]),columns=[nan])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue as a comment here

nan_colname_sparse = nan_colname.to_sparse()
self.assertTrue(np.isnan(nan_colname_sparse.columns[0]))


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