@@ -993,6 +993,15 @@ def test_stack_unstack(self):
993
993
actual = orig .stack (z = ['x' , 'y' ]).unstack ('z' )
994
994
self .assertDataArrayIdentical (orig , actual )
995
995
996
+ def test_stack_unstack_decreasing_coordinate (self ):
997
+ # regression test for GH980
998
+ orig = DataArray (np .random .rand (3 , 4 ), dims = ('y' , 'x' ),
999
+ coords = {'x' : np .arange (4 ),
1000
+ 'y' : np .arange (3 , 0 , - 1 )})
1001
+ stacked = orig .stack (allpoints = ['y' , 'x' ])
1002
+ actual = stacked .unstack ('allpoints' )
1003
+ self .assertDataArrayIdentical (orig , actual )
1004
+
996
1005
def test_unstack_pandas_consistency (self ):
997
1006
df = pd .DataFrame ({'foo' : range (3 ),
998
1007
'x' : ['a' , 'b' , 'b' ],
@@ -1628,15 +1637,15 @@ def test_align_dtype(self):
1628
1637
def test_align_copy (self ):
1629
1638
x = DataArray ([1 , 2 , 3 ], coords = [('a' , [1 , 2 , 3 ])])
1630
1639
y = DataArray ([1 , 2 ], coords = [('a' , [3 , 1 ])])
1631
-
1640
+
1632
1641
expected_x2 = x
1633
1642
expected_y2 = DataArray ([2 , np .nan , 1 ], coords = [('a' , [1 , 2 , 3 ])])
1634
1643
1635
1644
x2 , y2 = align (x , y , join = 'outer' , copy = False )
1636
1645
self .assertDataArrayIdentical (expected_x2 , x2 )
1637
1646
self .assertDataArrayIdentical (expected_y2 , y2 )
1638
1647
assert source_ndarray (x2 .data ) is source_ndarray (x .data )
1639
-
1648
+
1640
1649
x2 , y2 = align (x , y , join = 'outer' , copy = True )
1641
1650
self .assertDataArrayIdentical (expected_x2 , x2 )
1642
1651
self .assertDataArrayIdentical (expected_y2 , y2 )
@@ -1647,23 +1656,28 @@ def test_align_copy(self):
1647
1656
x2 , = align (x , copy = False )
1648
1657
self .assertDataArrayIdentical (x , x2 )
1649
1658
assert source_ndarray (x2 .data ) is source_ndarray (x .data )
1650
-
1659
+
1651
1660
x2 , = align (x , copy = True )
1652
1661
self .assertDataArrayIdentical (x , x2 )
1653
1662
assert source_ndarray (x2 .data ) is not source_ndarray (x .data )
1654
1663
1655
1664
def test_align_exclude (self ):
1656
- x = DataArray ([[1 , 2 ], [3 , 4 ]], coords = [('a' , [- 1 , - 2 ]), ('b' , [3 , 4 ])])
1657
- y = DataArray ([[1 , 2 ], [3 , 4 ]], coords = [('a' , [- 1 , 20 ]), ('b' , [5 , 6 ])])
1665
+ x = DataArray ([[1 , 2 ], [3 , 4 ]],
1666
+ coords = [('a' , [- 1 , - 2 ]), ('b' , [3 , 4 ])])
1667
+ y = DataArray ([[1 , 2 ], [3 , 4 ]],
1668
+ coords = [('a' , [- 1 , 20 ]), ('b' , [5 , 6 ])])
1658
1669
z = DataArray ([1 ], dims = ['a' ], coords = {'a' : [20 ], 'b' : 7 })
1659
-
1670
+
1660
1671
x2 , y2 , z2 = align (x , y , z , join = 'outer' , exclude = ['b' ])
1661
- expected_x2 = DataArray ([[3 , 4 ], [1 , 2 ], [np .nan , np .nan ]], coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1662
- expected_y2 = DataArray ([[np .nan , np .nan ], [1 , 2 ], [3 , 4 ]], coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [5 , 6 ])])
1663
- expected_z2 = DataArray ([np .nan , np .nan , 1 ], dims = ['a' ], coords = {'a' : [- 2 , - 1 , 20 ], 'b' : 7 })
1672
+ expected_x2 = DataArray ([[3 , 4 ], [1 , 2 ], [np .nan , np .nan ]],
1673
+ coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1674
+ expected_y2 = DataArray ([[np .nan , np .nan ], [1 , 2 ], [3 , 4 ]],
1675
+ coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [5 , 6 ])])
1676
+ expected_z2 = DataArray ([np .nan , np .nan , 1 ], dims = ['a' ],
1677
+ coords = {'a' : [- 2 , - 1 , 20 ], 'b' : 7 })
1664
1678
self .assertDataArrayIdentical (expected_x2 , x2 )
1665
1679
self .assertDataArrayIdentical (expected_y2 , y2 )
1666
- self .assertDataArrayIdentical (expected_z2 , z2 )
1680
+ self .assertDataArrayIdentical (expected_z2 , z2 )
1667
1681
1668
1682
def test_align_indexes (self ):
1669
1683
x = DataArray ([1 , 2 , 3 ], coords = [('a' , [- 1 , 10 , - 2 ])])
@@ -1676,7 +1690,8 @@ def test_align_indexes(self):
1676
1690
self .assertDataArrayIdentical (expected_y2 , y2 )
1677
1691
1678
1692
x2 , = align (x , join = 'outer' , indexes = {'a' : [- 2 , 7 , 10 , - 1 ]})
1679
- expected_x2 = DataArray ([3 , np .nan , 2 , 1 ], coords = [('a' , [- 2 , 7 , 10 , - 1 ])])
1693
+ expected_x2 = DataArray ([3 , np .nan , 2 , 1 ],
1694
+ coords = [('a' , [- 2 , 7 , 10 , - 1 ])])
1680
1695
self .assertDataArrayIdentical (expected_x2 , x2 )
1681
1696
1682
1697
def test_broadcast_arrays (self ):
@@ -1699,10 +1714,13 @@ def test_broadcast_arrays(self):
1699
1714
1700
1715
def test_broadcast_arrays_misaligned (self ):
1701
1716
# broadcast on misaligned coords must auto-align
1702
- x = DataArray ([[1 , 2 ], [3 , 4 ]], coords = [('a' , [- 1 , - 2 ]), ('b' , [3 , 4 ])])
1717
+ x = DataArray ([[1 , 2 ], [3 , 4 ]],
1718
+ coords = [('a' , [- 1 , - 2 ]), ('b' , [3 , 4 ])])
1703
1719
y = DataArray ([1 , 2 ], coords = [('a' , [- 1 , 20 ])])
1704
- expected_x2 = DataArray ([[3 , 4 ], [1 , 2 ], [np .nan , np .nan ]], coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1705
- expected_y2 = DataArray ([[np .nan , np .nan ], [1 , 1 ], [2 , 2 ]], coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1720
+ expected_x2 = DataArray ([[3 , 4 ], [1 , 2 ], [np .nan , np .nan ]],
1721
+ coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1722
+ expected_y2 = DataArray ([[np .nan , np .nan ], [1 , 1 ], [2 , 2 ]],
1723
+ coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1706
1724
x2 , y2 = broadcast (x , y )
1707
1725
self .assertDataArrayIdentical (expected_x2 , x2 )
1708
1726
self .assertDataArrayIdentical (expected_y2 , y2 )
@@ -1718,21 +1736,24 @@ def test_broadcast_arrays_nocopy(self):
1718
1736
self .assertDataArrayIdentical (expected_x2 , x2 )
1719
1737
self .assertDataArrayIdentical (expected_y2 , y2 )
1720
1738
assert source_ndarray (x2 .data ) is source_ndarray (x .data )
1721
-
1739
+
1722
1740
# single-element broadcast (trivial case)
1723
1741
x2 , = broadcast (x )
1724
1742
self .assertDataArrayIdentical (x , x2 )
1725
1743
assert source_ndarray (x2 .data ) is source_ndarray (x .data )
1726
1744
1727
1745
def test_broadcast_arrays_exclude (self ):
1728
- x = DataArray ([[1 , 2 ], [3 , 4 ]], coords = [('a' , [- 1 , - 2 ]), ('b' , [3 , 4 ])])
1746
+ x = DataArray ([[1 , 2 ], [3 , 4 ]],
1747
+ coords = [('a' , [- 1 , - 2 ]), ('b' , [3 , 4 ])])
1729
1748
y = DataArray ([1 , 2 ], coords = [('a' , [- 1 , 20 ])])
1730
1749
z = DataArray (5 , coords = {'b' : 5 })
1731
-
1750
+
1732
1751
x2 , y2 , z2 = broadcast (x , y , z , exclude = ['b' ])
1733
- expected_x2 = DataArray ([[3 , 4 ], [1 , 2 ], [np .nan , np .nan ]], coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1752
+ expected_x2 = DataArray ([[3 , 4 ], [1 , 2 ], [np .nan , np .nan ]],
1753
+ coords = [('a' , [- 2 , - 1 , 20 ]), ('b' , [3 , 4 ])])
1734
1754
expected_y2 = DataArray ([np .nan , 1 , 2 ], coords = [('a' , [- 2 , - 1 , 20 ])])
1735
- expected_z2 = DataArray ([5 , 5 , 5 ], dims = ['a' ], coords = {'a' : [- 2 , - 1 , 20 ], 'b' : 5 })
1755
+ expected_z2 = DataArray ([5 , 5 , 5 ], dims = ['a' ],
1756
+ coords = {'a' : [- 2 , - 1 , 20 ], 'b' : 5 })
1736
1757
self .assertDataArrayIdentical (expected_x2 , x2 )
1737
1758
self .assertDataArrayIdentical (expected_y2 , y2 )
1738
1759
self .assertDataArrayIdentical (expected_z2 , z2 )
0 commit comments