Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement permutedims #2447

Merged
merged 29 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1e5d04a
naive transpose implementations
kescobo Sep 21, 2020
2255c72
add copycols
kescobo Oct 2, 2020
f02eaae
add dest_namecol arg
kescobo Oct 2, 2020
97ae507
check comprehensions
kescobo Oct 2, 2020
33459e5
fix type signature
kescobo Oct 2, 2020
18be9f3
add some docs
kescobo Oct 2, 2020
fade332
add tests
kescobo Oct 2, 2020
4d91592
switch to permute dims, update args
kescobo Oct 5, 2020
302360b
add some tests <not working>
kescobo Oct 5, 2020
8fe8098
address part of nalimilan's review
kescobo Oct 6, 2020
4c6a2e7
fix and add tests
kescobo Oct 6, 2020
a580623
add doctests
kescobo Oct 6, 2020
ea627b1
bkamins review
kescobo Oct 6, 2020
6cb1772
most of bkamins review
kescobo Oct 6, 2020
4313c37
add docs to manual
kescobo Oct 6, 2020
5ba618f
address more comments
kescobo Oct 7, 2020
82dda05
more comma spaces and float formatting
kescobo Oct 7, 2020
0d51503
allow zero-row and 1 column permutation
kescobo Oct 7, 2020
7635e1a
remove ColumnIndex from docstring
kescobo Oct 7, 2020
097eaa8
slight clarification to docs
kescobo Oct 7, 2020
348cad7
more review comments, fix bounds error
kescobo Oct 8, 2020
94fc792
add news entry
kescobo Oct 8, 2020
cd7a4a2
really fix bounds error, and comma
kescobo Oct 8, 2020
2f3e7b0
fix function signature
kescobo Oct 8, 2020
040d856
missing makeunique + tests
kescobo Oct 12, 2020
e8cd6b1
remove default src_namescol, nalimilan's comments
kescobo Oct 15, 2020
6c7c176
some doc fixes / additions
kescobo Oct 15, 2020
114023e
bonehead
kescobo Oct 15, 2020
dc211be
Apply suggestions from code review
bkamins Oct 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions docs/src/man/reshaping_and_pivoting.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ julia> df1 = DataFrame(a=["x", "y"], b=[1.0, 2.0], c=[3, 4], d=[true, false])
│ 1 │ x │ 1.0 │ 3 │ 1 │
│ 2 │ y │ 2.0 │ 4 │ 0 │

julia> permutedims(df1)
julia> permutedims(df1, 1)
3×3 DataFrame
│ Row │ a │ x │ y │
│ │ String │ Float64 │ Float64 │
Expand All @@ -402,12 +402,15 @@ julia> permutedims(df1)
│ 3 │ d │ 1.0 │ 0.0 │
```

Note that the first column (by default) of the original `df`
Note that the column indexed by `src_colnames` in the original `df`
becomes the column names in the permuted result,
and the column names of the original become a new column.
Typically, this would be used on columns with homogenous element types,
since the element types of the other columns
are the result of `promote_type` on _all_ the permuted columns.
Note also that, by default, the new column created from the column names
of the original `df` has the same name as `src_namescol`.
An optional positional argument `dest_namescol` can alter this:

```jldoctest reshape
julia> df2 = DataFrame(a=["x", "y"], b=[1, "two"], c=[3, 4], d=[true, false])
Expand All @@ -418,12 +421,12 @@ julia> df2 = DataFrame(a=["x", "y"], b=[1, "two"], c=[3, 4], d=[true, false])
│ 1 │ x │ 1 │ 3 │ 1 │
│ 2 │ y │ two │ 4 │ 0 │

julia> permutedims(df2)
julia> permutedims(df2, 1, "different_name")
3×3 DataFrame
│ Row │ a │ x │ y │
│ │ String │ Any │ Any │
├─────┼────────┼─────┼─────┤
│ 1 │ b │ 1 │ two │
│ 2 │ c │ 3 │ 4 │
│ 3 │ d │ 1 │ 0 │
│ Row │ different_name │ x │ y │
│ │ String │ Any │ Any │
├─────┼────────────────┼─────┼─────┤
│ 1 │ b │ 1 │ two │
│ 2 │ c │ 3 │ 4 │
│ 3 │ d │ 1 │ 0 │
```
32 changes: 16 additions & 16 deletions src/abstractdataframe/reshape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ with name specified by `dest_namescol`.
# Arguments
- `df` : the `AbstractDataFrame`
- `src_namescol` : the column that will become the new header.
This column's element type must be `AbstractString` or `Symbol`.
This column's element type must be `AbstractString`, `Symbol`, or `Int`.
- `dest_namescol` : the name of the first column in the returned `DataFrame`.
Defaults to the same name as `src_namescol`.
- `makeunique` : if `false` (the default), an error will be raised
Expand All @@ -443,14 +443,6 @@ julia> df1 = DataFrame(a=["x", "y"], b=[1., 2.], c=[3, 4], d=[true,false])
│ 1 │ x │ 1.0 │ 3 │ 1 │
│ 2 │ y │ 2.0 │ 4 │ 0 │

julia> df2 = DataFrame(a=["x", "y"], b=[1, "two"], c=[3, 4], d=[true, false])
2×4 DataFrame
│ Row │ a │ b │ c │ d │
│ │ String │ Any │ Int64 │ Bool │
├─────┼────────┼─────┼───────┼──────┤
│ 1 │ x │ 1 │ 3 │ 1 │
│ 2 │ y │ two │ 4 │ 0 │

julia> permutedims(df1, 1) # note the column types
3×3 DataFrame
│ Row │ a │ x │ y │
Expand All @@ -460,14 +452,22 @@ julia> permutedims(df1, 1) # note the column types
│ 2 │ c │ 3.0 │ 4.0 │
│ 3 │ d │ 1.0 │ 0.0 │

julia> permutedims(df2, 1)
julia> df2 = DataFrame(a=["x", "y"], b=[1, "two"], c=[3, 4], d=[true, false])
2×4 DataFrame
│ Row │ a │ b │ c │ d │
│ │ String │ Any │ Int64 │ Bool │
├─────┼────────┼─────┼───────┼──────┤
│ 1 │ x │ 1 │ 3 │ 1 │
│ 2 │ y │ two │ 4 │ 0 │

julia> permutedims(df2, 1, "different_name")
3×3 DataFrame
│ Row │ a │ x │ y │
│ │ String │ Any │ Any │
├─────┼────────┼─────┼─────┤
│ 1 │ b │ 1 │ two │
│ 2 │ c │ 3 │ 4 │
│ 3 │ d │ 1 │ 0 │
│ Row │ different_name │ x │ y │
│ │ String │ Any │ Any │
├─────┼────────────────┼─────┼─────┤
│ 1 │ b │ 1 │ two │
│ 2 │ c │ 3 │ 4 │
│ 3 │ d │ 1 │ 0 │
```
"""
function Base.permutedims(df::AbstractDataFrame, src_namescol::ColumnIndex,
Expand Down