1
1
import operator
2
+ from distutils .version import LooseVersion
2
3
3
4
import numpy as np
4
5
import pandas as pd
19
20
unit_registry = pint .UnitRegistry (force_ndarray = True )
20
21
Quantity = unit_registry .Quantity
21
22
23
+
22
24
pytestmark = [
23
25
pytest .mark .skipif (
24
26
not IS_NEP18_ACTIVE , reason = "NUMPY_EXPERIMENTAL_ARRAY_FUNCTION is not enabled"
@@ -1536,27 +1538,17 @@ def test_missing_value_detection(self, func):
1536
1538
@pytest .mark .parametrize (
1537
1539
"unit,error" ,
1538
1540
(
1539
- pytest .param (
1540
- 1 ,
1541
- DimensionalityError ,
1542
- id = "no_unit" ,
1543
- marks = pytest .mark .xfail (reason = "uses 0 as a replacement" ),
1544
- ),
1541
+ pytest .param (1 , DimensionalityError , id = "no_unit" ),
1545
1542
pytest .param (
1546
1543
unit_registry .dimensionless , DimensionalityError , id = "dimensionless"
1547
1544
),
1548
1545
pytest .param (unit_registry .s , DimensionalityError , id = "incompatible_unit" ),
1549
- pytest .param (
1550
- unit_registry .cm ,
1551
- None ,
1552
- id = "compatible_unit" ,
1553
- marks = pytest .mark .xfail (reason = "converts to fill value's unit" ),
1554
- ),
1546
+ pytest .param (unit_registry .cm , None , id = "compatible_unit" ),
1555
1547
pytest .param (unit_registry .m , None , id = "identical_unit" ),
1556
1548
),
1557
1549
)
1558
1550
def test_missing_value_fillna (self , unit , error ):
1559
- value = 0
1551
+ value = 10
1560
1552
array = (
1561
1553
np .array (
1562
1554
[
@@ -1595,13 +1587,7 @@ def test_missing_value_fillna(self, unit, error):
1595
1587
pytest .param (1 , id = "no_unit" ),
1596
1588
pytest .param (unit_registry .dimensionless , id = "dimensionless" ),
1597
1589
pytest .param (unit_registry .s , id = "incompatible_unit" ),
1598
- pytest .param (
1599
- unit_registry .cm ,
1600
- id = "compatible_unit" ,
1601
- marks = pytest .mark .xfail (
1602
- reason = "checking for identical units does not work properly, yet"
1603
- ),
1604
- ),
1590
+ pytest .param (unit_registry .cm , id = "compatible_unit" ,),
1605
1591
pytest .param (unit_registry .m , id = "identical_unit" ),
1606
1592
),
1607
1593
)
@@ -1612,7 +1598,17 @@ def test_missing_value_fillna(self, unit, error):
1612
1598
pytest .param (True , id = "with_conversion" ),
1613
1599
),
1614
1600
)
1615
- @pytest .mark .parametrize ("func" , (method ("equals" ), method ("identical" )), ids = repr )
1601
+ @pytest .mark .parametrize (
1602
+ "func" ,
1603
+ (
1604
+ method ("equals" ),
1605
+ pytest .param (
1606
+ method ("identical" ),
1607
+ marks = pytest .mark .skip (reason = "behaviour of identical is unclear" ),
1608
+ ),
1609
+ ),
1610
+ ids = repr ,
1611
+ )
1616
1612
def test_comparisons (self , func , unit , convert_data , dtype ):
1617
1613
array = np .linspace (0 , 1 , 9 ).astype (dtype )
1618
1614
quantity1 = array * unit_registry .m
@@ -1762,14 +1758,7 @@ def test_1d_math(self, func, unit, error, dtype):
1762
1758
unit_registry .dimensionless , DimensionalityError , id = "dimensionless"
1763
1759
),
1764
1760
pytest .param (unit_registry .s , DimensionalityError , id = "incompatible_unit" ),
1765
- pytest .param (
1766
- unit_registry .cm ,
1767
- None ,
1768
- id = "compatible_unit" ,
1769
- marks = pytest .mark .xfail (
1770
- reason = "getitem_with_mask converts to the unit of other"
1771
- ),
1772
- ),
1761
+ pytest .param (unit_registry .cm , None , id = "compatible_unit" ),
1773
1762
pytest .param (unit_registry .m , None , id = "identical_unit" ),
1774
1763
),
1775
1764
)
@@ -1853,12 +1842,7 @@ def test_squeeze(self, dtype):
1853
1842
),
1854
1843
method ("reduce" , np .std , "x" ),
1855
1844
method ("round" , 2 ),
1856
- pytest .param (
1857
- method ("shift" , {"x" : - 2 }),
1858
- marks = pytest .mark .xfail (
1859
- reason = "trying to concatenate ndarray to quantity"
1860
- ),
1861
- ),
1845
+ method ("shift" , {"x" : - 2 }),
1862
1846
method ("transpose" , "y" , "x" ),
1863
1847
),
1864
1848
ids = repr ,
@@ -1933,7 +1917,6 @@ def test_unstack(self, dtype):
1933
1917
assert_units_equal (expected , actual )
1934
1918
xr .testing .assert_identical (expected , actual )
1935
1919
1936
- @pytest .mark .xfail (reason = "ignores units" )
1937
1920
@pytest .mark .parametrize (
1938
1921
"unit,error" ,
1939
1922
(
@@ -1948,25 +1931,28 @@ def test_unstack(self, dtype):
1948
1931
)
1949
1932
def test_concat (self , unit , error , dtype ):
1950
1933
array1 = (
1951
- np .linspace (0 , 5 , 3 * 10 ).reshape (3 , 10 ).astype (dtype ) * unit_registry .m
1934
+ np .linspace (0 , 5 , 9 * 10 ).reshape (3 , 6 , 5 ).astype (dtype ) * unit_registry .m
1952
1935
)
1953
- array2 = np .linspace (5 , 10 , 10 * 2 ).reshape (10 , 2 ).astype (dtype ) * unit
1936
+ array2 = np .linspace (5 , 10 , 10 * 3 ).reshape (3 , 2 , 5 ).astype (dtype ) * unit
1954
1937
1955
- variable = xr .Variable (("x" , "y" ), array1 )
1956
- other = xr .Variable (("y" , "z" ), array2 )
1938
+ variable = xr .Variable (("x" , "y" , "z" ), array1 )
1939
+ other = xr .Variable (("x" , " y" , "z" ), array2 )
1957
1940
1958
1941
if error is not None :
1959
1942
with pytest .raises (error ):
1960
- variable . concat (other )
1943
+ xr . Variable . concat ([ variable , other ], dim = "y" )
1961
1944
1962
1945
return
1963
1946
1964
1947
units = extract_units (variable )
1965
1948
expected = attach_units (
1966
- strip_units (variable ).concat (strip_units (convert_units (other , units ))),
1949
+ xr .Variable .concat (
1950
+ [strip_units (variable ), strip_units (convert_units (other , units ))],
1951
+ dim = "y" ,
1952
+ ),
1967
1953
units ,
1968
1954
)
1969
- actual = variable . concat (other )
1955
+ actual = xr . Variable . concat ([ variable , other ], dim = "y" )
1970
1956
1971
1957
assert_units_equal (expected , actual )
1972
1958
xr .testing .assert_identical (expected , actual )
@@ -2036,6 +2022,43 @@ def test_no_conflicts(self, unit, dtype):
2036
2022
2037
2023
assert expected == actual
2038
2024
2025
+ def test_pad (self , dtype ):
2026
+ data = np .arange (4 * 3 * 2 ).reshape (4 , 3 , 2 ).astype (dtype ) * unit_registry .m
2027
+ v = xr .Variable (["x" , "y" , "z" ], data )
2028
+
2029
+ xr_args = [{"x" : (2 , 1 )}, {"y" : (0 , 3 )}, {"x" : (3 , 1 ), "z" : (2 , 0 )}]
2030
+ np_args = [
2031
+ ((2 , 1 ), (0 , 0 ), (0 , 0 )),
2032
+ ((0 , 0 ), (0 , 3 ), (0 , 0 )),
2033
+ ((3 , 1 ), (0 , 0 ), (2 , 0 )),
2034
+ ]
2035
+ for xr_arg , np_arg in zip (xr_args , np_args ):
2036
+ actual = v .pad_with_fill_value (** xr_arg )
2037
+ expected = xr .Variable (
2038
+ v .dims ,
2039
+ np .pad (
2040
+ v .data .astype (float ),
2041
+ np_arg ,
2042
+ mode = "constant" ,
2043
+ constant_values = np .nan ,
2044
+ ),
2045
+ )
2046
+ xr .testing .assert_identical (expected , actual )
2047
+ assert_units_equal (expected , actual )
2048
+ assert isinstance (actual ._data , type (v ._data ))
2049
+
2050
+ # for the boolean array, we pad False
2051
+ data = np .full_like (data , False , dtype = bool ).reshape (4 , 3 , 2 )
2052
+ v = xr .Variable (["x" , "y" , "z" ], data )
2053
+ for xr_arg , np_arg in zip (xr_args , np_args ):
2054
+ actual = v .pad_with_fill_value (fill_value = data .flat [0 ], ** xr_arg )
2055
+ expected = xr .Variable (
2056
+ v .dims ,
2057
+ np .pad (v .data , np_arg , mode = "constant" , constant_values = v .data .flat [0 ]),
2058
+ )
2059
+ xr .testing .assert_identical (actual , expected )
2060
+ assert_units_equal (expected , actual )
2061
+
2039
2062
@pytest .mark .parametrize (
2040
2063
"unit,error" ,
2041
2064
(
@@ -2044,7 +2067,8 @@ def test_no_conflicts(self, unit, dtype):
2044
2067
DimensionalityError ,
2045
2068
id = "no_unit" ,
2046
2069
marks = pytest .mark .xfail (
2047
- reason = "is not treated the same as dimensionless"
2070
+ LooseVersion (pint .__version__ ) < LooseVersion ("0.10.2" ),
2071
+ reason = "bug in pint's implementation of np.pad" ,
2048
2072
),
2049
2073
),
2050
2074
pytest .param (
0 commit comments