Skip to content

Commit 614fb2f

Browse files
committed
fixed conversion to sparse for non-numeric index; todo: display of sparse
1 parent f5b1ebc commit 614fb2f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

pandas/sparse/array.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ class SparseArray(PandasObject, np.ndarray):
122122
sp_index = None
123123
fill_value = None
124124

125-
def __new__(
126-
cls, data, sparse_index=None, index=None, kind='integer', fill_value=None,
127-
dtype= None, copy=False):
125+
def __new__(cls, data, sparse_index=None, index=None, kind='integer',
126+
fill_value=None, dtype=None, copy=False):
128127

129128
if index is not None:
130129
if data is None:

pandas/sparse/tests/test_sparse.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from pandas.compat import range, lrange, StringIO, lrange
2727
from pandas import compat
2828
from pandas.tools.util import cartesian_product
29+
from pandas.tools.pivot import pivot_table
2930

3031
import pandas.sparse.frame as spf
3132

@@ -1449,7 +1450,7 @@ def test_apply_nonuq(self):
14491450
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=['a', 'a', 'c'])
14501451
df = df_orig.to_sparse()
14511452
rs = df.apply(lambda s: s[0], axis=1)
1452-
xp = Series([1, 4, 7], ['a', 'a', 'c'])
1453+
xp = Series([1, 4, 7], ['a', 'a', 'c'])#.astype(float)
14531454
assert_series_equal(rs, xp)
14541455

14551456
# df.T breaks
@@ -1719,6 +1720,18 @@ def test_nan_columnname(self):
17191720
nan_colname_sparse = nan_colname.to_sparse()
17201721
self.assertTrue(np.isnan(nan_colname_sparse.columns[0]))
17211722

1723+
def test_pivot(self):
1724+
# issue #11856
1725+
df = DataFrame( list(zip([3,2,4,1,5,3,2],
1726+
["chr1", "chr1", "chr1", "chr1", "chr2", "chr2", "chr3"],
1727+
[100,100, 100, 200, 1, 3, 1],
1728+
[True, True, True, False, True, False, True],
1729+
[-1,0,1,3, 0,2,1])) ,
1730+
columns = ["counts", "chr", "pos", "strand", "distance"])
1731+
1732+
dfpiv_from_sparse = pivot_table(SparseDataFrame(df), index= [ "chr", "pos"], columns= ["strand","distance"], values= "counts").fillna(0)
1733+
dfpiv = pivot_table(df, index= [ "chr", "pos"], columns= ["strand","distance"], values= "counts").fillna(0)
1734+
assert_frame_equal(dfpiv, dfpiv0)
17221735

17231736
def _dense_series_compare(s, f):
17241737
result = f(s)

0 commit comments

Comments
 (0)