Skip to content

Commit

Permalink
Add shortcut atoms for float dtypes
Browse files Browse the repository at this point in the history
It adds `:f32` and `:f64` as shortcuts/aliases for the float dtypes.

This is related to #739
  • Loading branch information
philss committed Nov 27, 2023
1 parent 5131196 commit dc78a2a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
13 changes: 13 additions & 0 deletions lib/explorer/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Explorer.Series do
* `{:datetime, precision}` - DateTime type with millisecond/microsecond/nanosecond precision that unwraps to `Elixir.NaiveDateTime`
* `{:duration, precision}` - Duration type with millisecond/microsecond/nanosecond precision that unwraps to `Explorer.Duration`
* `{:f, size}` - a 64-bit or 32-bit floating point number. The atom `:float` can be used as an alias for `{:f, 64}`.
The atoms `:f32` and `:f64` can be used as shortcuts for `{:f, 32}` and `{:f, 64}` respectively.
* `:integer` - 64-bit signed integer
* `:string` - UTF-8 encoded binary
* `:time` - Time type that unwraps to `Elixir.Time`
Expand Down Expand Up @@ -264,6 +265,18 @@ defmodule Explorer.Series do
string ["1", nil]
>
iex> Explorer.Series.from_list([1, 2], dtype: :f32)
#Explorer.Series<
Polars[2]
f32 [1.0, 2.0]
>
iex> Explorer.Series.from_list([1, nil, 2], dtype: :float)
#Explorer.Series<
Polars[3]
f64 [1.0, nil, 2.0]
>
The `dtype` option is particulary important if a `:binary` series is desired, because
by default binary series will have the dtype of `:string`:
Expand Down
3 changes: 2 additions & 1 deletion lib/explorer/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule Explorer.Shared do
end

def normalise_dtype(dtype) when dtype in @non_list_types, do: dtype
def normalise_dtype(:float), do: {:f, 64}
def normalise_dtype(dtype) when dtype in [:float, :f64], do: {:f, 64}
def normalise_dtype(:f32), do: {:f, 32}
def normalise_dtype(_dtype), do: nil

@doc """
Expand Down
21 changes: 17 additions & 4 deletions test/explorer/series_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,23 @@ defmodule Explorer.SeriesTest do
end

test "integers as {:f, 64}" do
s = Series.from_list([1, 2, 3, 4], dtype: :float)
assert s[0] == 1.0
assert Series.to_list(s) === [1.0, 2.0, 3.0, 4.0]
assert Series.dtype(s) == {:f, 64}
# Shortcuts and the "real" dtype
for dtype <- [:float, :f64, {:f, 64}] do
s = Series.from_list([1, 2, 3, 4], dtype: dtype)
assert s[0] === 1.0
assert Series.to_list(s) === [1.0, 2.0, 3.0, 4.0]
assert Series.dtype(s) === {:f, 64}
end
end

test "integers as {:f, 32}" do
# Shortcut and the "real" dtype
for dtype <- [:f32, {:f, 32}] do
s = Series.from_list([1, 2, 3, 4], dtype: dtype)
assert s[0] === 1.0
assert Series.to_list(s) === [1.0, 2.0, 3.0, 4.0]
assert Series.dtype(s) === {:f, 32}
end
end

test "mixing integers with an invalid atom" do
Expand Down

0 comments on commit dc78a2a

Please sign in to comment.