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

Change inner dtypes of structs to tuple lists #851

Merged
merged 4 commits into from
Feb 9, 2024

Conversation

philss
Copy link
Member

@philss philss commented Feb 8, 2024

This change is necessary to keep the position of fields predictable.

This is related to #847

This change is necessary to keep the position of fields
predictable.
@maennchen
Copy link
Contributor

maennchen commented Feb 8, 2024

@philss I expected the order of the dtype to be respected given that we provide it as a list now and not a map. But that doesn't seem to work.

I crafted the following tests to test my hypothesis:

Mix.install([
  {:explorer,
   github: "elixir-explorer/explorer", branch: "ps-change-struct-inner-dtypes-to-keylists"}
])

alias Explorer.DataFrame
alias Explorer.Series

ExUnit.configure(exclude: [:skip])
ExUnit.start(autorun: false)

defmodule DTypeTest do
  use ExUnit.Case

  test "preserves manually provided dtype order" do
    assert {:struct, [{"b", :string}, {"a", :string}]} =
             [%{"a" => "a", "b" => "b"}, %{"b" => "b", "a" => "a"}]
             |> Series.from_list(dtype: {:struct, [{"b", :string}, {"a", :string}]})
             |> then(& &1.dtype)

    assert {:struct, [{"a", :string}, {"b", :string}]} =
             [%{"a" => "a", "b" => "b"}, %{"b" => "b", "a" => "a"}]
             |> Series.from_list(dtype: {:struct, [{"a", :string}, {"b", :string}]})
             |> then(& &1.dtype)
  end

  test "can cast dtype order" do
    assert {:struct, [{"b", :string}, {"a", :string}]} =
             [%{"a" => "a", "b" => "b"}, %{"b" => "b", "a" => "a"}]
             |> Series.from_list(dtype: {:struct, [{"a", :string}, {"b", :string}]})
             |> Series.cast({:struct, [{"b", :string}, {"a", :string}]})
             |> then(& &1.dtype)
  end

  test "infers correctly ordered dtype from ordered source" do
    assert {:struct, [{"b", :string}, {"a", :string}]} =
             """
             {"col": {"b": "b", "a": "a"}}
             """
             |> DataFrame.load_ndjson!()
             |> then(& &1["col"].dtype)

    assert {:struct, [{"a", :string}, {"b", :string}]} =
             """
             {"col": {"a": "a", "b": "b"}}
             """
             |> DataFrame.load_ndjson!()
             |> then(& &1["col"].dtype)
  end
end

ExUnit.run()

Output:

$ elixir test.exs
Excluding tags: [:skip]



  1) test can cast dtype order (DTypeTest)
     test.exs:27
     match (=) failed
     code:  assert {:struct, [{"b", :string}, {"a", :string}]} =
              [%{"a" => "a", "b" => "b"}, %{"b" => "b", "a" => "a"}]
              |> Series.from_list(dtype: {:struct, [{"a", :string}, {"b", :string}]})
              |> Series.cast({:struct, [{"b", :string}, {"a", :string}]})
              |> then(& &1.dtype)
     left:  {:struct, [{"b", :string}, {"a", :string}]}
     right: {:struct, [{"a", :string}, {"b", :string}]}
     stacktrace:
       test.exs:28: (test)



  2) test infers correctly ordered dtype from ordered source (DTypeTest)
     test.exs:35
     match (=) failed
     code:  assert {:struct, [{"b", :string}, {"a", :string}]} =
              "{\"col\": {\"b\": \"b\", \"a\": \"a\"}}\n"
              |> DataFrame.load_ndjson!()
              |> then(& &1["col"].dtype)
     left:  {:struct, [{"b", :string}, {"a", :string}]}
     right: {:struct, [{"a", :string}, {"b", :string}]}
     stacktrace:
       test.exs:36: (test)



  3) test preserves manually provided dtype order (DTypeTest)
     test.exs:15
     match (=) failed
     code:  assert {:struct, [{"b", :string}, {"a", :string}]} =
              [%{"a" => "a", "b" => "b"}, %{"b" => "b", "a" => "a"}]
              |> Series.from_list(dtype: {:struct, [{"b", :string}, {"a", :string}]})
              |> then(& &1.dtype)
     left:  {:struct, [{"b", :string}, {"a", :string}]}
     right: {:struct, [{"a", :string}, {"b", :string}]}
     stacktrace:
       test.exs:16: (test)


Finished in 0.3 seconds (0.00s async, 0.3s sync)
3 tests, 3 failures

Randomized with seed 206478

@philss
Copy link
Member Author

philss commented Feb 8, 2024

@maennchen yeah, I think some sorting I'm doing is not necessary. Let me try to fix it.

This prevents us to change the order that the user prefers in
the case of a series created using "from_list/2" with a given dtype,
or a series casted with `cast/2`.
@philss
Copy link
Member Author

philss commented Feb 8, 2024

@maennchen please try again. I think it is correct now :)

Copy link
Contributor

@maennchen maennchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philss Looking good :)

lib/explorer/shared.ex Outdated Show resolved Hide resolved
@philss philss merged commit 3942102 into main Feb 9, 2024
4 checks passed
@philss philss deleted the ps-change-struct-inner-dtypes-to-keylists branch February 9, 2024 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants