Skip to content

Commit 3818d01

Browse files
committed
add tests for align in comparison ops (we don't have align yet, so raise!)
1 parent 32b3732 commit 3818d01

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pandas/core/panel.py

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
_get_combined_index)
1414
from pandas.core.indexing import _NDFrameIndexer, _maybe_droplevels
1515
from pandas.core.internals import BlockManager, make_block, form_blocks
16+
from pandas.core.series import Series
1617
from pandas.core.frame import DataFrame
1718
from pandas.core.generic import NDFrame
1819
from pandas.util import py3compat
@@ -152,6 +153,8 @@ def na_op(x, y):
152153
def f(self, other):
153154
if isinstance(other, self._constructor):
154155
return self._compare_constructor(other, func)
156+
elif isinstance(other, (self._constructor_sliced, DataFrame, Series)):
157+
raise Exception("input needs alignment for this object [%s]" % self._constructor)
155158
else:
156159
return self._combine_const(other, na_op)
157160

@@ -443,6 +446,10 @@ def _indexed_same(self, other):
443446
return all([ getattr(self,a).equals(getattr(other,a)) for a in self._AXIS_ORDERS ])
444447

445448
def _compare_constructor(self, other, func):
449+
if not self._indexed_same(other):
450+
raise Exception('Can only compare identically-labeled '
451+
'same type objects')
452+
446453
new_data = {}
447454
for col in getattr(self,self._info_axis):
448455
new_data[col] = func(self[col], other[col])

pandas/tests/test_panel.py

+12
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,23 @@ def test_comparisons(self):
657657
p1 = tm.makePanel()
658658
p2 = tm.makePanel()
659659

660+
tp = p1.reindex(items = p1.items + ['foo'])
661+
df = p1[p1.items[0]]
662+
660663
def test_comp(func):
664+
665+
# versus same index
661666
result = func(p1, p2)
662667
self.assert_(np.array_equal(result.values,
663668
func(p1.values, p2.values)))
664669

670+
# versus non-indexed same objs
671+
self.assertRaises(Exception, func, p1, tp)
672+
673+
# versus different objs
674+
self.assertRaises(Exception, func, p1, df)
675+
676+
# versus scalar
665677
result3 = func(self.panel, 0)
666678
self.assert_(np.array_equal(result3.values,
667679
func(self.panel.values, 0)))

0 commit comments

Comments
 (0)