34
34
source_ndarray ,
35
35
)
36
36
37
+ from .test_dask import raise_if_dask_computes
38
+
37
39
38
40
class TestDataArray :
39
41
@pytest .fixture (autouse = True )
@@ -4524,11 +4526,21 @@ def test_argmax(self, x, minindex, maxindex, nanindex):
4524
4526
4525
4527
assert_identical (result2 , expected2 )
4526
4528
4527
- def test_idxmin (self , x , minindex , maxindex , nanindex ):
4528
- ar0 = xr .DataArray (
4529
+ @pytest .mark .parametrize ("use_dask" , [True , False ])
4530
+ def test_idxmin (self , x , minindex , maxindex , nanindex , use_dask ):
4531
+ if use_dask and not has_dask :
4532
+ pytest .skip ("requires dask" )
4533
+ if use_dask and x .dtype .kind == "M" :
4534
+ pytest .xfail ("dask operation 'argmin' breaks when dtype is datetime64 (M)" )
4535
+ ar0_raw = xr .DataArray (
4529
4536
x , dims = ["x" ], coords = {"x" : np .arange (x .size ) * 4 }, attrs = self .attrs
4530
4537
)
4531
4538
4539
+ if use_dask :
4540
+ ar0 = ar0_raw .chunk ({})
4541
+ else :
4542
+ ar0 = ar0_raw
4543
+
4532
4544
# dim doesn't exist
4533
4545
with pytest .raises (KeyError ):
4534
4546
ar0 .idxmin (dim = "spam" )
@@ -4620,11 +4632,21 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
4620
4632
result7 = ar0 .idxmin (fill_value = - 1j )
4621
4633
assert_identical (result7 , expected7 )
4622
4634
4623
- def test_idxmax (self , x , minindex , maxindex , nanindex ):
4624
- ar0 = xr .DataArray (
4635
+ @pytest .mark .parametrize ("use_dask" , [True , False ])
4636
+ def test_idxmax (self , x , minindex , maxindex , nanindex , use_dask ):
4637
+ if use_dask and not has_dask :
4638
+ pytest .skip ("requires dask" )
4639
+ if use_dask and x .dtype .kind == "M" :
4640
+ pytest .xfail ("dask operation 'argmax' breaks when dtype is datetime64 (M)" )
4641
+ ar0_raw = xr .DataArray (
4625
4642
x , dims = ["x" ], coords = {"x" : np .arange (x .size ) * 4 }, attrs = self .attrs
4626
4643
)
4627
4644
4645
+ if use_dask :
4646
+ ar0 = ar0_raw .chunk ({})
4647
+ else :
4648
+ ar0 = ar0_raw
4649
+
4628
4650
# dim doesn't exist
4629
4651
with pytest .raises (KeyError ):
4630
4652
ar0 .idxmax (dim = "spam" )
@@ -4944,14 +4966,31 @@ def test_argmax(self, x, minindex, maxindex, nanindex):
4944
4966
4945
4967
assert_identical (result3 , expected2 )
4946
4968
4947
- def test_idxmin (self , x , minindex , maxindex , nanindex ):
4948
- ar0 = xr .DataArray (
4969
+ @pytest .mark .parametrize ("use_dask" , [True , False ])
4970
+ def test_idxmin (self , x , minindex , maxindex , nanindex , use_dask ):
4971
+ if use_dask and not has_dask :
4972
+ pytest .skip ("requires dask" )
4973
+ if use_dask and x .dtype .kind == "M" :
4974
+ pytest .xfail ("dask operation 'argmin' breaks when dtype is datetime64 (M)" )
4975
+
4976
+ if x .dtype .kind == "O" :
4977
+ # TODO: nanops._nan_argminmax_object computes once to check for all-NaN slices.
4978
+ max_computes = 1
4979
+ else :
4980
+ max_computes = 0
4981
+
4982
+ ar0_raw = xr .DataArray (
4949
4983
x ,
4950
4984
dims = ["y" , "x" ],
4951
4985
coords = {"x" : np .arange (x .shape [1 ]) * 4 , "y" : 1 - np .arange (x .shape [0 ])},
4952
4986
attrs = self .attrs ,
4953
4987
)
4954
4988
4989
+ if use_dask :
4990
+ ar0 = ar0_raw .chunk ({})
4991
+ else :
4992
+ ar0 = ar0_raw
4993
+
4955
4994
assert_identical (ar0 , ar0 )
4956
4995
4957
4996
# No dimension specified
@@ -4982,15 +5021,18 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
4982
5021
expected0 .name = "x"
4983
5022
4984
5023
# Default fill value (NaN)
4985
- result0 = ar0 .idxmin (dim = "x" )
5024
+ with raise_if_dask_computes (max_computes = max_computes ):
5025
+ result0 = ar0 .idxmin (dim = "x" )
4986
5026
assert_identical (result0 , expected0 )
4987
5027
4988
5028
# Manually specify NaN fill_value
4989
- result1 = ar0 .idxmin (dim = "x" , fill_value = np .NaN )
5029
+ with raise_if_dask_computes (max_computes = max_computes ):
5030
+ result1 = ar0 .idxmin (dim = "x" , fill_value = np .NaN )
4990
5031
assert_identical (result1 , expected0 )
4991
5032
4992
5033
# keep_attrs
4993
- result2 = ar0 .idxmin (dim = "x" , keep_attrs = True )
5034
+ with raise_if_dask_computes (max_computes = max_computes ):
5035
+ result2 = ar0 .idxmin (dim = "x" , keep_attrs = True )
4994
5036
expected2 = expected0 .copy ()
4995
5037
expected2 .attrs = self .attrs
4996
5038
assert_identical (result2 , expected2 )
@@ -5008,11 +5050,13 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
5008
5050
expected3 .name = "x"
5009
5051
expected3 .attrs = {}
5010
5052
5011
- result3 = ar0 .idxmin (dim = "x" , skipna = False )
5053
+ with raise_if_dask_computes (max_computes = max_computes ):
5054
+ result3 = ar0 .idxmin (dim = "x" , skipna = False )
5012
5055
assert_identical (result3 , expected3 )
5013
5056
5014
5057
# fill_value should be ignored with skipna=False
5015
- result4 = ar0 .idxmin (dim = "x" , skipna = False , fill_value = - 100j )
5058
+ with raise_if_dask_computes (max_computes = max_computes ):
5059
+ result4 = ar0 .idxmin (dim = "x" , skipna = False , fill_value = - 100j )
5016
5060
assert_identical (result4 , expected3 )
5017
5061
5018
5062
# Float fill_value
@@ -5024,7 +5068,8 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
5024
5068
expected5 = xr .concat (expected5 , dim = "y" )
5025
5069
expected5 .name = "x"
5026
5070
5027
- result5 = ar0 .idxmin (dim = "x" , fill_value = - 1.1 )
5071
+ with raise_if_dask_computes (max_computes = max_computes ):
5072
+ result5 = ar0 .idxmin (dim = "x" , fill_value = - 1.1 )
5028
5073
assert_identical (result5 , expected5 )
5029
5074
5030
5075
# Integer fill_value
@@ -5036,7 +5081,8 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
5036
5081
expected6 = xr .concat (expected6 , dim = "y" )
5037
5082
expected6 .name = "x"
5038
5083
5039
- result6 = ar0 .idxmin (dim = "x" , fill_value = - 1 )
5084
+ with raise_if_dask_computes (max_computes = max_computes ):
5085
+ result6 = ar0 .idxmin (dim = "x" , fill_value = - 1 )
5040
5086
assert_identical (result6 , expected6 )
5041
5087
5042
5088
# Complex fill_value
@@ -5048,17 +5094,35 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
5048
5094
expected7 = xr .concat (expected7 , dim = "y" )
5049
5095
expected7 .name = "x"
5050
5096
5051
- result7 = ar0 .idxmin (dim = "x" , fill_value = - 5j )
5097
+ with raise_if_dask_computes (max_computes = max_computes ):
5098
+ result7 = ar0 .idxmin (dim = "x" , fill_value = - 5j )
5052
5099
assert_identical (result7 , expected7 )
5053
5100
5054
- def test_idxmax (self , x , minindex , maxindex , nanindex ):
5055
- ar0 = xr .DataArray (
5101
+ @pytest .mark .parametrize ("use_dask" , [True , False ])
5102
+ def test_idxmax (self , x , minindex , maxindex , nanindex , use_dask ):
5103
+ if use_dask and not has_dask :
5104
+ pytest .skip ("requires dask" )
5105
+ if use_dask and x .dtype .kind == "M" :
5106
+ pytest .xfail ("dask operation 'argmax' breaks when dtype is datetime64 (M)" )
5107
+
5108
+ if x .dtype .kind == "O" :
5109
+ # TODO: nanops._nan_argminmax_object computes once to check for all-NaN slices.
5110
+ max_computes = 1
5111
+ else :
5112
+ max_computes = 0
5113
+
5114
+ ar0_raw = xr .DataArray (
5056
5115
x ,
5057
5116
dims = ["y" , "x" ],
5058
5117
coords = {"x" : np .arange (x .shape [1 ]) * 4 , "y" : 1 - np .arange (x .shape [0 ])},
5059
5118
attrs = self .attrs ,
5060
5119
)
5061
5120
5121
+ if use_dask :
5122
+ ar0 = ar0_raw .chunk ({})
5123
+ else :
5124
+ ar0 = ar0_raw
5125
+
5062
5126
# No dimension specified
5063
5127
with pytest .raises (ValueError ):
5064
5128
ar0 .idxmax ()
@@ -5090,15 +5154,18 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
5090
5154
expected0 .name = "x"
5091
5155
5092
5156
# Default fill value (NaN)
5093
- result0 = ar0 .idxmax (dim = "x" )
5157
+ with raise_if_dask_computes (max_computes = max_computes ):
5158
+ result0 = ar0 .idxmax (dim = "x" )
5094
5159
assert_identical (result0 , expected0 )
5095
5160
5096
5161
# Manually specify NaN fill_value
5097
- result1 = ar0 .idxmax (dim = "x" , fill_value = np .NaN )
5162
+ with raise_if_dask_computes (max_computes = max_computes ):
5163
+ result1 = ar0 .idxmax (dim = "x" , fill_value = np .NaN )
5098
5164
assert_identical (result1 , expected0 )
5099
5165
5100
5166
# keep_attrs
5101
- result2 = ar0 .idxmax (dim = "x" , keep_attrs = True )
5167
+ with raise_if_dask_computes (max_computes = max_computes ):
5168
+ result2 = ar0 .idxmax (dim = "x" , keep_attrs = True )
5102
5169
expected2 = expected0 .copy ()
5103
5170
expected2 .attrs = self .attrs
5104
5171
assert_identical (result2 , expected2 )
@@ -5116,11 +5183,13 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
5116
5183
expected3 .name = "x"
5117
5184
expected3 .attrs = {}
5118
5185
5119
- result3 = ar0 .idxmax (dim = "x" , skipna = False )
5186
+ with raise_if_dask_computes (max_computes = max_computes ):
5187
+ result3 = ar0 .idxmax (dim = "x" , skipna = False )
5120
5188
assert_identical (result3 , expected3 )
5121
5189
5122
5190
# fill_value should be ignored with skipna=False
5123
- result4 = ar0 .idxmax (dim = "x" , skipna = False , fill_value = - 100j )
5191
+ with raise_if_dask_computes (max_computes = max_computes ):
5192
+ result4 = ar0 .idxmax (dim = "x" , skipna = False , fill_value = - 100j )
5124
5193
assert_identical (result4 , expected3 )
5125
5194
5126
5195
# Float fill_value
@@ -5132,7 +5201,8 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
5132
5201
expected5 = xr .concat (expected5 , dim = "y" )
5133
5202
expected5 .name = "x"
5134
5203
5135
- result5 = ar0 .idxmax (dim = "x" , fill_value = - 1.1 )
5204
+ with raise_if_dask_computes (max_computes = max_computes ):
5205
+ result5 = ar0 .idxmax (dim = "x" , fill_value = - 1.1 )
5136
5206
assert_identical (result5 , expected5 )
5137
5207
5138
5208
# Integer fill_value
@@ -5144,7 +5214,8 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
5144
5214
expected6 = xr .concat (expected6 , dim = "y" )
5145
5215
expected6 .name = "x"
5146
5216
5147
- result6 = ar0 .idxmax (dim = "x" , fill_value = - 1 )
5217
+ with raise_if_dask_computes (max_computes = max_computes ):
5218
+ result6 = ar0 .idxmax (dim = "x" , fill_value = - 1 )
5148
5219
assert_identical (result6 , expected6 )
5149
5220
5150
5221
# Complex fill_value
@@ -5156,7 +5227,8 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
5156
5227
expected7 = xr .concat (expected7 , dim = "y" )
5157
5228
expected7 .name = "x"
5158
5229
5159
- result7 = ar0 .idxmax (dim = "x" , fill_value = - 5j )
5230
+ with raise_if_dask_computes (max_computes = max_computes ):
5231
+ result7 = ar0 .idxmax (dim = "x" , fill_value = - 5j )
5160
5232
assert_identical (result7 , expected7 )
5161
5233
5162
5234
0 commit comments