@@ -228,7 +228,7 @@ def test_transform_dtype():
228
228
def test_transform_bug ():
229
229
# GH 5712
230
230
# transforming on a datetime column
231
- df = DataFrame (dict ( A = Timestamp ("20130101" ), B = np .arange (5 )) )
231
+ df = DataFrame ({ "A" : Timestamp ("20130101" ), "B" : np .arange (5 )} )
232
232
result = df .groupby ("A" )["B" ].transform (lambda x : x .rank (ascending = False ))
233
233
expected = Series (np .arange (5 , 0 , step = - 1 ), name = "B" )
234
234
tm .assert_series_equal (result , expected )
@@ -251,7 +251,7 @@ def test_transform_numeric_to_boolean():
251
251
def test_transform_datetime_to_timedelta ():
252
252
# GH 15429
253
253
# transforming a datetime to timedelta
254
- df = DataFrame (dict ( A = Timestamp ("20130101" ), B = np .arange (5 )) )
254
+ df = DataFrame ({ "A" : Timestamp ("20130101" ), "B" : np .arange (5 )} )
255
255
expected = Series ([Timestamp ("20130101" ) - Timestamp ("20130101" )] * 5 , name = "A" )
256
256
257
257
# this does date math without changing result type in transform
@@ -442,7 +442,7 @@ def test_transform_coercion():
442
442
# 14457
443
443
# when we are transforming be sure to not coerce
444
444
# via assignment
445
- df = DataFrame (dict ( A = ["a" , "a" ], B = [0 , 1 ]) )
445
+ df = DataFrame ({ "A" : ["a" , "a" ], "B" : [0 , 1 ]} )
446
446
g = df .groupby ("A" )
447
447
448
448
expected = g .transform (np .mean )
@@ -456,43 +456,50 @@ def test_groupby_transform_with_int():
456
456
457
457
# floats
458
458
df = DataFrame (
459
- dict (
460
- A = [1 , 1 , 1 , 2 , 2 , 2 ],
461
- B = Series (1 , dtype = "float64" ),
462
- C = Series ([1 , 2 , 3 , 1 , 2 , 3 ], dtype = "float64" ),
463
- D = "foo" ,
464
- )
459
+ {
460
+ "A" : [1 , 1 , 1 , 2 , 2 , 2 ],
461
+ "B" : Series (1 , dtype = "float64" ),
462
+ "C" : Series ([1 , 2 , 3 , 1 , 2 , 3 ], dtype = "float64" ),
463
+ "D" : "foo" ,
464
+ }
465
465
)
466
466
with np .errstate (all = "ignore" ):
467
467
result = df .groupby ("A" ).transform (lambda x : (x - x .mean ()) / x .std ())
468
468
expected = DataFrame (
469
- dict ( B = np .nan , C = Series ([- 1 , 0 , 1 , - 1 , 0 , 1 ], dtype = "float64" ))
469
+ { "B" : np .nan , "C" : Series ([- 1 , 0 , 1 , - 1 , 0 , 1 ], dtype = "float64" )}
470
470
)
471
471
tm .assert_frame_equal (result , expected )
472
472
473
473
# int case
474
- df = DataFrame (dict (A = [1 , 1 , 1 , 2 , 2 , 2 ], B = 1 , C = [1 , 2 , 3 , 1 , 2 , 3 ], D = "foo" ))
474
+ df = DataFrame (
475
+ {
476
+ "A" : [1 , 1 , 1 , 2 , 2 , 2 ],
477
+ "B" : 1 ,
478
+ "C" : [1 , 2 , 3 , 1 , 2 , 3 ],
479
+ "D" : "foo" ,
480
+ }
481
+ )
475
482
with np .errstate (all = "ignore" ):
476
483
result = df .groupby ("A" ).transform (lambda x : (x - x .mean ()) / x .std ())
477
- expected = DataFrame (dict ( B = np .nan , C = [- 1 , 0 , 1 , - 1 , 0 , 1 ]) )
484
+ expected = DataFrame ({ "B" : np .nan , "C" : [- 1 , 0 , 1 , - 1 , 0 , 1 ]} )
478
485
tm .assert_frame_equal (result , expected )
479
486
480
487
# int that needs float conversion
481
488
s = Series ([2 , 3 , 4 , 10 , 5 , - 1 ])
482
- df = DataFrame (dict ( A = [1 , 1 , 1 , 2 , 2 , 2 ], B = 1 , C = s , D = " foo") )
489
+ df = DataFrame ({ "A" : [1 , 1 , 1 , 2 , 2 , 2 ], "B" : 1 , "C" : s , "D" : " foo"} )
483
490
with np .errstate (all = "ignore" ):
484
491
result = df .groupby ("A" ).transform (lambda x : (x - x .mean ()) / x .std ())
485
492
486
493
s1 = s .iloc [0 :3 ]
487
494
s1 = (s1 - s1 .mean ()) / s1 .std ()
488
495
s2 = s .iloc [3 :6 ]
489
496
s2 = (s2 - s2 .mean ()) / s2 .std ()
490
- expected = DataFrame (dict ( B = np .nan , C = concat ([s1 , s2 ])) )
497
+ expected = DataFrame ({ "B" : np .nan , "C" : concat ([s1 , s2 ])} )
491
498
tm .assert_frame_equal (result , expected )
492
499
493
500
# int downcasting
494
501
result = df .groupby ("A" ).transform (lambda x : x * 2 / 2 )
495
- expected = DataFrame (dict ( B = 1 , C = [2 , 3 , 4 , 10 , 5 , - 1 ]) )
502
+ expected = DataFrame ({ "B" : 1 , "C" : [2 , 3 , 4 , 10 , 5 , - 1 ]} )
496
503
tm .assert_frame_equal (result , expected )
497
504
498
505
@@ -659,11 +666,11 @@ def test_cython_transform_frame(op, args, targop):
659
666
# group by values, index level, columns
660
667
for df in [df , df2 ]:
661
668
for gb_target in [
662
- dict ( by = labels ) ,
663
- dict ( level = 0 ) ,
664
- dict ( by = " string") ,
665
- ]: # dict(by= 'string_missing') ]:
666
- # dict(by= ['int','string']) ]:
669
+ { "by" : labels } ,
670
+ { " level" : 0 } ,
671
+ { "by" : " string"} ,
672
+ ]: # {"by": 'string_missing'} ]:
673
+ # {"by": ['int','string']} ]:
667
674
668
675
gb = df .groupby (** gb_target )
669
676
# allowlisted methods set the selection before applying
@@ -986,7 +993,7 @@ def test_transform_absent_categories(func):
986
993
x_vals = [1 ]
987
994
x_cats = range (2 )
988
995
y = [1 ]
989
- df = DataFrame (dict ( x = Categorical (x_vals , x_cats ), y = y ) )
996
+ df = DataFrame ({ "x" : Categorical (x_vals , x_cats ), "y" : y } )
990
997
result = getattr (df .y .groupby (df .x ), func )()
991
998
expected = df .y
992
999
tm .assert_series_equal (result , expected )
@@ -1005,7 +1012,7 @@ def test_ffill_not_in_axis(func, key, val):
1005
1012
1006
1013
def test_transform_invalid_name_raises ():
1007
1014
# GH#27486
1008
- df = DataFrame (dict ( a = [0 , 1 , 1 , 2 ]) )
1015
+ df = DataFrame ({ "a" : [0 , 1 , 1 , 2 ]} )
1009
1016
g = df .groupby (["a" , "b" , "b" , "c" ])
1010
1017
with pytest .raises (ValueError , match = "not a valid function name" ):
1011
1018
g .transform ("some_arbitrary_name" )
@@ -1025,7 +1032,8 @@ def test_transform_invalid_name_raises():
1025
1032
"obj" ,
1026
1033
[
1027
1034
DataFrame (
1028
- dict (a = [0 , 0 , 0 , 1 , 1 , 1 ], b = range (6 )), index = ["A" , "B" , "C" , "D" , "E" , "F" ]
1035
+ {"a" : [0 , 0 , 0 , 1 , 1 , 1 ], "b" : range (6 )},
1036
+ index = ["A" , "B" , "C" , "D" , "E" , "F" ],
1029
1037
),
1030
1038
Series ([0 , 0 , 0 , 1 , 1 , 1 ], index = ["A" , "B" , "C" , "D" , "E" , "F" ]),
1031
1039
],
0 commit comments