60
60
"setting an item on a masked array which has a shared mask will not copy" )
61
61
62
62
63
+ # For parametrized numeric testing
64
+ num_dts = [np .dtype (dt_ ) for dt_ in '?bhilqBHILQefdgFD' ]
65
+ num_ids = [dt_ .char for dt_ in num_dts ]
66
+
67
+
63
68
class TestMaskedArray (object ):
64
69
# Base test class for MaskedArrays.
65
70
@@ -1415,23 +1420,34 @@ def test_eq_on_structured(self):
1415
1420
# Test the equality of structured arrays
1416
1421
ndtype = [('A' , int ), ('B' , int )]
1417
1422
a = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (0 , 0 )], dtype = ndtype )
1423
+
1418
1424
test = (a == a )
1419
1425
assert_equal (test .data , [True , True ])
1420
1426
assert_equal (test .mask , [False , False ])
1427
+ assert_ (test .fill_value == True )
1428
+
1421
1429
test = (a == a [0 ])
1422
1430
assert_equal (test .data , [True , False ])
1423
1431
assert_equal (test .mask , [False , False ])
1432
+ assert_ (test .fill_value == True )
1433
+
1424
1434
b = array ([(1 , 1 ), (2 , 2 )], mask = [(1 , 0 ), (0 , 0 )], dtype = ndtype )
1425
1435
test = (a == b )
1426
1436
assert_equal (test .data , [False , True ])
1427
1437
assert_equal (test .mask , [True , False ])
1438
+ assert_ (test .fill_value == True )
1439
+
1428
1440
test = (a [0 ] == b )
1429
1441
assert_equal (test .data , [False , False ])
1430
1442
assert_equal (test .mask , [True , False ])
1443
+ assert_ (test .fill_value == True )
1444
+
1431
1445
b = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (1 , 0 )], dtype = ndtype )
1432
1446
test = (a == b )
1433
1447
assert_equal (test .data , [True , True ])
1434
1448
assert_equal (test .mask , [False , False ])
1449
+ assert_ (test .fill_value == True )
1450
+
1435
1451
# complicated dtype, 2-dimensional array.
1436
1452
ndtype = [('A' , int ), ('B' , [('BA' , int ), ('BB' , int )])]
1437
1453
a = array ([[(1 , (1 , 1 )), (2 , (2 , 2 ))],
@@ -1441,28 +1457,40 @@ def test_eq_on_structured(self):
1441
1457
test = (a [0 , 0 ] == a )
1442
1458
assert_equal (test .data , [[True , False ], [False , False ]])
1443
1459
assert_equal (test .mask , [[False , False ], [False , True ]])
1460
+ assert_ (test .fill_value == True )
1444
1461
1445
1462
def test_ne_on_structured (self ):
1446
1463
# Test the equality of structured arrays
1447
1464
ndtype = [('A' , int ), ('B' , int )]
1448
1465
a = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (0 , 0 )], dtype = ndtype )
1466
+
1449
1467
test = (a != a )
1450
1468
assert_equal (test .data , [False , False ])
1451
1469
assert_equal (test .mask , [False , False ])
1470
+ assert_ (test .fill_value == True )
1471
+
1452
1472
test = (a != a [0 ])
1453
1473
assert_equal (test .data , [False , True ])
1454
1474
assert_equal (test .mask , [False , False ])
1475
+ assert_ (test .fill_value == True )
1476
+
1455
1477
b = array ([(1 , 1 ), (2 , 2 )], mask = [(1 , 0 ), (0 , 0 )], dtype = ndtype )
1456
1478
test = (a != b )
1457
1479
assert_equal (test .data , [True , False ])
1458
1480
assert_equal (test .mask , [True , False ])
1481
+ assert_ (test .fill_value == True )
1482
+
1459
1483
test = (a [0 ] != b )
1460
1484
assert_equal (test .data , [True , True ])
1461
1485
assert_equal (test .mask , [True , False ])
1486
+ assert_ (test .fill_value == True )
1487
+
1462
1488
b = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (1 , 0 )], dtype = ndtype )
1463
1489
test = (a != b )
1464
1490
assert_equal (test .data , [False , False ])
1465
1491
assert_equal (test .mask , [False , False ])
1492
+ assert_ (test .fill_value == True )
1493
+
1466
1494
# complicated dtype, 2-dimensional array.
1467
1495
ndtype = [('A' , int ), ('B' , [('BA' , int ), ('BB' , int )])]
1468
1496
a = array ([[(1 , (1 , 1 )), (2 , (2 , 2 ))],
@@ -1472,6 +1500,7 @@ def test_ne_on_structured(self):
1472
1500
test = (a [0 , 0 ] != a )
1473
1501
assert_equal (test .data , [[False , True ], [True , True ]])
1474
1502
assert_equal (test .mask , [[False , False ], [False , True ]])
1503
+ assert_ (test .fill_value == True )
1475
1504
1476
1505
def test_eq_ne_structured_extra (self ):
1477
1506
# ensure simple examples are symmetric and make sense.
@@ -1507,6 +1536,120 @@ def test_eq_ne_structured_extra(self):
1507
1536
el_by_el = [m1 [name ] != m2 [name ] for name in dt .names ]
1508
1537
assert_equal (array (el_by_el , dtype = bool ).any (), ne_expected )
1509
1538
1539
+ @pytest .mark .parametrize ('dt' , ['S' , 'U' ])
1540
+ @pytest .mark .parametrize ('fill' , [None , 'A' ])
1541
+ def test_eq_for_strings (self , dt , fill ):
1542
+ # Test the equality of structured arrays
1543
+ a = array (['a' , 'b' ], dtype = dt , mask = [0 , 1 ], fill_value = fill )
1544
+
1545
+ test = (a == a )
1546
+ assert_equal (test .data , [True , True ])
1547
+ assert_equal (test .mask , [False , True ])
1548
+ assert_ (test .fill_value == True )
1549
+
1550
+ test = (a == a [0 ])
1551
+ assert_equal (test .data , [True , False ])
1552
+ assert_equal (test .mask , [False , True ])
1553
+ assert_ (test .fill_value == True )
1554
+
1555
+ b = array (['a' , 'b' ], dtype = dt , mask = [1 , 0 ], fill_value = fill )
1556
+ test = (a == b )
1557
+ assert_equal (test .data , [False , False ])
1558
+ assert_equal (test .mask , [True , True ])
1559
+ assert_ (test .fill_value == True )
1560
+
1561
+ # test = (a[0] == b) # doesn't work in Python2
1562
+ test = (b == a [0 ])
1563
+ assert_equal (test .data , [False , False ])
1564
+ assert_equal (test .mask , [True , False ])
1565
+ assert_ (test .fill_value == True )
1566
+
1567
+ @pytest .mark .parametrize ('dt' , ['S' , 'U' ])
1568
+ @pytest .mark .parametrize ('fill' , [None , 'A' ])
1569
+ def test_ne_for_strings (self , dt , fill ):
1570
+ # Test the equality of structured arrays
1571
+ a = array (['a' , 'b' ], dtype = dt , mask = [0 , 1 ], fill_value = fill )
1572
+
1573
+ test = (a != a )
1574
+ assert_equal (test .data , [False , False ])
1575
+ assert_equal (test .mask , [False , True ])
1576
+ assert_ (test .fill_value == True )
1577
+
1578
+ test = (a != a [0 ])
1579
+ assert_equal (test .data , [False , True ])
1580
+ assert_equal (test .mask , [False , True ])
1581
+ assert_ (test .fill_value == True )
1582
+
1583
+ b = array (['a' , 'b' ], dtype = dt , mask = [1 , 0 ], fill_value = fill )
1584
+ test = (a != b )
1585
+ assert_equal (test .data , [True , True ])
1586
+ assert_equal (test .mask , [True , True ])
1587
+ assert_ (test .fill_value == True )
1588
+
1589
+ # test = (a[0] != b) # doesn't work in Python2
1590
+ test = (b != a [0 ])
1591
+ assert_equal (test .data , [True , True ])
1592
+ assert_equal (test .mask , [True , False ])
1593
+ assert_ (test .fill_value == True )
1594
+
1595
+ @pytest .mark .parametrize ('dt1' , num_dts , ids = num_ids )
1596
+ @pytest .mark .parametrize ('dt2' , num_dts , ids = num_ids )
1597
+ @pytest .mark .parametrize ('fill' , [None , 1 ])
1598
+ def test_eq_for_numeric (self , dt1 , dt2 , fill ):
1599
+ # Test the equality of structured arrays
1600
+ a = array ([0 , 1 ], dtype = dt1 , mask = [0 , 1 ], fill_value = fill )
1601
+
1602
+ test = (a == a )
1603
+ assert_equal (test .data , [True , True ])
1604
+ assert_equal (test .mask , [False , True ])
1605
+ assert_ (test .fill_value == True )
1606
+
1607
+ test = (a == a [0 ])
1608
+ assert_equal (test .data , [True , False ])
1609
+ assert_equal (test .mask , [False , True ])
1610
+ assert_ (test .fill_value == True )
1611
+
1612
+ b = array ([0 , 1 ], dtype = dt2 , mask = [1 , 0 ], fill_value = fill )
1613
+ test = (a == b )
1614
+ assert_equal (test .data , [False , False ])
1615
+ assert_equal (test .mask , [True , True ])
1616
+ assert_ (test .fill_value == True )
1617
+
1618
+ # test = (a[0] == b) # doesn't work in Python2
1619
+ test = (b == a [0 ])
1620
+ assert_equal (test .data , [False , False ])
1621
+ assert_equal (test .mask , [True , False ])
1622
+ assert_ (test .fill_value == True )
1623
+
1624
+ @pytest .mark .parametrize ('dt1' , num_dts , ids = num_ids )
1625
+ @pytest .mark .parametrize ('dt2' , num_dts , ids = num_ids )
1626
+ @pytest .mark .parametrize ('fill' , [None , 1 ])
1627
+ def test_ne_for_numeric (self , dt1 , dt2 , fill ):
1628
+ # Test the equality of structured arrays
1629
+ a = array ([0 , 1 ], dtype = dt1 , mask = [0 , 1 ], fill_value = fill )
1630
+
1631
+ test = (a != a )
1632
+ assert_equal (test .data , [False , False ])
1633
+ assert_equal (test .mask , [False , True ])
1634
+ assert_ (test .fill_value == True )
1635
+
1636
+ test = (a != a [0 ])
1637
+ assert_equal (test .data , [False , True ])
1638
+ assert_equal (test .mask , [False , True ])
1639
+ assert_ (test .fill_value == True )
1640
+
1641
+ b = array ([0 , 1 ], dtype = dt2 , mask = [1 , 0 ], fill_value = fill )
1642
+ test = (a != b )
1643
+ assert_equal (test .data , [True , True ])
1644
+ assert_equal (test .mask , [True , True ])
1645
+ assert_ (test .fill_value == True )
1646
+
1647
+ # test = (a[0] != b) # doesn't work in Python2
1648
+ test = (b != a [0 ])
1649
+ assert_equal (test .data , [True , True ])
1650
+ assert_equal (test .mask , [True , False ])
1651
+ assert_ (test .fill_value == True )
1652
+
1510
1653
def test_eq_with_None (self ):
1511
1654
# Really, comparisons with None should not be done, but check them
1512
1655
# anyway. Note that pep8 will flag these tests.
@@ -5017,11 +5160,8 @@ def test_astype_mask_ordering():
5017
5160
assert_ (x_f2 .mask .flags .f_contiguous )
5018
5161
5019
5162
5020
- dts = [np .dtype (dt_ ) for dt_ in '?bhilqBHILQefdgFD' ]
5021
- ids = [dt_ .char for dt_ in dts ]
5022
-
5023
- @pytest .mark .parametrize ('dt1' , dts , ids = ids )
5024
- @pytest .mark .parametrize ('dt2' , dts , ids = ids )
5163
+ @pytest .mark .parametrize ('dt1' , num_dts , ids = num_ids )
5164
+ @pytest .mark .parametrize ('dt2' , num_dts , ids = num_ids )
5025
5165
@pytest .mark .filterwarnings ('ignore::numpy.ComplexWarning' )
5026
5166
def test_astype_basic (dt1 , dt2 ):
5027
5167
# See gh-12070
0 commit comments