@@ -25,10 +25,10 @@ const ≅ = isequal
25
25
# first column stays as CategoricalArray in df3
26
26
@test df3 == df4
27
27
# Make sure unstack works with missing values at the start of the value column
28
- df[1 ,:Value ] = missing
28
+ df[1 , :Value ] = missing
29
29
df2 = unstack (df, :Fish , :Key , :Value )
30
30
# This changes the expected result
31
- df4[1 ,:Mass ] = missing
31
+ df4[1 , :Mass ] = missing
32
32
@test df2 ≅ df4
33
33
34
34
df = DataFrame (Fish = CategoricalArray {Union{String, Missing}} ([" Bob" , " Bob" , " Batman" , " Batman" ]),
@@ -62,11 +62,11 @@ const ≅ = isequal
62
62
@test df3 == df4
63
63
# Make sure unstack works with missing values at the start of the value column
64
64
allowmissing! (df, :Value )
65
- df[1 ,:Value ] = missing
65
+ df[1 , :Value ] = missing
66
66
df2 = unstack (df, :Fish , :Key , :Value )
67
67
# This changes the expected result
68
68
allowmissing! (df4, :Mass )
69
- df4[2 ,:Mass ] = missing
69
+ df4[2 , :Mass ] = missing
70
70
@test df2 ≅ df4
71
71
72
72
df = DataFrame (Fish = [" Bob" , " Bob" , " Batman" , " Batman" ],
@@ -89,9 +89,9 @@ const ≅ = isequal
89
89
@test_throws TypeError unstack (df, :Key , :Value , renamecols= Symbol)
90
90
91
91
# test missing value in grouping variable
92
- mdf = DataFrame (id= [missing ,1 , 2 , 3 ], a= 1 : 4 , b= 1 : 4 )
93
- @test unstack (stack (mdf, Not (:id )), :id , :variable , :value )[1 : 3 ,:] == sort (mdf)[1 : 3 ,:]
94
- @test unstack (stack (mdf, Not (1 )), :id , :variable , :value )[1 : 3 ,:] == sort (mdf)[1 : 3 ,:]
92
+ mdf = DataFrame (id= [missing , 1 , 2 , 3 ], a= 1 : 4 , b= 1 : 4 )
93
+ @test unstack (stack (mdf, Not (:id )), :id , :variable , :value )[1 : 3 , :] == sort (mdf)[1 : 3 , :]
94
+ @test unstack (stack (mdf, Not (1 )), :id , :variable , :value )[1 : 3 , :] == sort (mdf)[1 : 3 , :]
95
95
@test unstack (stack (mdf, Not (:id )), :id , :variable , :value )[:, 2 : 3 ] == sort (mdf)[:, 2 : 3 ]
96
96
@test unstack (stack (mdf, Not (1 )), :id , :variable , :value )[:, 2 : 3 ] == sort (mdf)[:, 2 : 3 ]
97
97
158
158
b = unstack (df, :variable , :value )
159
159
@test a ≅ b ≅ DataFrame (id = [1 , 2 ], a = [3 , missing ], b = [missing , 4 ])
160
160
161
- df = DataFrame (variable= [" x" , " x" ], value= [missing , missing ], id= [1 ,1 ])
161
+ df = DataFrame (variable= [" x" , " x" ], value= [missing , missing ], id= [1 , 1 ])
162
162
@test_logs (:warn , " Duplicate entries in unstack at row 2 for key 1 and variable x." ) unstack (df, :variable , :value )
163
163
@test_logs (:warn , " Duplicate entries in unstack at row 2 for key 1 and variable x." ) unstack (df, :id , :variable , :value )
164
164
end
@@ -225,14 +225,14 @@ end
225
225
@test d1s2 == d1s3
226
226
@test propertynames (d1s) == [:c , :d , :e , :variable , :value ]
227
227
@test d1s == d1m
228
- d1m = stack (d1[:, [1 ,3 , 4 ]], Not (:a ))
228
+ d1m = stack (d1[:, [1 , 3 , 4 ]], Not (:a ))
229
229
@test propertynames (d1m) == [:a , :variable , :value ]
230
230
231
231
# Test naming of measure/value columns
232
232
d1s_named = stack (d1, [:a , :b ], variable_name= :letter , value_name= :someval )
233
233
@test d1s_named == stack (d1, r" [ab]" , variable_name= :letter , value_name= :someval )
234
234
@test propertynames (d1s_named) == [:c , :d , :e , :letter , :someval ]
235
- d1m_named = stack (d1[:, [1 ,3 , 4 ]], Not (:a ), variable_name= :letter , value_name= :someval )
235
+ d1m_named = stack (d1[:, [1 , 3 , 4 ]], Not (:a ), variable_name= :letter , value_name= :someval )
236
236
@test propertynames (d1m_named) == [:a , :letter , :someval ]
237
237
238
238
# test empty measures or ids
@@ -270,21 +270,21 @@ end
270
270
@test d1s[! , 5 ] isa DataFrames. StackedVector
271
271
@test ndims (d1s[! , 5 ]) == 1
272
272
@test ndims (typeof (d1s[! , 2 ])) == 1
273
- @test d1s[! , 4 ][[1 ,24 ]] == [" a" , " b" ]
274
- @test d1s[! , 5 ][[1 ,24 ]] == [1 , 4 ]
273
+ @test d1s[! , 4 ][[1 , 24 ]] == [" a" , " b" ]
274
+ @test d1s[! , 5 ][[1 , 24 ]] == [1 , 4 ]
275
275
@test_throws ArgumentError d1s[! , 4 ][true ]
276
276
@test_throws ArgumentError d1s[! , 5 ][true ]
277
277
@test_throws ArgumentError d1s[! , 4 ][1.0 ]
278
278
@test_throws ArgumentError d1s[! , 5 ][1.0 ]
279
279
280
280
d1ss = stack (d1, [:a , :b ], view= true )
281
- @test d1ss[! , 4 ][[1 ,24 ]] == [" a" , " b" ]
281
+ @test d1ss[! , 4 ][[1 , 24 ]] == [" a" , " b" ]
282
282
@test d1ss[! , 4 ] isa DataFrames. RepeatedVector
283
283
d1ss = stack (d1, [:a , :b ], view= true , variable_eltype= String)
284
- @test d1ss[! , 4 ][[1 ,24 ]] == [" a" , " b" ]
284
+ @test d1ss[! , 4 ][[1 , 24 ]] == [" a" , " b" ]
285
285
@test d1ss[! , 4 ] isa DataFrames. RepeatedVector
286
286
d1ss = stack (d1, [:a , :b ], view= true , variable_eltype= Symbol)
287
- @test d1ss[! , 4 ][[1 ,24 ]] == [:a , :b ]
287
+ @test d1ss[! , 4 ][[1 , 24 ]] == [:a , :b ]
288
288
@test d1ss[! , 4 ] isa DataFrames. RepeatedVector
289
289
290
290
# Those tests check indexing RepeatedVector/StackedVector by a vector
307
307
@test d1s2 == d1s3
308
308
@test propertynames (d1s) == [:c , :d , :e , :variable , :value ]
309
309
@test d1s == d1m
310
- d1m = stack (d1[:, [1 ,3 , 4 ]], Not (:a ), view= true )
310
+ d1m = stack (d1[:, [1 , 3 , 4 ]], Not (:a ), view= true )
311
311
@test propertynames (d1m) == [:a , :variable , :value ]
312
312
313
313
d1s_named = stack (d1, [:a , :b ], variable_name= :letter , value_name= :someval , view= true )
@@ -329,13 +329,13 @@ end
329
329
@test d1us3 == unstack (d1s2)
330
330
331
331
# test unstack with exactly one key column that is not passed
332
- df1 = stack (DataFrame (rand (10 ,10 )))
332
+ df1 = stack (DataFrame (rand (10 , 10 )))
333
333
df1[! , :id ] = 1 : 100
334
334
@test size (unstack (df1, :variable , :value )) == (100 , 11 )
335
335
@test unstack (df1, :variable , :value ) ≅ unstack (df1)
336
336
337
337
# test empty keycol
338
- @test_throws ArgumentError unstack (stack (DataFrame (rand (3 ,2 ))), :variable , :value )
338
+ @test_throws ArgumentError unstack (stack (DataFrame (rand (3 , 2 ))), :variable , :value )
339
339
end
340
340
341
341
@testset " column names duplicates" begin
494
494
end
495
495
496
496
@testset " test stack eltype" begin
497
- df = DataFrame (rand (4 ,5 ))
497
+ df = DataFrame (rand (4 , 5 ))
498
498
sdf = stack (df)
499
499
@test eltype (sdf. variable) === String
500
500
@test eltype (typeof (sdf. variable)) === String
507
507
@test eltype (typeof (sdf2. value)) === Float64
508
508
end
509
509
510
+ @testset " permutedims" begin
511
+ df1 = DataFrame (a= [" x" , " y" ], b= rand (2 ), c= [1 , 2 ], d= rand (Bool, 2 ))
512
+
513
+ @test_throws MethodError transpose (df1)
514
+ @test_throws ArgumentError permutedims (df1, :bar )
515
+
516
+ df1_pd = permutedims (df1, 1 )
517
+ @test size (df1_pd, 1 ) == ncol (df1) - 1
518
+ @test size (df1_pd, 2 ) == nrow (df1) + 1
519
+ @test names (df1_pd) == [" a" , " x" , " y" ]
520
+ @test df1_pd == permutedims (df1, :a ) == permutedims (df1, 1 )
521
+ @test names (permutedims (df1, :a , :foo )) == [" foo" , " x" , " y" ]
522
+
523
+ orignames1 = names (df1)[2 : end ]
524
+ for (i, row) in enumerate (eachrow (df1_pd))
525
+ @test Vector (row) == [orignames1[i]; df1[! , orignames1[i]]]
526
+ end
527
+
528
+ # All columns should be promoted
529
+ @test eltype (df1_pd. x) == Float64
530
+ @test eltype (df1_pd. y) == Float64
531
+
532
+ df2 = DataFrame (a= [" x" , " y" ], b= [1.0 , " str" ], c= [1 , 2 ], d= rand (Bool, 2 ))
533
+
534
+ df2_pd = permutedims (df2, :a )
535
+ @test size (df2_pd, 1 ) == ncol (df2) - 1
536
+ @test size (df2_pd, 2 ) == nrow (df2) + 1
537
+ @test names (df2_pd) == [" a" , " x" , " y" ]
538
+
539
+ orignames2 = names (df2)[2 : end ]
540
+ for (i, row) in enumerate (eachrow (df2_pd))
541
+ @test Vector (row) == [orignames2[i]; df2[! , orignames2[i]]]
542
+ end
543
+ @test Any == eltype (df2_pd. x)
544
+ @test Any == eltype (df2_pd. y)
545
+
546
+ df3 = DataFrame (a= fill (" x" , 10 ), b= rand (10 ), c= rand (Int, 10 ), d= rand (Bool, 10 ))
547
+
548
+ d3pd_names = [" a" , " x" , (" x_$i " for i in 1 : 9 ). .. ]
549
+ @test_throws ArgumentError permutedims (df3, 1 )
550
+ @test names (permutedims (df3, 1 , makeunique= true )) == d3pd_names
551
+ @test_throws ArgumentError permutedims (df3[! , [:a ]], 1 ) # single column branch
552
+ @test names (permutedims (df3[! , [:a ]], 1 , makeunique= true )) == d3pd_names
553
+
554
+ df4 = DataFrame (a= rand (2 ), b= rand (2 ), c= [1 , 2 ], d= [1. , missing ],
555
+ e= [" x" , " y" ], f= [:x , :y ], # valid src
556
+ g= [missing , " y" ], h= Union{Missing, String}[" x" , " y" ] # invalid src
557
+ )
558
+
559
+ @test permutedims (df4[! , [:a , :b , :c , :e ]], :e ) ==
560
+ permutedims (df4[! , [:e , :a , :b , :c ]], 1 ) ==
561
+ permutedims (df4[! , [:a , :b , :c , :f ]], :f , :e )
562
+ # Can permute single-column
563
+ @test permutedims (df4[! , [:e ]], 1 ) == DataFrame (e= String[], x= [], y= [])
564
+ # Can't index float Column
565
+ @test_throws ArgumentError permutedims (df4[! , [:a , :b , :c ]], 1 )
566
+ @test_throws ArgumentError permutedims (DataFrame (a= Float64[], b= Float64[]), 1 )
567
+ # Can't index columns that allow for missing
568
+ @test_throws ArgumentError permutedims (df4[! , [:g , :a , :b , :c ]], 1 )
569
+ @test_throws ArgumentError permutedims (df4[! , [:h , :a , :b ]], 1 )
570
+ # Can't permute empty `df` ...
571
+ @test_throws BoundsError permutedims (DataFrame (), 1 )
572
+ # ... but can permute zero-row df
573
+ @test permutedims (DataFrame (a= String[], b= Float64[]), 1 ) == DataFrame (a= [" b" ])
574
+ end
575
+
510
576
end # module
0 commit comments