@@ -4312,4 +4312,70 @@ end
43124312 @test_throws ArgumentError gdf[Not ([true true true true ])]
43134313end
43144314
4315+ @testset " aggregation of empty GroupedDataFrame with table output" begin
4316+ df = DataFrame (:a => Int[])
4317+ gdf = groupby (df, :a )
4318+ @test isequal_typed (combine (gdf, :a => (x -> [(x= 1 , y= " a" )]) => AsTable, :a => :b ),
4319+ DataFrame (a= Int[], x= Int[], y= String[], b= Int[]))
4320+ @test isequal_typed (combine (gdf, :a => (x -> [(1 , " a" )]) => AsTable, :a => :b ),
4321+ DataFrame (a= Int[], x1= Int[], x2= String[], b= Int[]))
4322+ @test isequal_typed (combine (gdf, :a => (x -> [" ab" ]) => AsTable, :a => :b ),
4323+ DataFrame (a= Int[], x1= Char[], x2= Char[], b= Int[]))
4324+ # test below errors because keys for strings do not support == comparison
4325+ @test_throws ArgumentError combine (gdf, :a => (x -> [" ab" , " cd" ]) => AsTable, :a => :b )
4326+ @test isequal_typed (combine (gdf, :a => (x -> []) => AsTable, :a => :b ),
4327+ DataFrame (a= Int[], b= Int[]))
4328+ @test_throws ArgumentError combine (gdf, :a => (x -> [(a= x, b= x), (a= x, c= x)]) => AsTable)
4329+ @test isequal_typed (combine (gdf, :a => (x -> [(x= 1 , y= 2 ), (x= 3 , y= " a" )]) => AsTable),
4330+ DataFrame (a= Int[], x= Int[], y= Any[]))
4331+ @test isequal_typed (combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => AsTable),
4332+ DataFrame (a= Int[], x= Vector{Int}[], y= Any[]))
4333+ @test isequal_typed (combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => [:z1 , :z2 ]),
4334+ DataFrame (a= Int[], z1= Vector{Int}[], z2= Any[]))
4335+ @test_throws ArgumentError combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => [:z1 , :z2 , :z3 ])
4336+
4337+ df = DataFrame (:a => [1 , 2 ])
4338+ gdf = groupby (df, :a )[2 : 1 ]
4339+ @test isequal_typed (combine (gdf, :a => (x -> [(x= 1 , y= " a" )]) => AsTable, :a => :b ),
4340+ DataFrame (a= Int[], x= Int[], y= String[], b= Int[]))
4341+ @test isequal_typed (combine (gdf, :a => (x -> [(1 , " a" )]) => AsTable, :a => :b ),
4342+ DataFrame (a= Int[], x1= Int[], x2= String[], b= Int[]))
4343+ @test isequal_typed (combine (gdf, :a => (x -> [" ab" ]) => AsTable, :a => :b ),
4344+ DataFrame (a= Int[], x1= Char[], x2= Char[], b= Int[]))
4345+ # test below errors because keys for strings do not support == comparison
4346+ @test_throws ArgumentError combine (gdf, :a => (x -> [" ab" , " cd" ]) => AsTable, :a => :b )
4347+ @test isequal_typed (combine (gdf, :a => (x -> []) => AsTable, :a => :b ),
4348+ DataFrame (a= Int[], b= Int[]))
4349+ @test_throws ArgumentError combine (gdf, :a => (x -> [(a= x, b= x), (a= x, c= x)]) => AsTable)
4350+ @test isequal_typed (combine (gdf, :a => (x -> [(x= 1 , y= 2 ), (x= 3 , y= " a" )]) => AsTable),
4351+ DataFrame (a= Int[], x= Int[], y= Any[]))
4352+ @test isequal_typed (combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => AsTable),
4353+ DataFrame (a= Int[], x= Vector{Int}[], y= Any[]))
4354+ @test isequal_typed (combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => [:z1 , :z2 ]),
4355+ DataFrame (a= Int[], z1= Vector{Int}[], z2= Any[]))
4356+ @test_throws ArgumentError combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => [:z1 , :z2 , :z3 ])
4357+
4358+ df = DataFrame (:a => [1 , 2 ])
4359+ gdf = groupby (df, :a )
4360+ @test isequal_typed (combine (gdf, :a => (x -> [(x= 1 , y= " a" )]) => AsTable, :a => :b ),
4361+ DataFrame (a= 1 : 2 , x= [1 , 1 ], y= [" a" , " a" ], b= 1 : 2 ))
4362+ @test isequal_typed (combine (gdf, :a => (x -> [(1 , " a" )]) => AsTable, :a => :b ),
4363+ DataFrame (a= 1 : 2 , x1= [1 , 1 ], x2= [" a" , " a" ], b= 1 : 2 ))
4364+ @test isequal_typed (combine (gdf, :a => (x -> [" ab" ]) => AsTable, :a => :b ),
4365+ DataFrame (a= 1 : 2 , x1= [' a' , ' a' ], x2= [' b' , ' b' ], b= 1 : 2 ))
4366+ # test below errors because keys for strings do not support == comparison
4367+ @test_throws ArgumentError combine (gdf, :a => (x -> [" ab" , " cd" ]) => AsTable, :a => :b )
4368+ @test isequal_typed (combine (gdf, :a => (x -> []) => AsTable, :a => :b ),
4369+ DataFrame (a= 1 : 2 , b= 1 : 2 ))
4370+ @test_throws ArgumentError combine (gdf, :a => (x -> [(a= x, b= x), (a= x, c= x)]) => AsTable)
4371+ @test isequal_typed (combine (gdf, :a => (x -> [(x= 1 , y= 2 ), (x= 3 , y= " a" )]) => AsTable),
4372+ DataFrame (a= [1 , 1 , 2 , 2 ], x= [1 , 3 , 1 , 3 ], y= Any[2 , " a" , 2 , " a" ]))
4373+ @test isequal_typed (combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => AsTable),
4374+ DataFrame (a= [1 , 1 , 2 , 2 ], x= [[1 ], [3 ], [1 ], [3 ]], y= Any[2 , " a" , 2 , " a" ]))
4375+ @test isequal_typed (combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => [:z1 , :z2 ]),
4376+ DataFrame (a= [1 , 1 , 2 , 2 ], z1= [[1 ], [3 ], [1 ], [3 ]], z2= Any[2 , " a" , 2 , " a" ]))
4377+ @test_throws ArgumentError combine (gdf, :a => (x -> [(x= [1 ], y= 2 ), (x= [3 ], y= " a" )]) => [:z1 , :z2 , :z3 ])
4378+ @test_throws ArgumentError combine (gdf, :a => (x -> [Dict (' x' => 1 )]) => AsTable)
4379+ end
4380+
43154381end # module
0 commit comments