@@ -38,17 +38,20 @@ def int_frame_const_col():
38
38
def test_apply (float_frame ):
39
39
with np .errstate (all = "ignore" ):
40
40
# ufunc
41
- applied = float_frame .apply (np .sqrt )
42
- tm .assert_series_equal (np .sqrt (float_frame ["A" ]), applied ["A" ])
41
+ result = np .sqrt (float_frame ["A" ])
42
+ expected = float_frame .apply (np .sqrt )["A" ]
43
+ tm .assert_series_equal (result , expected )
43
44
44
45
# aggregator
45
- applied = float_frame .apply (np .mean )
46
- assert applied ["A" ] == np .mean (float_frame ["A" ])
46
+ result = float_frame .apply (np .mean )["A" ]
47
+ expected = np .mean (float_frame ["A" ])
48
+ assert result == expected
47
49
48
50
d = float_frame .index [0 ]
49
- applied = float_frame .apply (np .mean , axis = 1 )
50
- assert applied [d ] == np .mean (float_frame .xs (d ))
51
- assert applied .index is float_frame .index # want this
51
+ result = float_frame .apply (np .mean , axis = 1 )
52
+ expected = np .mean (float_frame .xs (d ))
53
+ assert result [d ] == expected
54
+ assert result .index is float_frame .index
52
55
53
56
# invalid axis
54
57
df = DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]], index = ["a" , "a" , "c" ])
@@ -58,42 +61,42 @@ def test_apply(float_frame):
58
61
59
62
# GH 9573
60
63
df = DataFrame ({"c0" : ["A" , "A" , "B" , "B" ], "c1" : ["C" , "C" , "D" , "D" ]})
61
- df = df .apply (lambda ts : ts .astype ("category" ))
64
+ result = df .apply (lambda ts : ts .astype ("category" ))
62
65
63
- assert df .shape == (4 , 2 )
64
- assert isinstance (df ["c0" ].dtype , CategoricalDtype )
65
- assert isinstance (df ["c1" ].dtype , CategoricalDtype )
66
+ assert result .shape == (4 , 2 )
67
+ assert isinstance (result ["c0" ].dtype , CategoricalDtype )
68
+ assert isinstance (result ["c1" ].dtype , CategoricalDtype )
66
69
67
70
68
71
def test_apply_axis1_with_ea ():
69
72
# GH#36785
70
- df = DataFrame ({"A" : [Timestamp ("2013-01-01" , tz = "UTC" )]})
71
- result = df .apply (lambda x : x , axis = 1 )
72
- tm .assert_frame_equal (result , df )
73
+ expected = DataFrame ({"A" : [Timestamp ("2013-01-01" , tz = "UTC" )]})
74
+ result = expected .apply (lambda x : x , axis = 1 )
75
+ tm .assert_frame_equal (result , expected )
73
76
74
77
75
78
def test_apply_mixed_datetimelike ():
76
79
# mixed datetimelike
77
80
# GH 7778
78
- df = DataFrame (
81
+ expected = DataFrame (
79
82
{
80
83
"A" : date_range ("20130101" , periods = 3 ),
81
84
"B" : pd .to_timedelta (np .arange (3 ), unit = "s" ),
82
85
}
83
86
)
84
- result = df .apply (lambda x : x , axis = 1 )
85
- tm .assert_frame_equal (result , df )
87
+ result = expected .apply (lambda x : x , axis = 1 )
88
+ tm .assert_frame_equal (result , expected )
86
89
87
90
88
91
def test_apply_empty (float_frame ):
89
92
# empty
90
93
empty_frame = DataFrame ()
91
94
92
- applied = empty_frame .apply (np .sqrt )
93
- assert applied .empty
95
+ result = empty_frame .apply (np .sqrt )
96
+ assert result .empty
94
97
95
- applied = empty_frame .apply (np .mean )
96
- assert applied .empty
98
+ result = empty_frame .apply (np .mean )
99
+ assert result .empty
97
100
98
101
no_rows = float_frame [:0 ]
99
102
result = no_rows .apply (lambda x : x .mean ())
@@ -108,7 +111,7 @@ def test_apply_empty(float_frame):
108
111
# GH 2476
109
112
expected = DataFrame (index = ["a" ])
110
113
result = expected .apply (lambda x : x ["a" ], axis = 1 )
111
- tm .assert_frame_equal (expected , result )
114
+ tm .assert_frame_equal (result , expected )
112
115
113
116
114
117
def test_apply_with_reduce_empty ():
@@ -285,14 +288,13 @@ def _assert_raw(x):
285
288
float_frame .apply (_assert_raw , raw = True )
286
289
float_frame .apply (_assert_raw , axis = 1 , raw = True )
287
290
288
- result0 = float_frame .apply (np .mean , raw = True )
289
- result1 = float_frame .apply (np .mean , axis = 1 , raw = True )
290
-
291
- expected0 = float_frame .apply (lambda x : x .values .mean ())
292
- expected1 = float_frame .apply (lambda x : x .values .mean (), axis = 1 )
291
+ result = float_frame .apply (np .mean , raw = True )
292
+ expected = float_frame .apply (lambda x : x .values .mean ())
293
+ tm .assert_series_equal (result , expected )
293
294
294
- tm .assert_series_equal (result0 , expected0 )
295
- tm .assert_series_equal (result1 , expected1 )
295
+ result = float_frame .apply (np .mean , axis = 1 , raw = True )
296
+ expected = float_frame .apply (lambda x : x .values .mean (), axis = 1 )
297
+ tm .assert_series_equal (result , expected )
296
298
297
299
# no reduction
298
300
result = float_frame .apply (lambda x : x * 2 , raw = True )
@@ -306,8 +308,9 @@ def _assert_raw(x):
306
308
307
309
def test_apply_axis1 (float_frame ):
308
310
d = float_frame .index [0 ]
309
- tapplied = float_frame .apply (np .mean , axis = 1 )
310
- assert tapplied [d ] == np .mean (float_frame .xs (d ))
311
+ result = float_frame .apply (np .mean , axis = 1 )[d ]
312
+ expected = np .mean (float_frame .xs (d ))
313
+ assert result == expected
311
314
312
315
313
316
def test_apply_mixed_dtype_corner ():
@@ -401,27 +404,25 @@ def test_apply_reduce_to_dict():
401
404
# GH 25196 37544
402
405
data = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ["c0" , "c1" ], index = ["i0" , "i1" ])
403
406
404
- result0 = data .apply (dict , axis = 0 )
405
- expected0 = Series ([{"i0" : 1 , "i1" : 3 }, {"i0" : 2 , "i1" : 4 }], index = data .columns )
406
- tm .assert_series_equal (result0 , expected0 )
407
+ result = data .apply (dict , axis = 0 )
408
+ expected = Series ([{"i0" : 1 , "i1" : 3 }, {"i0" : 2 , "i1" : 4 }], index = data .columns )
409
+ tm .assert_series_equal (result , expected )
407
410
408
- result1 = data .apply (dict , axis = 1 )
409
- expected1 = Series ([{"c0" : 1 , "c1" : 2 }, {"c0" : 3 , "c1" : 4 }], index = data .index )
410
- tm .assert_series_equal (result1 , expected1 )
411
+ result = data .apply (dict , axis = 1 )
412
+ expected = Series ([{"c0" : 1 , "c1" : 2 }, {"c0" : 3 , "c1" : 4 }], index = data .index )
413
+ tm .assert_series_equal (result , expected )
411
414
412
415
413
416
def test_apply_differently_indexed ():
414
417
df = DataFrame (np .random .randn (20 , 10 ))
415
418
416
- result0 = df .apply (Series .describe , axis = 0 )
417
- expected0 = DataFrame ({i : v .describe () for i , v in df .items ()}, columns = df .columns )
418
- tm .assert_frame_equal (result0 , expected0 )
419
+ result = df .apply (Series .describe , axis = 0 )
420
+ expected = DataFrame ({i : v .describe () for i , v in df .items ()}, columns = df .columns )
421
+ tm .assert_frame_equal (result , expected )
419
422
420
- result1 = df .apply (Series .describe , axis = 1 )
421
- expected1 = DataFrame (
422
- {i : v .describe () for i , v in df .T .items ()}, columns = df .index
423
- ).T
424
- tm .assert_frame_equal (result1 , expected1 )
423
+ result = df .apply (Series .describe , axis = 1 )
424
+ expected = DataFrame ({i : v .describe () for i , v in df .T .items ()}, columns = df .index ).T
425
+ tm .assert_frame_equal (result , expected )
425
426
426
427
427
428
def test_apply_modify_traceback ():
@@ -525,7 +526,7 @@ def f(r):
525
526
526
527
527
528
def test_apply_convert_objects ():
528
- data = DataFrame (
529
+ expected = DataFrame (
529
530
{
530
531
"A" : [
531
532
"foo" ,
@@ -572,8 +573,8 @@ def test_apply_convert_objects():
572
573
}
573
574
)
574
575
575
- result = data .apply (lambda x : x , axis = 1 )
576
- tm .assert_frame_equal (result . _convert ( datetime = True ), data )
576
+ result = expected .apply (lambda x : x , axis = 1 ). _convert ( datetime = True )
577
+ tm .assert_frame_equal (result , expected )
577
578
578
579
579
580
def test_apply_attach_name (float_frame ):
@@ -635,17 +636,17 @@ def test_applymap(float_frame):
635
636
float_frame .applymap (type )
636
637
637
638
# GH 465: function returning tuples
638
- result = float_frame .applymap (lambda x : (x , x ))
639
- assert isinstance (result [ "A" ][ 0 ] , tuple )
639
+ result = float_frame .applymap (lambda x : (x , x ))[ "A" ][ 0 ]
640
+ assert isinstance (result , tuple )
640
641
641
642
# GH 2909: object conversion to float in constructor?
642
643
df = DataFrame (data = [1 , "a" ])
643
- result = df .applymap (lambda x : x )
644
- assert result . dtypes [ 0 ] == object
644
+ result = df .applymap (lambda x : x ). dtypes [ 0 ]
645
+ assert result == object
645
646
646
647
df = DataFrame (data = [1.0 , "a" ])
647
- result = df .applymap (lambda x : x )
648
- assert result . dtypes [ 0 ] == object
648
+ result = df .applymap (lambda x : x ). dtypes [ 0 ]
649
+ assert result == object
649
650
650
651
# GH 2786
651
652
df = DataFrame (np .random .random ((3 , 4 )))
@@ -672,10 +673,10 @@ def test_applymap(float_frame):
672
673
DataFrame (index = list ("ABC" )),
673
674
DataFrame ({"A" : [], "B" : [], "C" : []}),
674
675
]
675
- for frame in empty_frames :
676
+ for expected in empty_frames :
676
677
for func in [round , lambda x : x ]:
677
- result = frame .applymap (func )
678
- tm .assert_frame_equal (result , frame )
678
+ result = expected .applymap (func )
679
+ tm .assert_frame_equal (result , expected )
679
680
680
681
681
682
def test_applymap_na_ignore (float_frame ):
@@ -743,7 +744,8 @@ def test_frame_apply_dont_convert_datetime64():
743
744
df = df .applymap (lambda x : x + BDay ())
744
745
df = df .applymap (lambda x : x + BDay ())
745
746
746
- assert df .x1 .dtype == "M8[ns]"
747
+ result = df .x1 .dtype
748
+ assert result == "M8[ns]"
747
749
748
750
749
751
def test_apply_non_numpy_dtype ():
@@ -787,11 +789,13 @@ def apply_list(row):
787
789
788
790
def test_apply_noreduction_tzaware_object ():
789
791
# https://github.com/pandas-dev/pandas/issues/31505
790
- df = DataFrame ({"foo" : [Timestamp ("2020" , tz = "UTC" )]}, dtype = "datetime64[ns, UTC]" )
791
- result = df .apply (lambda x : x )
792
- tm .assert_frame_equal (result , df )
793
- result = df .apply (lambda x : x .copy ())
794
- tm .assert_frame_equal (result , df )
792
+ expected = DataFrame (
793
+ {"foo" : [Timestamp ("2020" , tz = "UTC" )]}, dtype = "datetime64[ns, UTC]"
794
+ )
795
+ result = expected .apply (lambda x : x )
796
+ tm .assert_frame_equal (result , expected )
797
+ result = expected .apply (lambda x : x .copy ())
798
+ tm .assert_frame_equal (result , expected )
795
799
796
800
797
801
def test_apply_function_runs_once ():
@@ -885,11 +889,11 @@ def test_infer_row_shape():
885
889
# GH 17437
886
890
# if row shape is changing, infer it
887
891
df = DataFrame (np .random .rand (10 , 2 ))
888
- result = df .apply (np .fft .fft , axis = 0 )
889
- assert result . shape == (10 , 2 )
892
+ result = df .apply (np .fft .fft , axis = 0 ). shape
893
+ assert result == (10 , 2 )
890
894
891
- result = df .apply (np .fft .rfft , axis = 0 )
892
- assert result . shape == (6 , 2 )
895
+ result = df .apply (np .fft .rfft , axis = 0 ). shape
896
+ assert result == (6 , 2 )
893
897
894
898
895
899
def test_with_dictlike_columns ():
0 commit comments