@@ -4298,6 +4298,12 @@ def setup(self):
4298
4298
(np .array ([0 , 1 , 2 , 0 , - 2 , - 4 , 2 ]), 5 , 2 , None ),
4299
4299
(np .array ([0.0 , 1.0 , 2.0 , 0.0 , - 2.0 , - 4.0 , 2.0 ]), 5 , 2 , None ),
4300
4300
(np .array ([1.0 , np .NaN , 2.0 , np .NaN , - 2.0 , - 4.0 , 2.0 ]), 5 , 2 , 1 ),
4301
+ (
4302
+ np .array ([1.0 , np .NaN , 2.0 , np .NaN , - 2.0 , - 4.0 , 2.0 ]).astype ("object" ),
4303
+ 5 ,
4304
+ 2 ,
4305
+ 1 ,
4306
+ ),
4301
4307
(np .array ([np .NaN , np .NaN ]), np .NaN , np .NaN , 0 ),
4302
4308
(
4303
4309
np .array (
@@ -4329,7 +4335,7 @@ def test_min(self, x, minindex, maxindex, nanindex):
4329
4335
assert_identical (result1 , expected1 )
4330
4336
4331
4337
result2 = ar .min (skipna = False )
4332
- if nanindex is not None :
4338
+ if nanindex is not None and ar . dtype . kind != "O" :
4333
4339
expected2 = ar .isel (x = nanindex , drop = True )
4334
4340
expected2 .attrs = {}
4335
4341
else :
@@ -4355,7 +4361,7 @@ def test_max(self, x, minindex, maxindex, nanindex):
4355
4361
assert_identical (result1 , expected1 )
4356
4362
4357
4363
result2 = ar .max (skipna = False )
4358
- if nanindex is not None :
4364
+ if nanindex is not None and ar . dtype . kind != "O" :
4359
4365
expected2 = ar .isel (x = nanindex , drop = True )
4360
4366
expected2 .attrs = {}
4361
4367
else :
@@ -4384,7 +4390,7 @@ def test_argmin(self, x, minindex, maxindex, nanindex):
4384
4390
assert_identical (result1 , expected1 )
4385
4391
4386
4392
result2 = ar .argmin (skipna = False )
4387
- if nanindex is not None :
4393
+ if nanindex is not None and ar . dtype . kind != "O" :
4388
4394
expected2 = indarr .isel (x = nanindex , drop = True )
4389
4395
expected2 .attrs = {}
4390
4396
else :
@@ -4413,7 +4419,7 @@ def test_argmax(self, x, minindex, maxindex, nanindex):
4413
4419
assert_identical (result1 , expected1 )
4414
4420
4415
4421
result2 = ar .argmax (skipna = False )
4416
- if nanindex is not None :
4422
+ if nanindex is not None and ar . dtype . kind != "O" :
4417
4423
expected2 = indarr .isel (x = nanindex , drop = True )
4418
4424
expected2 .attrs = {}
4419
4425
else :
@@ -4448,7 +4454,7 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
4448
4454
assert_identical (result2 , expected1 )
4449
4455
4450
4456
result3 = ar .idxmin (skipna = False )
4451
- if nanindex is not None :
4457
+ if nanindex is not None and ar . dtype . kind != "O" :
4452
4458
expected2 = coordarr .isel (x = nanindex , drop = True ).astype ("float" )
4453
4459
expected2 .name = "x"
4454
4460
expected2 .attrs = {}
@@ -4461,7 +4467,7 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
4461
4467
with pytest .raises (TypeError ):
4462
4468
ar .idxmin (promote = False )
4463
4469
4464
- if nanindex is None :
4470
+ if nanindex is None or ar . dtype . kind == "O" :
4465
4471
expected3 = coordarr .isel (x = minindex , drop = True )
4466
4472
expected3 .name = "x"
4467
4473
@@ -4480,6 +4486,13 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
4480
4486
result7 = ar .idxmin (skipna = False , promote = None )
4481
4487
assert_identical (result7 , expected4 )
4482
4488
4489
+ with pytest .raises (KeyError ):
4490
+ ar .idxmin (dim = "spam" )
4491
+
4492
+ ar2 = xr .DataArray (5 )
4493
+ with pytest .raises (ValueError ):
4494
+ ar2 .idxmin ()
4495
+
4483
4496
def test_idxmax (self , x , minindex , maxindex , nanindex ):
4484
4497
ar = xr .DataArray (
4485
4498
x , dims = ["x" ], coords = {"x" : np .arange (x .size ) * 4 }, attrs = self .attrs ,
@@ -4507,7 +4520,7 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
4507
4520
assert_identical (result2 , expected1 )
4508
4521
4509
4522
result3 = ar .idxmax (skipna = False )
4510
- if nanindex is not None :
4523
+ if nanindex is not None and ar . dtype . kind != "O" :
4511
4524
expected2 = coordarr .isel (x = nanindex , drop = True ).astype ("float" )
4512
4525
expected2 .name = "x"
4513
4526
expected2 .attrs = {}
@@ -4529,7 +4542,7 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
4529
4542
result5 = ar2 .idxmax (promote = None )
4530
4543
assert_identical (result5 , expected1 )
4531
4544
4532
- if nanindex is None :
4545
+ if nanindex is None or ar . dtype . kind == "O" :
4533
4546
expected3 = coordarr .isel (x = maxindex , drop = True )
4534
4547
expected3 .name = "x"
4535
4548
@@ -4548,6 +4561,13 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
4548
4561
result9 = ar .idxmax (skipna = False , promote = None )
4549
4562
assert_identical (result9 , expected4 )
4550
4563
4564
+ with pytest .raises (KeyError ):
4565
+ ar .idxmin (dim = "spam" )
4566
+
4567
+ ar2 = xr .DataArray (5 )
4568
+ with pytest .raises (ValueError ):
4569
+ ar2 .idxmin ()
4570
+
4551
4571
4552
4572
@pytest .mark .parametrize (
4553
4573
"x, minindex, maxindex, nanindex" ,
@@ -4576,6 +4596,18 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
4576
4596
[0 , 2 , np .NaN ],
4577
4597
[None , 1 , 0 ],
4578
4598
),
4599
+ (
4600
+ np .array (
4601
+ [
4602
+ [2.0 , 1.0 , 2.0 , 0.0 , - 2.0 , - 4.0 , 2.0 ],
4603
+ [- 4.0 , np .NaN , 2.0 , np .NaN , - 2.0 , - 4.0 , 2.0 ],
4604
+ [np .NaN ] * 7 ,
4605
+ ]
4606
+ ).astype ("object" ),
4607
+ [5 , 0 , np .NaN ],
4608
+ [0 , 2 , np .NaN ],
4609
+ [None , 1 , 0 ],
4610
+ ),
4579
4611
(
4580
4612
np .array (
4581
4613
[
@@ -4617,7 +4649,10 @@ def test_min(self, x, minindex, maxindex, nanindex):
4617
4649
result2 = ar .min (axis = 1 )
4618
4650
assert_identical (result2 , expected1 )
4619
4651
4620
- minindex = [x if y is None else y for x , y in zip (minindex , nanindex )]
4652
+ minindex = [
4653
+ x if y is None or ar .dtype .kind == "O" else y
4654
+ for x , y in zip (minindex , nanindex )
4655
+ ]
4621
4656
expected2 = [
4622
4657
ar .isel (y = yi ).isel (x = indi , drop = True ) for yi , indi in enumerate (minindex )
4623
4658
]
@@ -4653,7 +4688,10 @@ def test_max(self, x, minindex, maxindex, nanindex):
4653
4688
result2 = ar .max (axis = 1 )
4654
4689
assert_identical (result2 , expected1 )
4655
4690
4656
- maxindex = [x if y is None else y for x , y in zip (maxindex , nanindex )]
4691
+ maxindex = [
4692
+ x if y is None or ar .dtype .kind == "O" else y
4693
+ for x , y in zip (maxindex , nanindex )
4694
+ ]
4657
4695
expected2 = [
4658
4696
ar .isel (y = yi ).isel (x = indi , drop = True ) for yi , indi in enumerate (maxindex )
4659
4697
]
@@ -4696,7 +4734,10 @@ def test_argmin(self, x, minindex, maxindex, nanindex):
4696
4734
expected1 .attrs = self .attrs
4697
4735
assert_identical (result2 , expected1 )
4698
4736
4699
- minindex = [x if y is None else y for x , y in zip (minindex , nanindex )]
4737
+ minindex = [
4738
+ x if y is None or ar .dtype .kind == "O" else y
4739
+ for x , y in zip (minindex , nanindex )
4740
+ ]
4700
4741
expected2 = [
4701
4742
indarr .isel (y = yi ).isel (x = indi , drop = True )
4702
4743
for yi , indi in enumerate (minindex )
@@ -4740,7 +4781,10 @@ def test_argmax(self, x, minindex, maxindex, nanindex):
4740
4781
expected1 .attrs = self .attrs
4741
4782
assert_identical (result2 , expected1 )
4742
4783
4743
- maxindex = [x if y is None else y for x , y in zip (maxindex , nanindex )]
4784
+ maxindex = [
4785
+ x if y is None or ar .dtype .kind == "O" else y
4786
+ for x , y in zip (maxindex , nanindex )
4787
+ ]
4744
4788
expected2 = [
4745
4789
indarr .isel (y = yi ).isel (x = indi , drop = True )
4746
4790
for yi , indi in enumerate (maxindex )
@@ -4782,7 +4826,10 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
4782
4826
expected1 .attrs = self .attrs
4783
4827
assert_identical (result1 , expected1 )
4784
4828
4785
- minindex = [x if y is None else y for x , y in zip (minindex , nanindex )]
4829
+ minindex = [
4830
+ x if y is None or ar .dtype .kind == "O" else y
4831
+ for x , y in zip (minindex , nanindex )
4832
+ ]
4786
4833
expected2 = [
4787
4834
coordarr .isel (y = yi ).isel (x = indi , drop = True )
4788
4835
for yi , indi in enumerate (minindex )
@@ -4835,6 +4882,9 @@ def test_idxmin(self, x, minindex, maxindex, nanindex):
4835
4882
with pytest .raises (KeyError ):
4836
4883
ar .idxmin (dim = "y" )
4837
4884
4885
+ with pytest .raises (KeyError ):
4886
+ ar .idxmax (dim = "spam" )
4887
+
4838
4888
def test_idxmax (self , x , minindex , maxindex , nanindex ):
4839
4889
ar = xr .DataArray (
4840
4890
x ,
@@ -4865,7 +4915,10 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
4865
4915
expected1 .attrs = self .attrs
4866
4916
assert_identical (result1 , expected1 )
4867
4917
4868
- maxindex = [x if y is None else y for x , y in zip (maxindex , nanindex )]
4918
+ maxindex = [
4919
+ x if y is None or ar .dtype .kind == "O" else y
4920
+ for x , y in zip (maxindex , nanindex )
4921
+ ]
4869
4922
expected2 = [
4870
4923
coordarr .isel (y = yi ).isel (x = indi , drop = True )
4871
4924
for yi , indi in enumerate (maxindex )
@@ -4918,6 +4971,9 @@ def test_idxmax(self, x, minindex, maxindex, nanindex):
4918
4971
with pytest .raises (KeyError ):
4919
4972
ar .idxmax (dim = "y" )
4920
4973
4974
+ with pytest .raises (KeyError ):
4975
+ ar .idxmax (dim = "spam" )
4976
+
4921
4977
4922
4978
@pytest .fixture (params = [1 ])
4923
4979
def da (request ):
0 commit comments