Skip to content

Commit 8a30a5d

Browse files
committed
add tests for align in comparison ops (we don't have align yet, so raise!)
1 parent b28f8b4 commit 8a30a5d

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
@@ -664,11 +664,23 @@ def test_comparisons(self):
664664
p1 = tm.makePanel()
665665
p2 = tm.makePanel()
666666

667+
tp = p1.reindex(items = p1.items + ['foo'])
668+
df = p1[p1.items[0]]
669+
667670
def test_comp(func):
671+
672+
# versus same index
668673
result = func(p1, p2)
669674
self.assert_(np.array_equal(result.values,
670675
func(p1.values, p2.values)))
671676

677+
# versus non-indexed same objs
678+
self.assertRaises(Exception, func, p1, tp)
679+
680+
# versus different objs
681+
self.assertRaises(Exception, func, p1, df)
682+
683+
# versus scalar
672684
result3 = func(self.panel, 0)
673685
self.assert_(np.array_equal(result3.values,
674686
func(self.panel.values, 0)))

0 commit comments

Comments
 (0)