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

fix(python): Fix cast Float to String where Float is not turn to Integer before turning to String #18123

Merged
merged 6 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion crates/polars-core/src/datatypes/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,13 @@ impl<'a> AnyValue<'a> {

// to string
(av, DataType::String) => {
AnyValue::StringOwned(format_smartstring!("{}", av.extract::<i64>()?))
if av.is_unsigned_integer() {
AnyValue::StringOwned(format_smartstring!("{}", av.extract::<u64>()?))
} else if av.is_float() {
AnyValue::StringOwned(format_smartstring!("{}", av.extract::<f64>()?))
} else {
AnyValue::StringOwned(format_smartstring!("{}", av.extract::<i64>()?))
}
},

// to binary
Expand Down
11 changes: 11 additions & 0 deletions py-polars/tests/unit/functions/test_lit.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ def test_lit_decimal() -> None:
assert result == value


def test_lit_string_float() -> None:
value = 3.2

expr = pl.lit(value, dtype=pl.Utf8)
df = pl.select(expr)
result = df.item()

assert df.dtypes[0] == pl.String
assert result == str(value)


@given(s=series(min_size=1, max_size=1, allow_null=False, allowed_dtypes=pl.Decimal))
def test_lit_decimal_parametric(s: pl.Series) -> None:
scale = s.dtype.scale # type: ignore[attr-defined]
Expand Down