Skip to content

Incorrect series elements for {:list, :category} #1085

Description

@petrkozorezov

I wrote a custom test for #1083 and found strange behavior for {:list, :category} (and other deep :list types with :category inside).

    test "create series for all types" do
      forall_types(fn t ->
        list = 0..3 |> Enum.map(fn _ -> value(t) end)
        s = list |> Series.from_list(dtype: t)
        #IO.puts("type #{inspect t} list: #{inspect list} value #{inspect s}")
        try do
          assert Series.dtype(s) == t
          assert Series.to_list(s) == list
        rescue err ->
          IO.puts("error in \ntype: #{inspect t}\nlist: #{inspect list}\nseries: #{inspect s}\nerror: #{Exception.format(:error, err, __STACKTRACE__)}")
          raise err
        end
      end)
    end

    defp forall_types(fun) do
      types = [
        :null, :binary, :boolean, :category, :date, :time, :string, {:f, 64}, {:decimal, 10, 1}, # {:f, 32},
        {:naive_datetime, :microsecond}, # {:duration, :microsecond} # {:datetime, :microsecond, "UTC"},
      ] ++ (for signed <- [:s, :u], size <- [8, 16, 32, 64], do: {signed, size})
      types = types
        ++ (for t <- types, do: {:list, t})
        ++ (for t <- types, do: {:struct, [{"a", t}]})
      types = types
        ++ (for t <- types, do: {:list, t})
        ++ (for t <- types, do: {:struct, [{"a", t}]})
      types = types
        ++ (for t <- types, do: {:list, t})
        ++ (for t <- types, do: {:struct, [{"a", t}]})
      for t <- types do
        fun.(t)
      end
    end

    defp value(:null), do: nil
    defp value(:binary), do: Enum.random(["ba", "bb"])
    defp value(:boolean), do: Enum.random([true, false])
    defp value(:category), do: Enum.random(["ca", "cb"])
    defp value(:date), do: Enum.random([~D[2000-01-01], ~D[2001-01-01]])
    defp value(:time), do: Enum.random([~T[00:00:00.000000], ~T[01:01:01.000000]])
    defp value(:string), do: Enum.random(["sa", "sb"])
    defp value({:decimal, _, _}), do: Enum.random([Decimal.new("1.3"), Decimal.new("2.3")])
    defp value({:naive_datetime, :microsecond}), do: Enum.random([~N[2000-01-01 00:00:00.000000], ~N[2001-01-01 00:00:00.000000]])
    defp value({:datetime, :microsecond, "UTC"}), do: Enum.random([~U[2000-01-01 00:00:00.000000Z], ~U[2001-01-01 00:00:00.000000Z]])
    defp value({:duration, :microsecond}), do: Enum.random([0, 1246421])
    defp value({:f, _}), do: Enum.random([1.0, 0.1])
    defp value({:s, _}), do: Enum.random([1, -1])
    defp value({:u, _}), do: Enum.random([1, 2])
    defp value({:list, t}), do: [value(t)]
    defp value({:struct, ts}), do: Enum.map(ts, fn {k, t} -> {k, value(t)} end) |> Enum.into(%{})
error in 
type: {:list, {:list, :category}}
list: [[["ca"]], [["cb"]], [["ca"]], [["cb"]]]
series: #Explorer.Series<
  Polars[4]
  list[list[category]] [[["ca"]], [["ca"]], [["ca"]], [["ca"]]]
>
error: ** (ExUnit.AssertionError) 

Assertion with == failed
code:  assert Series.to_list(s) == list
left:  [[["ca"]], [["ca"]], [["ca"]], [["ca"]]]
right: [[["ca"]], [["cb"]], [["ca"]], [["cb"]]]

    (ex_unit 1.18.2) lib/ex_unit/assertions.ex:436: ExUnit.Assertions.assert/2
    test/explorer/series_test.exs:6698: anonymous fn/1 in Explorer.SeriesTest."test frequencies/1 create series for all types"/1
    (elixir 1.18.2) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
    (elixir 1.18.2) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
    test/explorer/series_test.exs:6692: Explorer.SeriesTest."test frequencies/1 create series for all types"/1
    (ex_unit 1.18.2) lib/ex_unit/runner.ex:511: ExUnit.Runner.exec_test/2
    (stdlib 6.2) timer.erl:595: :timer.tc/2
    (ex_unit 1.18.2) lib/ex_unit/runner.ex:433: anonymous fn/6 in ExUnit.Runner.spawn_test_monitor/4

Looks like a bug in polars, but I'm not sure.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions