Skip to content

Commit a3867e4

Browse files
authored
1-arg permutedims(df) (#3115)
1 parent 8acb679 commit a3867e4

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/abstractdataframe/reshape.jl

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ Base.transpose(::AbstractDataFrame, args...; kwargs...) =
695695
throw(ArgumentError("`transpose` not defined for `AbstractDataFrame`s. Try `permutedims` instead"))
696696

697697
"""
698-
permutedims(df::AbstractDataFrame, src_namescol::Union{Int, Symbol, AbstractString},
698+
permutedims(df::AbstractDataFrame,
699+
[src_namescol::Union{Int, Symbol, AbstractString}],
699700
[dest_namescol::Union{Symbol, AbstractString}];
700701
makeunique::Bool=false, strict::Bool=true)
701702
@@ -707,21 +708,25 @@ with name specified by `dest_namescol`.
707708
# Arguments
708709
- `df` : the `AbstractDataFrame`
709710
- `src_namescol` : the column that will become the new header.
711+
If omitted then column names `:x1`, `:x2`, ... are generated automatically.
710712
- `dest_namescol` : the name of the first column in the returned `DataFrame`.
711713
Defaults to the same name as `src_namescol`.
714+
Not supported when `src_namescol` is a vector or is omitted.
712715
- `makeunique` : if `false` (the default), an error will be raised
713716
if duplicate names are found; if `true`, duplicate names will be suffixed
714717
with `_i` (`i` starting at 1 for the first duplicate).
718+
Not supported when `src_namescol` is omitted.
715719
- `strict` : if `true` (the default), an error will be raised if the values
716720
contained in the `src_namescol` are not all `Symbol` or all `AbstractString`,
717721
or can all be converted to `String` using `convert`. If `false`
718722
then any values are accepted and the will be changed to strings using
719723
the `string` function.
724+
Not supported when `src_namescol` is a vector or is omitted.
720725
721726
Note: The element types of columns in resulting `DataFrame`
722-
(other than the first column, which always has element type `String`)
723-
will depend on the element types of _all_ input columns
724-
based on the result of `promote_type`.
727+
(other than the first column if it is created from `df` column names,
728+
which always has element type `String`) will depend on the element types of
729+
_all_ input columns based on the result of `promote_type`.
725730
That is, if the source data frame contains `Int` and `Float64` columns,
726731
resulting columns will have element type `Float64`. If the source has
727732
`Int` and `String` columns, resulting columns will have element type `Any`.
@@ -732,6 +737,30 @@ column-level metadata is dropped.
732737
# Examples
733738
734739
```jldoctest
740+
julia> df = DataFrame(a=1:2, b=3:4)
741+
2×2 DataFrame
742+
Row │ a b
743+
│ Int64 Int64
744+
─────┼──────────────
745+
1 │ 1 3
746+
2 │ 2 4
747+
748+
julia> permutedims(df)
749+
2×2 DataFrame
750+
Row │ x1 x2
751+
│ Int64 Int64
752+
─────┼──────────────
753+
1 │ 1 2
754+
2 │ 3 4
755+
756+
julia> permutedims(df, [:p, :q])
757+
2×2 DataFrame
758+
Row │ p q
759+
│ Int64 Int64
760+
─────┼──────────────
761+
1 │ 1 2
762+
2 │ 3 4
763+
735764
julia> df1 = DataFrame(a=["x", "y"], b=[1.0, 2.0], c=[3, 4], d=[true, false])
736765
2×4 DataFrame
737766
Row │ a b c d
@@ -821,3 +850,7 @@ function Base.permutedims(df::AbstractDataFrame, src_namescol::ColumnIndex;
821850
return permutedims(df, src_namescol, dest_namescol;
822851
makeunique=makeunique, strict=strict)
823852
end
853+
854+
Base.permutedims(df::AbstractDataFrame) = DataFrame(permutedims(Matrix(df)), :auto)
855+
Base.permutedims(df::AbstractDataFrame, cnames::AbstractVector; makeunique::Bool=false) =
856+
DataFrame(permutedims(Matrix(df)), cnames, makeunique=makeunique)

test/reshape.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,15 @@ end
668668
@test_throws ArgumentError permutedims(df, 1)
669669
# but allowed with strict=false
670670
@test permutedims(df, 1, strict=false) == ref
671+
672+
df = DataFrame(a=1:2, b=3:4)
673+
@test permutedims(df) == DataFrame(x1=[1, 3], x2=[2, 4])
674+
@test permutedims(df, [:p, :q]) == DataFrame(p=[1, 3], q=[2, 4])
675+
@test permutedims(df, ["p", "q"]) == DataFrame(p=[1, 3], q=[2, 4])
676+
@test_throws ArgumentError permutedims(df, ["p", "p"]) == DataFrame(p=[1, 3], q=[2, 4])
677+
@test permutedims(df, ["p", "p"], makeunique=true) == DataFrame(p=[1, 3], p_1=[2, 4])
678+
@test permutedims(DataFrame()) == permutedims(DataFrame(a=[], b=[])) ==
679+
permutedims(DataFrame(), []) == permutedims(DataFrame(a=[], b=[]), []) == DataFrame()
671680
end
672681

673682
@testset "stack view=true additional tests" begin

0 commit comments

Comments
 (0)