Skip to content

Commit a97b9b5

Browse files
committed
BUG: SparseDataFrame.icol return SparseSeries. SparseSeries.from_array return SparseSeries. close #2227, #2229
1 parent bd34795 commit a97b9b5

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pandas 0.9.1
105105
- Fix SparseSeries.__pow__ issue with NA input (#2220)
106106
- Fix icol with integer sequence failure (#2228)
107107
- Fixed resampling tz-aware time series issue (#2245)
108+
- SparseDataFrame.icol was not returning SparseSeries (#2227, #2229)
108109
109110
pandas 0.9.0
110111
============

pandas/core/frame.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class DataFrame(NDFrame):
321321
_auto_consolidate = True
322322
_verbose_info = True
323323
_het_axis = 1
324+
_col_klass = Series
324325

325326
_AXIS_NUMBERS = {
326327
'index': 0,
@@ -1733,7 +1734,8 @@ def icol(self, i):
17331734
return self.ix[:, i]
17341735

17351736
values = self._data.iget(i)
1736-
return Series.from_array(values, index=self.index, name=label)
1737+
return self._col_klass.from_array(values, index=self.index,
1738+
name=label)
17371739

17381740
def _ixs(self, i, axis=0):
17391741
if axis == 0:

pandas/sparse/frame.py

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class SparseDataFrame(DataFrame):
6464
_columns = None
6565
_series = None
6666
_is_mixed_type = False
67+
_col_klass = SparseSeries
6768
ndim = 2
6869

6970
def __init__(self, data=None, index=None, columns=None,

pandas/sparse/series.py

+7
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ def __new__(cls, data, index=None, sparse_index=None, kind='block',
146146
output.name = name
147147
return output
148148

149+
@classmethod
150+
def from_array(cls, arr, index=None, name=None, copy=False):
151+
"""
152+
Simplified alternate constructor
153+
"""
154+
return SparseSeries(arr, index=index, name=name, copy=copy)
155+
149156
def __init__(self, data, index=None, sparse_index=None, kind='block',
150157
fill_value=None, name=None, copy=False):
151158
"""Data structure for labeled, sparse floating point data

pandas/sparse/tests/test_sparse.py

+6
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,12 @@ def test_getitem(self):
901901

902902
self.assertRaises(Exception, sdf.__getitem__, ['a', 'd'])
903903

904+
def test_icol(self):
905+
# #2227
906+
result = self.frame.icol(0)
907+
self.assertTrue(isinstance(result, SparseSeries))
908+
assert_sp_series_equal(result, self.frame['A'])
909+
904910
def test_set_value(self):
905911
res = self.frame.set_value('foobar', 'B', 1.5)
906912
self.assert_(res is not self.frame)

0 commit comments

Comments
 (0)