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: Ensure same fmt in Series/AnyValue to string cast #18982

Merged
merged 1 commit into from
Sep 27, 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
2 changes: 1 addition & 1 deletion crates/polars-arrow/src/compute/cast/primitive_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::offset::{Offset, Offsets};
use crate::temporal_conversions::*;
use crate::types::{days_ms, f16, months_days_ns, NativeType};

pub(super) trait SerPrimitive {
pub trait SerPrimitive {
fn write(f: &mut Vec<u8>, val: Self) -> usize
where
Self: Sized;
Expand Down
17 changes: 10 additions & 7 deletions crates/polars-core/src/datatypes/any_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use arrow::compute::cast::SerPrimitive;
use arrow::types::PrimitiveType;
use polars_utils::format_pl_smallstr;
#[cfg(feature = "dtype-categorical")]
use polars_utils::sync::SyncPtr;
use polars_utils::total_ord::ToTotalOrd;
Expand Down Expand Up @@ -563,19 +563,22 @@ impl<'a> AnyValue<'a> {
(AnyValue::Float64(v), DataType::Boolean) => AnyValue::Boolean(*v != f64::default()),

// to string
(AnyValue::String(v), DataType::String) => {
AnyValue::StringOwned(PlSmallStr::from_str(v))
},
(AnyValue::String(v), DataType::String) => AnyValue::String(v),
(AnyValue::StringOwned(v), DataType::String) => AnyValue::StringOwned(v.clone()),

(av, DataType::String) => {
let mut tmp = vec![];
if av.is_unsigned_integer() {
AnyValue::StringOwned(format_pl_smallstr!("{}", av.extract::<u64>()?))
let val = av.extract::<u64>()?;
SerPrimitive::write(&mut tmp, val);
} else if av.is_float() {
AnyValue::StringOwned(format_pl_smallstr!("{}", av.extract::<f64>()?))
let val = av.extract::<f64>()?;
SerPrimitive::write(&mut tmp, val);
} else {
AnyValue::StringOwned(format_pl_smallstr!("{}", av.extract::<i64>()?))
let val = av.extract::<i64>()?;
SerPrimitive::write(&mut tmp, val);
}
AnyValue::StringOwned(PlSmallStr::from_str(std::str::from_utf8(&tmp).unwrap()))
},

// to binary
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/operations/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,9 @@ def test_bool_numeric_supertype(dtype: PolarsDataType) -> None:
df = pl.DataFrame({"v": [1, 2, 3, 4, 5, 6]})
result = df.select((pl.col("v") < 3).sum().cast(dtype) / pl.len())
assert result.item() - 0.3333333 <= 0.00001


def test_cast_consistency() -> None:
assert pl.DataFrame().with_columns(a=pl.lit(0.0)).with_columns(
b=pl.col("a").cast(pl.String), c=pl.lit(0.0).cast(pl.String)
).to_dict(as_series=False) == {"a": [0.0], "b": ["0.0"], "c": ["0.0"]}