@@ -1550,22 +1550,70 @@ def test_groupby(self):
1550
1550
tm .assert_dict_equal (groups , exp )
1551
1551
1552
1552
def test_equals_op (self ):
1553
- # For issue #9785
1553
+ # GH9947
1554
1554
index_a = Index (['foo' , 'bar' , 'baz' ])
1555
1555
index_b = Index (['foo' , 'bar' , 'baz' , 'qux' ])
1556
- # Testing Numpy Results Equivelent
1557
- assert_array_equal (
1558
- index_a .equals (index_a ),
1559
- index_a == index_a
1560
- )
1561
- assert_array_equal (
1562
- index_a .equals (index_b ),
1563
- index_a == index_b ,
1564
- )
1565
- assert_array_equal (
1566
- index_b .equals (index_a ),
1567
- index_b == index_a ,
1568
- )
1556
+ index_c = Index (['foo' , 'bar' , 'qux' ])
1557
+ index_d = Index (['foo' ])
1558
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1559
+ index_a == index_b
1560
+ assert_array_equal (index_a == index_a , np .array ([True , True , True ]))
1561
+ assert_array_equal (index_a == index_c , np .array ([True , True , False ]))
1562
+
1563
+ # test comparisons with numpy arrays
1564
+ array_a = np .array (['foo' , 'bar' , 'baz' ])
1565
+ array_b = np .array (['foo' , 'bar' , 'baz' , 'qux' ])
1566
+ array_c = np .array (['foo' , 'bar' , 'qux' ])
1567
+ array_d = np .array (['foo' ])
1568
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1569
+ index_a == array_b
1570
+ assert_array_equal (index_a == array_a , np .array ([True , True , True ]))
1571
+ assert_array_equal (index_a == array_c , np .array ([True , True , False ]))
1572
+
1573
+ # test comparisons with Series
1574
+ series_a = Series (['foo' , 'bar' , 'baz' ])
1575
+ series_b = Series (['foo' , 'bar' , 'baz' , 'qux' ])
1576
+ series_c = Series (['foo' , 'bar' , 'qux' ])
1577
+ series_d = Series (['foo' ])
1578
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1579
+ index_a == series_b
1580
+ assert_array_equal (index_a == series_a , np .array ([True , True , True ]))
1581
+ assert_array_equal (index_a == series_c , np .array ([True , True , False ]))
1582
+
1583
+ # cases where length is 1 for one of them
1584
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1585
+ index_a == index_d
1586
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1587
+ index_a == series_d
1588
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1589
+ index_a == array_d
1590
+ with tm .assertRaisesRegexp (ValueError , "Series lengths must match" ):
1591
+ series_a == series_d
1592
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1593
+ series_a == array_d
1594
+
1595
+ # comparing with scalar should broadcast
1596
+ assert_array_equal (index_a == 'foo' , np .array ([True , False , False ]))
1597
+ assert_array_equal (series_a == 'foo' , np .array ([True , False , False ]))
1598
+ assert_array_equal (array_a == 'foo' , np .array ([True , False , False ]))
1599
+
1600
+ # GH9785
1601
+ # test comparisons of multiindex
1602
+ from pandas .compat import StringIO
1603
+ df = pd .read_csv (StringIO ('a,b,c\n 1,2,3\n 4,5,6' ), index_col = [0 , 1 ])
1604
+ assert_array_equal (df .index == df .index , np .array ([True , True ]))
1605
+
1606
+ mi1 = MultiIndex .from_tuples ([(1 , 2 ), (4 , 5 )])
1607
+ assert_array_equal (df .index == mi1 , np .array ([True , True ]))
1608
+ mi2 = MultiIndex .from_tuples ([(1 , 2 ), (4 , 6 )])
1609
+ assert_array_equal (df .index == mi2 , np .array ([True , False ]))
1610
+ mi3 = MultiIndex .from_tuples ([(1 , 2 ), (4 , 5 ), (8 , 9 )])
1611
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1612
+ df .index == mi3
1613
+ with tm .assertRaisesRegexp (ValueError , "Lengths must match" ):
1614
+ df .index == index_a
1615
+ assert_array_equal (index_a == mi3 , np .array ([False , False , False ]))
1616
+
1569
1617
1570
1618
class TestCategoricalIndex (Base , tm .TestCase ):
1571
1619
_holder = CategoricalIndex
@@ -4815,47 +4863,9 @@ def test_index_name_retained(self):
4815
4863
tm .assert_frame_equal (result , df_expected )
4816
4864
4817
4865
def test_equals_operator (self ):
4818
- # For issue #9785
4866
+ # GH9785
4819
4867
self .assertTrue ((self .index == self .index ).all ())
4820
4868
4821
- def test_index_compare (self ):
4822
- # For issue #9785
4823
- index_unequal = Index (['foo' , 'bar' , 'baz' ])
4824
- index_equal = Index ([
4825
- ('foo' , 'one' ), ('foo' , 'two' ), ('bar' , 'one' ),
4826
- ('baz' , 'two' ), ('qux' , 'one' ), ('qux' , 'two' )
4827
- ], tupleize_cols = False )
4828
- # Testing Numpy Results Equivelent
4829
- assert_array_equal (
4830
- index_unequal .equals (self .index ),
4831
- index_unequal == self .index ,
4832
- err_msg = 'Index compared with MultiIndex failed' ,
4833
- )
4834
- assert_array_equal (
4835
- self .index .equals (index_unequal ),
4836
- self .index == index_unequal ,
4837
- err_msg = 'MultiIndex compared with Index failed' ,
4838
- )
4839
- assert_array_equal (
4840
- self .index .equals (index_equal ),
4841
- self .index == index_equal ,
4842
- err_msg = 'MultiIndex compared with Similar Index failed' ,
4843
- )
4844
- assert_array_equal (
4845
- index_equal .equals (self .index ),
4846
- index_equal == self .index ,
4847
- err_msg = 'Index compared with Similar MultiIndex failed' ,
4848
- )
4849
- # Testing that the result is true for the index_equal case
4850
- self .assertTrue (
4851
- (self .index == index_equal ).all (),
4852
- msg = 'Assert Index compared with Similar MultiIndex match'
4853
- )
4854
- self .assertTrue (
4855
- (index_equal == self .index ).all (),
4856
- msg = 'Assert MultiIndex compared with Similar Index match'
4857
- )
4858
-
4859
4869
4860
4870
def test_get_combined_index ():
4861
4871
from pandas .core .index import _get_combined_index
0 commit comments