-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Document/test unstable decimal dtype #1033
Merged
+69
−0
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule Explorer.PolarsBackend.DecimalUnstableTest do | ||
# This module tracks some oddities we've found with decimal dtypes. The Polars | ||
# docs warn that decimals are unstable: | ||
# | ||
# > This functionality is considered unstable. It is a work-in-progress | ||
# > feature and may not always work as expected. It may be changed at any | ||
# > point without it being considered a breaking change. | ||
# | ||
# https://docs.pola.rs/api/python/stable/reference/api/polars.datatypes.Decimal.html | ||
# | ||
# If the tests in the module start breaking, it probably means Polars has | ||
# changed its decimal implementation. Be prepared to change the tests: they | ||
# function as canaries rather than imposing expected behavior. | ||
|
||
use ExUnit.Case, async: true | ||
|
||
alias Explorer.PolarsBackend | ||
|
||
require Explorer.DataFrame, as: DF | ||
|
||
setup do | ||
%{df: DF.new(a: [Decimal.new("1.0"), Decimal.new("2.0")])} | ||
end | ||
|
||
test "mean returns decimal instead of float", %{df: df} do | ||
assert_raise( | ||
RuntimeError, | ||
""" | ||
DataFrame mismatch. | ||
|
||
expected: | ||
|
||
names: ["a"] | ||
dtypes: %{"a" => {:f, 64}} | ||
|
||
got: | ||
|
||
names: ["a"] | ||
dtypes: %{"a" => {:decimal, 38, 1}} | ||
""", | ||
fn -> DF.summarise(df, a: mean(a)) end | ||
) | ||
end | ||
|
||
test "unchecked mean returns nil", %{df: df} do | ||
# Here we recreate the internals of `DF.summarise(df, a: mean(a))` to avoid | ||
# the invalid dtype expectation. What we should get is a decimal that | ||
# represents the mean, but what we do get is `nil`. | ||
qf = Explorer.Query.new(df) | ||
ldf = PolarsBackend.DataFrame.lazy(df) | ||
lazy_mean_a = Explorer.Series.mean(qf["a"]) | ||
|
||
expr = | ||
lazy_mean_a.data | ||
|> PolarsBackend.Expression.to_expr() | ||
|> PolarsBackend.Expression.alias_expr("a") | ||
|
||
df = | ||
with {:ok, pdf1} <- PolarsBackend.Native.lf_summarise_with(ldf.data, [], [expr]), | ||
{:ok, pdf2} <- PolarsBackend.Native.lf_compute(pdf1), | ||
do: PolarsBackend.Shared.create_dataframe!(pdf2) | ||
|
||
assert Explorer.Series.to_list(df["a"]) == [nil] | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a result of our expectations:
explorer/lib/explorer/backend/lazy_series.ex
Lines 772 to 783 in 020fb23
I'm not entirely sure it's correct. But it seems to be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is fine to default our decimal operations to float and fix them as Polars start returning decimals for them too. I would prefer that than raising (because returning float still "works").
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josevalim Sorry forgot about this. Are you saying we should remove this test and force our decimal operations to return float instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should return whatever Polars return for now and we will fix it naturally as Polars fixes it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, then the test can stay. When Polars fixes their end, we will learn about it because the test will break. Then we can convert it to a test of what the behavior should actually be.