@@ -4494,29 +4494,36 @@ def test_count(self):
4494
4494
actual = ds .count ()
4495
4495
assert_identical (expected , actual )
4496
4496
4497
- def test_apply (self ):
4497
+ def test_map (self ):
4498
4498
data = create_test_data ()
4499
4499
data .attrs ["foo" ] = "bar"
4500
4500
4501
- assert_identical (data .apply (np .mean ), data .mean ())
4501
+ assert_identical (data .map (np .mean ), data .mean ())
4502
4502
4503
4503
expected = data .mean (keep_attrs = True )
4504
- actual = data .apply (lambda x : x .mean (keep_attrs = True ), keep_attrs = True )
4504
+ actual = data .map (lambda x : x .mean (keep_attrs = True ), keep_attrs = True )
4505
4505
assert_identical (expected , actual )
4506
4506
4507
- assert_identical (data .apply (lambda x : x , keep_attrs = True ), data .drop ("time" ))
4507
+ assert_identical (data .map (lambda x : x , keep_attrs = True ), data .drop ("time" ))
4508
4508
4509
4509
def scale (x , multiple = 1 ):
4510
4510
return multiple * x
4511
4511
4512
- actual = data .apply (scale , multiple = 2 )
4512
+ actual = data .map (scale , multiple = 2 )
4513
4513
assert_equal (actual ["var1" ], 2 * data ["var1" ])
4514
4514
assert_identical (actual ["numbers" ], data ["numbers" ])
4515
4515
4516
- actual = data .apply (np .asarray )
4516
+ actual = data .map (np .asarray )
4517
4517
expected = data .drop ("time" ) # time is not used on a data var
4518
4518
assert_equal (expected , actual )
4519
4519
4520
+ def test_apply_deprecated_map (self ):
4521
+ data = create_test_data ()
4522
+ data .attrs ["foo" ] = "bar"
4523
+
4524
+ with pytest .warns (DeprecationWarning ):
4525
+ assert_identical (data .map (np .mean ), data .mean ())
4526
+
4520
4527
def make_example_math_dataset (self ):
4521
4528
variables = {
4522
4529
"bar" : ("x" , np .arange (100 , 400 , 100 )),
@@ -4543,15 +4550,15 @@ def test_dataset_number_math(self):
4543
4550
def test_unary_ops (self ):
4544
4551
ds = self .make_example_math_dataset ()
4545
4552
4546
- assert_identical (ds .apply (abs ), abs (ds ))
4547
- assert_identical (ds .apply (lambda x : x + 4 ), ds + 4 )
4553
+ assert_identical (ds .map (abs ), abs (ds ))
4554
+ assert_identical (ds .map (lambda x : x + 4 ), ds + 4 )
4548
4555
4549
4556
for func in [
4550
4557
lambda x : x .isnull (),
4551
4558
lambda x : x .round (),
4552
4559
lambda x : x .astype (int ),
4553
4560
]:
4554
- assert_identical (ds .apply (func ), func (ds ))
4561
+ assert_identical (ds .map (func ), func (ds ))
4555
4562
4556
4563
assert_identical (ds .isnull (), ~ ds .notnull ())
4557
4564
@@ -4564,7 +4571,7 @@ def test_unary_ops(self):
4564
4571
def test_dataset_array_math (self ):
4565
4572
ds = self .make_example_math_dataset ()
4566
4573
4567
- expected = ds .apply (lambda x : x - ds ["foo" ])
4574
+ expected = ds .map (lambda x : x - ds ["foo" ])
4568
4575
assert_identical (expected , ds - ds ["foo" ])
4569
4576
assert_identical (expected , - ds ["foo" ] + ds )
4570
4577
assert_identical (expected , ds - ds ["foo" ].variable )
@@ -4573,7 +4580,7 @@ def test_dataset_array_math(self):
4573
4580
actual -= ds ["foo" ]
4574
4581
assert_identical (expected , actual )
4575
4582
4576
- expected = ds .apply (lambda x : x + ds ["bar" ])
4583
+ expected = ds .map (lambda x : x + ds ["bar" ])
4577
4584
assert_identical (expected , ds + ds ["bar" ])
4578
4585
actual = ds .copy (deep = True )
4579
4586
actual += ds ["bar" ]
@@ -4589,7 +4596,7 @@ def test_dataset_dataset_math(self):
4589
4596
assert_identical (ds , ds + 0 * ds )
4590
4597
assert_identical (ds , ds + {"foo" : 0 , "bar" : 0 })
4591
4598
4592
- expected = ds .apply (lambda x : 2 * x )
4599
+ expected = ds .map (lambda x : 2 * x )
4593
4600
assert_identical (expected , 2 * ds )
4594
4601
assert_identical (expected , ds + ds )
4595
4602
assert_identical (expected , ds + ds .data_vars )
@@ -4686,7 +4693,7 @@ def test_dataset_transpose(self):
4686
4693
assert_identical (expected , actual )
4687
4694
4688
4695
actual = ds .transpose ("x" , "y" )
4689
- expected = ds .apply (lambda x : x .transpose ("x" , "y" , transpose_coords = True ))
4696
+ expected = ds .map (lambda x : x .transpose ("x" , "y" , transpose_coords = True ))
4690
4697
assert_identical (expected , actual )
4691
4698
4692
4699
ds = create_test_data ()
0 commit comments