Skip to content

Commit

Permalink
Wrap join on with list (#866)
Browse files Browse the repository at this point in the history
* add failing test for case

#865

* use List.wrap/1 with opts[:on] for join/3

* update docs to reflect changes

---------

Co-authored-by: Philip Capel <[email protected]>
  • Loading branch information
pcapel and pcapel authored Feb 21, 2024
1 parent 4b86ce8 commit 6312461
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/explorer/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4751,7 +4751,7 @@ defmodule Explorer.DataFrame do
## Options
* `:on` - The columns to join on. Defaults to overlapping columns. Does not apply to cross join.
* `:on` - The column(s) to join on. Defaults to overlapping columns. Does not apply to cross join.
* `:how` - One of the join types (as an atom) described above. Defaults to `:inner`.
## Examples
Expand Down Expand Up @@ -4935,7 +4935,7 @@ defmodule Explorer.DataFrame do
end

{on, how} =
case {opts[:on], opts[:how]} do
case {List.wrap(opts[:on]), opts[:how]} do
{[], :cross} ->
{[], :cross}

Expand Down
4 changes: 2 additions & 2 deletions lib/explorer/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ defmodule Explorer.Query do
It is equivalent to `df[name]` but inside a query.
This can also be used if you want to access a column
programatically, for example:
programmatically, for example:
iex> df = Explorer.DataFrame.new(nums: [1, 2, 3])
iex> name = :nums
Expand All @@ -706,7 +706,7 @@ defmodule Explorer.Query do
nums s64 [3]
>
For traversing multiple columns programatically,
For traversing multiple columns programmatically,
see `across/0` and `across/1`.
"""
defmacro col(name) do
Expand Down
13 changes: 13 additions & 0 deletions test/explorer/data_frame_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,19 @@ defmodule Explorer.DataFrameTest do
assert_raise ArgumentError, msg, fn -> DF.join(left, right, how: :inner_join) end
end

test "with matching column indexes as single value" do
left = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])
right = DF.new(a: [1, 2, 2], c: ["d", "e", "f"])

df = DF.join(left, right, on: 0)

assert DF.to_columns(df, atom_keys: true) == %{
a: [1, 2, 2],
b: ["a", "b", "b"],
c: ["d", "e", "f"]
}
end

test "with matching column indexes" do
left = DF.new(a: [1, 2, 3], b: ["a", "b", "c"])
right = DF.new(a: [1, 2, 2], c: ["d", "e", "f"])
Expand Down

0 comments on commit 6312461

Please sign in to comment.