@@ -296,14 +296,15 @@ module TestDataFrame
296
296
297
297
# Check the output of unstack
298
298
df = DataFrame (Fish = CategoricalArray {Union{String, Missing}} ([" Bob" , " Bob" , " Batman" , " Batman" ]),
299
- Key = CategoricalArray{Union{String, Missing}}[" Mass" , " Color" , " Mass" , " Color" ],
299
+ Key = CategoricalArray {Union{String, Missing}} ( [" Mass" , " Color" , " Mass" , " Color" ]) ,
300
300
Value = Union{String, Missing}[" 12 g" , " Red" , " 18 g" , " Grey" ])
301
301
# Check that reordering levels does not confuse unstack
302
302
levels! (df[1 ], [" XXX" , " Bob" , " Batman" ])
303
303
levels! (df[2 ], [" YYY" , " Color" , " Mass" ])
304
304
# Unstack specifying a row column
305
305
df2 = unstack (df, :Fish , :Key , :Value )
306
306
@test levels (df[1 ]) == [" XXX" , " Bob" , " Batman" ] # make sure we did not mess df[1] levels
307
+ @test levels (df[2 ]) == [" YYY" , " Color" , " Mass" ] # make sure we did not mess df[2] levels
307
308
# Unstack without specifying a row column
308
309
df3 = unstack (df, :Key , :Value )
309
310
# The expected output, XXX level should be dropped as it has no rows with this key
@@ -321,6 +322,31 @@ module TestDataFrame
321
322
df4[1 ,:Mass ] = missing
322
323
@test df2 ≅ df4
323
324
325
+ # The same as above but without CategoricalArray
326
+ df = DataFrame (Fish = [" Bob" , " Bob" , " Batman" , " Batman" ],
327
+ Key = [" Mass" , " Color" , " Mass" , " Color" ],
328
+ Value = [" 12 g" , " Red" , " 18 g" , " Grey" ])
329
+ # Unstack specifying a row column
330
+ df2 = unstack (df, :Fish , :Key , :Value )
331
+ # Unstack without specifying a row column
332
+ df3 = unstack (df, :Key , :Value )
333
+ # The expected output, XXX level should be dropped as it has no rows with this key
334
+ df4 = DataFrame (Fish = [" Batman" , " Bob" ],
335
+ Color = [" Grey" , " Red" ],
336
+ Mass = [" 18 g" , " 12 g" ])
337
+ @test df2 ≅ df4
338
+ @test typeof (df2[:Fish ]) <: Vector{String}
339
+ # first column stays as CategoricalArray in df3
340
+ @test df3 == df4
341
+ # Make sure unstack works with missing values at the start of the value column
342
+ allowmissing! (df, :Value )
343
+ df[1 ,:Value ] = missing
344
+ df2 = unstack (df, :Fish , :Key , :Value )
345
+ # This changes the expected result
346
+ allowmissing! (df4, :Mass )
347
+ df4[2 ,:Mass ] = missing
348
+ @test df2 ≅ df4
349
+
324
350
# test empty set of grouping variables
325
351
@test_throws ArgumentError unstack (df, Int[], :Key , :Value )
326
352
@test_throws ArgumentError unstack (df, Symbol[], :Key , :Value )
0 commit comments