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

Let {:datetime, _} be an alias for {:naive_datetime, _} for backwards compatibility #924

Merged
merged 8 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions lib/explorer/data_frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2865,8 +2865,8 @@ defmodule Explorer.DataFrame do
naive_datetime = %NaiveDateTime{} ->
LazySeries.new(:lazy, [naive_datetime], {:naive_datetime, :nanosecond})

datetime = %DateTime{} ->
LazySeries.new(:lazy, [datetime], {:datetime, :nanosecond})
datetime = %DateTime{time_zone: time_zone} ->
LazySeries.new(:lazy, [datetime], {:datetime, :nanosecond, time_zone})
billylanchantin marked this conversation as resolved.
Show resolved Hide resolved

duration = %Explorer.Duration{precision: precision} ->
LazySeries.new(:lazy, [duration], {:duration, precision})
Expand Down
6 changes: 4 additions & 2 deletions lib/explorer/series.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ defmodule Explorer.Series do
and compatibility with the Elixir ecosystem:

* All numeric dtypes (signed integer, unsigned integer, and floats) can
be specified as an atom in the form of `:s32`, `:u8`, `:f32` and o son
be specified as an atom in the form of `:s32`, `:u8`, `:f32` and so on
* The atom `:float` as an alias for `{:f, 64}` to mirror Elixir's floats
* The atom `:integer` as an alias for `{:s, 64}` to mirror Elixir's integers
* The tuple `{:datetime, _}` as an alias for `{:naive_datetime, _}`.
Note: `{:datetime, _}` has been deprecated and using it will raise a warning.
billylanchantin marked this conversation as resolved.
Show resolved Hide resolved

A series must consist of a single data type only. Series may have `nil` values in them.
The series `dtype` can be retrieved via the `dtype/1` function or directly accessed as
Expand Down Expand Up @@ -370,7 +372,7 @@ defmodule Explorer.Series do
A list of `Date`, `Time`, `NaiveDateTime`, `DateTime`, and
`Explorer.Duration` structs are also supported, and they will become series
with the respective dtypes: `:date`, `:time`, `{:naive_datetime, :microsecond}`,
and `{:duration, precision}`.
`{:datetime, :microsecond, time_zone}` and `{:duration, precision}`.
For example:

iex> Explorer.Series.from_list([~D[0001-01-01], ~D[1970-01-01], ~D[1986-10-13]])
Expand Down
12 changes: 12 additions & 0 deletions lib/explorer/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Explorer.Shared do
# A collection of **private** helpers shared in Explorer.
@moduledoc false

require Logger

@integer_types [
{:s, 8},
{:s, 16},
Expand Down Expand Up @@ -87,6 +89,16 @@ defmodule Explorer.Shared do
def normalise_dtype(:u32), do: {:u, 32}
def normalise_dtype(:u64), do: {:u, 64}

def normalise_dtype({:datetime, precision}) do
:ok =
Logger.warning("""
The `{:datetime, _}` dtype has been deprecated.
Please use `{:naive_datetime, _}` instead.
""")
billylanchantin marked this conversation as resolved.
Show resolved Hide resolved

{:naive_datetime, precision}
end

def normalise_dtype(_dtype), do: nil

@doc """
Expand Down
25 changes: 25 additions & 0 deletions test/explorer/series_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Explorer.SeriesTest do

alias Explorer.Series

import ExUnit.CaptureLog

doctest Explorer.Series

test "defines doc metadata" do
Expand Down Expand Up @@ -286,6 +288,29 @@ defmodule Explorer.SeriesTest do
dates
end

test "with naive datetimes aliased" do
naive_datetimes = [
~N[2022-04-13 15:44:31.560227],
~N[1022-01-04 21:18:31.224123],
~N[1988-11-23 06:36:16.158432],
~N[2353-03-07 00:39:35.702789]
]

dtype_alias = {:datetime, :microsecond}
dtype_actual = {:naive_datetime, :microsecond}

{series, warning} =
with_log(fn -> Series.from_list(naive_datetimes, dtype: dtype_alias) end)

assert Series.to_list(series) == naive_datetimes
assert Series.dtype(series) == dtype_actual

assert warning =~ """
The `{:datetime, _}` dtype has been deprecated.
Please use `{:naive_datetime, _}` instead.
"""
end

test "with 32-bit integers" do
for dtype <- [:s32, {:s, 32}] do
s = Series.from_list([-1, 0, 1, 2, 3, nil], dtype: dtype)
Expand Down
Loading