Skip to content

fixed conversion to sparse for non-numeric index #11856

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
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
10 changes: 6 additions & 4 deletions pandas/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pandas import compat, lib
from pandas.compat import range
from pandas.core.common import notnull

from pandas._sparse import SparseIndex, BlockIndex, IntIndex
import pandas._sparse as splib
Expand Down Expand Up @@ -122,7 +123,7 @@ class SparseArray(PandasObject, np.ndarray):
fill_value = None

def __new__(cls, data, sparse_index=None, index=None, kind='integer',
fill_value=None, dtype=np.float64, copy=False):
fill_value=None, dtype=None, copy=False):

if index is not None:
if data is None:
Expand All @@ -133,8 +134,6 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer',
values.fill(data)
data = values

if dtype is not None:
dtype = np.dtype(dtype)
is_sparse_array = isinstance(data, SparseArray)
if fill_value is None:
if is_sparse_array:
Expand All @@ -156,6 +155,9 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer',
raise AssertionError("Non array-like type {0} must have"
" the same length as the"
" index".format(type(values)))
if dtype is None:
dtype = values.dtype
dtype = np.dtype(dtype)

# Create array, do *not* copy data by default
if copy:
Expand Down Expand Up @@ -544,7 +546,7 @@ def make_sparse(arr, kind='block', fill_value=nan):
length = len(arr)

if np.isnan(fill_value):
mask = ~np.isnan(arr)
mask = notnull(arr)
else:
mask = arr != fill_value

Expand Down
Loading