Skip to content

Commit 88dbb09

Browse files
committed
BUG: Defined .size attribute across NDFrame objects to provide compat with numpy >= 1.9.1; buggy with np.array_split (GH8846)
1 parent 08105ab commit 88dbb09

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/source/whatsnew/v0.15.2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Bug Fixes
7575

7676

7777

78-
78+
- Defined ``.size`` attribute across ``NDFrame`` objects to provide compat with numpy >= 1.9.1; buggy with ``np.array_split`` (:issue:`8846`)
7979

8080

8181
- Skip testing of histogram plots for matplotlib <= 1.2 (:issue:`8648`).

pandas/core/generic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ def ndim(self):
381381
"Number of axes / array dimensions"
382382
return self._data.ndim
383383

384+
@property
385+
def size(self):
386+
"number of elements in the NDFrame"
387+
return np.prod(self.shape)
388+
384389
def _expand_axes(self, key):
385390
new_axes = []
386391
for k, ax in zip(key, self.axes):

pandas/tests/test_generic.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,21 @@ def test_head_tail(self):
352352
self._compare(o.head(-3), o.head(7))
353353
self._compare(o.tail(-3), o.tail(7))
354354

355+
356+
def test_size_compat(self):
357+
# GH8846
358+
# size property should be defined
359+
360+
o = self._construct(shape=10)
361+
self.assertTrue(o.size == np.prod(o.shape))
362+
self.assertTrue(o.size == 10**len(o.axes))
363+
364+
def test_split_compat(self):
365+
# xref GH8846
366+
o = self._construct(shape=10)
367+
self.assertTrue(len(np.array_split(o,5)) == 5)
368+
self.assertTrue(len(np.array_split(o,2)) == 2)
369+
355370
class TestSeries(tm.TestCase, Generic):
356371
_typ = Series
357372
_comparator = lambda self, x, y: assert_series_equal(x,y)
@@ -1422,8 +1437,8 @@ def test_equals(self):
14221437
self.assertTrue(a.equals(c))
14231438
self.assertTrue(a.equals(d))
14241439
self.assertFalse(a.equals(e))
1425-
self.assertTrue(e.equals(f))
1426-
1440+
self.assertTrue(e.equals(f))
1441+
14271442
def test_describe_raises(self):
14281443
with tm.assertRaises(NotImplementedError):
14291444
tm.makePanel().describe()

0 commit comments

Comments
 (0)