Skip to content

Commit b550acc

Browse files
sinhrksjreback
authored andcommitted
BUG: SparseSeries.shape ignores fill_value
closes #10452 Based on existing tests / text search, no other metohd depends on current ``SparseSeries.shape`` (buggy) behavior. Author: sinhrks <[email protected]> Closes #12742 from sinhrks/sparse_shape and squashes the following commits: 88b5694 [sinhrks] BUG: SparseSeries.shape ignores fill_value
1 parent 896454e commit b550acc

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

doc/source/whatsnew/v0.18.1.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ Bug Fixes
134134

135135

136136

137+
138+
139+
140+
137141
- Bug in ``value_counts`` when ``normalize=True`` and ``dropna=True`` where nulls still contributed to the normalized count (:issue:`12558`)
138142
- Bug in ``Panel.fillna()`` ignoring ``inplace=True`` (:issue:`12633`)
139143
- Bug in ``Series.rename``, ``DataFrame.rename`` and ``DataFrame.rename_axis`` not treating ``Series`` as mappings to relabel (:issue:`12623`).
@@ -147,14 +151,11 @@ Bug Fixes
147151

148152

149153

150-
151-
152-
153154
- Bug in ``CategoricalIndex.get_loc`` returns different result from regular ``Index`` (:issue:`12531`)
154155

155156

156157

157-
158+
- Bug in ``SparseSeries.shape`` ignores ``fill_value`` (:issue:`10452`)
158159

159160

160161

pandas/sparse/series.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ def as_sparse_array(self, kind=None, fill_value=None, copy=False):
282282
def __len__(self):
283283
return len(self.block)
284284

285+
@property
286+
def shape(self):
287+
return self._data.shape
288+
285289
def __unicode__(self):
286290
# currently, unicode is same as repr...fixes infinite loop
287291
series_rep = Series.__unicode__(self)

pandas/sparse/tests/test_sparse.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def test_dense_to_sparse(self):
213213
assert_sp_series_equal(iseries, self.iseries, check_names=False)
214214
self.assertEqual(iseries.name, self.bseries.name)
215215

216+
self.assertEqual(len(series), len(bseries))
217+
self.assertEqual(len(series), len(iseries))
218+
self.assertEqual(series.shape, bseries.shape)
219+
self.assertEqual(series.shape, iseries.shape)
220+
216221
# non-NaN fill value
217222
series = self.zbseries.to_dense()
218223
zbseries = series.to_sparse(kind='block', fill_value=0)
@@ -221,6 +226,11 @@ def test_dense_to_sparse(self):
221226
assert_sp_series_equal(ziseries, self.ziseries, check_names=False)
222227
self.assertEqual(ziseries.name, self.zbseries.name)
223228

229+
self.assertEqual(len(series), len(zbseries))
230+
self.assertEqual(len(series), len(ziseries))
231+
self.assertEqual(series.shape, zbseries.shape)
232+
self.assertEqual(series.shape, ziseries.shape)
233+
224234
def test_to_dense_preserve_name(self):
225235
assert (self.bseries.name is not None)
226236
result = self.bseries.to_dense()
@@ -271,12 +281,18 @@ def _check_const(sparse, name):
271281
sp.sp_values[:5] = 97
272282
self.assertEqual(values[0], 97)
273283

284+
self.assertEqual(len(sp), 20)
285+
self.assertEqual(sp.shape, (20, ))
286+
274287
# but can make it copy!
275288
sp = SparseSeries(values, sparse_index=self.bseries.sp_index,
276289
copy=True)
277290
sp.sp_values[:5] = 100
278291
self.assertEqual(values[0], 97)
279292

293+
self.assertEqual(len(sp), 20)
294+
self.assertEqual(sp.shape, (20, ))
295+
280296
def test_constructor_scalar(self):
281297
data = 5
282298
sp = SparseSeries(data, np.arange(100))
@@ -286,6 +302,8 @@ def test_constructor_scalar(self):
286302

287303
data = np.nan
288304
sp = SparseSeries(data, np.arange(100))
305+
self.assertEqual(len(sp), 100)
306+
self.assertEqual(sp.shape, (100, ))
289307

290308
def test_constructor_ndarray(self):
291309
pass
@@ -294,11 +312,14 @@ def test_constructor_nonnan(self):
294312
arr = [0, 0, 0, nan, nan]
295313
sp_series = SparseSeries(arr, fill_value=0)
296314
assert_equal(sp_series.values.values, arr)
315+
self.assertEqual(len(sp_series), 5)
316+
self.assertEqual(sp_series.shape, (5, ))
297317

298318
# GH 9272
299319
def test_constructor_empty(self):
300320
sp = SparseSeries()
301321
self.assertEqual(len(sp.index), 0)
322+
self.assertEqual(sp.shape, (0, ))
302323

303324
def test_copy_astype(self):
304325
cop = self.bseries.astype(np.float64)
@@ -328,6 +349,18 @@ def test_copy_astype(self):
328349
view.sp_values[:5] = 5
329350
self.assertTrue((self.bseries.sp_values[:5] == 5).all())
330351

352+
def test_shape(self):
353+
# GH 10452
354+
self.assertEqual(self.bseries.shape, (20, ))
355+
self.assertEqual(self.btseries.shape, (20, ))
356+
self.assertEqual(self.iseries.shape, (20, ))
357+
358+
self.assertEqual(self.bseries2.shape, (15, ))
359+
self.assertEqual(self.iseries2.shape, (15, ))
360+
361+
self.assertEqual(self.zbseries2.shape, (15, ))
362+
self.assertEqual(self.ziseries2.shape, (15, ))
363+
331364
def test_astype(self):
332365
self.assertRaises(Exception, self.bseries.astype, np.int64)
333366

@@ -1090,6 +1123,13 @@ def test_dtypes(self):
10901123
expected = Series({'float64': 4})
10911124
assert_series_equal(result, expected)
10921125

1126+
def test_shape(self):
1127+
# GH 10452
1128+
self.assertEqual(self.frame.shape, (10, 4))
1129+
self.assertEqual(self.iframe.shape, (10, 4))
1130+
self.assertEqual(self.zframe.shape, (10, 4))
1131+
self.assertEqual(self.fill_frame.shape, (10, 4))
1132+
10931133
def test_str(self):
10941134
df = DataFrame(np.random.randn(10000, 4))
10951135
df.ix[:9998] = np.nan

0 commit comments

Comments
 (0)