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

Add support for renamecols keyword argument in crossjoin #3314

Merged
merged 3 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Add `Iterators.partition` support for `DataFrameRows`
([#3299](https://github.com/JuliaData/DataFrames.jl/pull/3299))
* Add support for `renamecols` keyword argument in `crossjoin`
([#3314](https://github.com/JuliaData/DataFrames.jl/pull/3314))
* `DataFrameRows` and `DataFrameColumns` now support
`nrow`, `ncol`, and `Tables.subset`
([#3311](https://github.com/JuliaData/DataFrames.jl/pull/3311))
Expand Down
16 changes: 13 additions & 3 deletions src/join/composer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,8 @@ antijoin(df1::AbstractDataFrame, df2::AbstractDataFrame;
order=:left)

"""
crossjoin(df1::AbstractDataFrame, df2::AbstractDataFrame;
makeunique::Bool=false, renamecols=identity => identity)
crossjoin(df1, df2, dfs...; makeunique = false)

Perform a cross join of two or more data frame objects and return a `DataFrame`
Expand All @@ -1509,6 +1511,11 @@ dimension that changes the fastest.
if duplicate names are found in columns not joined on;
if `true`, duplicate names will be suffixed with `_i`
(`i` starting at 1 for the first duplicate).
- `renamecols` : a `Pair` specifying how columns of left and right data frames should
be renamed in the resulting data frame. Each element of the pair can be a
string or a `Symbol` can be passed in which case it is appended to the original
column name; alternatively a function can be passed in which case it is applied
to each column name, which is passed to it as a `String`.

If more than two data frames are passed, the join is performed
recursively with left associativity.
Expand Down Expand Up @@ -1552,14 +1559,17 @@ julia> crossjoin(df1, df2)
6 │ 3 b
```
"""
function crossjoin(df1::AbstractDataFrame, df2::AbstractDataFrame; makeunique::Bool=false)
function crossjoin(df1::AbstractDataFrame, df2::AbstractDataFrame;
makeunique::Bool=false, renamecols::Pair=identity => identity)
_check_consistency(df1)
_check_consistency(df2)
r1, r2 = size(df1, 1), size(df2, 1)
colindex = merge(index(df1), index(df2), makeunique=makeunique)

new_names = vcat(_rename_cols(_names(df1), first(renamecols)),
_rename_cols(_names(df2), last(renamecols)))
cols = Any[[repeat(c, inner=r2) for c in eachcol(df1)];
[repeat(c, outer=r1) for c in eachcol(df2)]]
res = DataFrame(cols, colindex, copycols=false)
res = DataFrame(cols, new_names, copycols=false, makeunique=makeunique)

for i in 1:ncol(df1)
_copy_col_note_metadata!(res, i, df1, i)
Expand Down
14 changes: 13 additions & 1 deletion test/join.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ anti = left[Bool[ismissing(x) for x in left.Job], [:ID, :Name]]
C=[3, 4, 5, 3, 4, 5])

@test crossjoin(df1, df2) == cross

@test crossjoin(df1, df2, renamecols="_left" => "_right") ==
crossjoin(df1, df2, renamecols=:_left => :_right) ==
crossjoin(df1, df2, renamecols="_left" => :_right) ==
crossjoin(df1, df2, renamecols=:_left => "_right") ==
rename(cross, [:A_left, :B_left, :C_right])
@test crossjoin(df1, df2, renamecols=(x -> x * "_left") => x -> x * "_right") ==
rename(cross, [:A_left, :B_left, :C_right])
@test crossjoin(df1, df2, renamecols=(x -> Symbol(x * "_left")) => x -> Symbol(x * "_right")) ==
rename(cross, [:A_left, :B_left, :C_right])
@test_throws ArgumentError crossjoin(df1, df2, renamecols=(x -> "a") => x -> "a")
@test crossjoin(df1, df2, renamecols=(x -> "a") => x -> "a", makeunique=true) ==
rename(cross, [:a, :a_1, :a_2])

# Cross joins handle naming collisions
@test size(crossjoin(df1, df1, makeunique=true)) == (4, 4)

Expand Down