Skip to content

Commit

Permalink
fix: Properly cast AnyValue string (#18888)
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite authored Sep 24, 2024
1 parent a716989 commit d3f8a5c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/polars-core/src/datatypes/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,11 @@ 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::StringOwned(v), DataType::String) => AnyValue::StringOwned(v.clone()),

(av, DataType::String) => {
if av.is_unsigned_integer() {
AnyValue::StringOwned(format_pl_smallstr!("{}", av.extract::<u64>()?))
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-python/src/dataframe/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ impl PyDataFrame {
(0..df.height()).map(|idx| {
PyTuple::new_bound(
py,
self.df.get_columns().iter().map(|s| match s.dtype() {
self.df.get_columns().iter().map(|c| match c.dtype() {
DataType::Null => py.None(),
DataType::Object(_, _) => {
let obj: Option<&ObjectValue> =
s.get_object(idx).map(|any| any.into());
c.get_object(idx).map(|any| any.into());
obj.to_object(py)
},
// SAFETY: we are in bounds.
_ => unsafe { Wrap(s.get_unchecked(idx)).into_py(py) },
_ => unsafe { Wrap(c.get_unchecked(idx)).into_py(py) },
}),
)
}),
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/unit/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ def test_from_dicts_all_cols_6716() -> None:
):
pl.from_dicts(dicts, infer_schema_length=20)
assert pl.from_dicts(dicts, infer_schema_length=None).dtypes == [pl.String]


def test_dict_float_string_roundtrip_18882() -> None:
assert pl.from_dicts([{"A": "0.1"}]).to_dicts() == [{"A": "0.1"}]

0 comments on commit d3f8a5c

Please sign in to comment.