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

Conversation

EricTulowetzke
Copy link
Contributor

@EricTulowetzke EricTulowetzke commented Aug 9, 2024

closes #17773

Fixed:

>>> pl.select(pl.lit(3.2, dtype=pl.Utf8))
shape: (1, 1)
┌─────────┐
│ literal │
│ ---     │
│ str     │
╞═════════╡
│ 3.2     │
└─────────┘

This is my first time doing a PR for this project so set it to draft to hear feedback. If I keep the below line in test where we use agg, max, min break for Categorical data.
(_, DataType::String) => { AnyValue::StringOwned(format_smartstring!("{}", self.extract::<i64>()?)) },

I can add more cases for string casting if that is wanted so we define all types.

Copy link

codecov bot commented Aug 9, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 80.33%. Comparing base (9dd9569) to head (644cb53).
Report is 2 commits behind head on main.

Files Patch % Lines
crates/polars-core/src/datatypes/any_value.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #18123   +/-   ##
=======================================
  Coverage   80.33%   80.33%           
=======================================
  Files        1496     1496           
  Lines      197685   197693    +8     
  Branches     2821     2821           
=======================================
+ Hits       158806   158820   +14     
+ Misses      38358    38352    -6     
  Partials      521      521           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EricTulowetzke EricTulowetzke changed the title fix(python): Fix pl.select(pl.lit(float, dtype=pl.Utf8)) to not round down fix(python): Fix cast Float to String where Float is not turn to Integer before turning to String Aug 9, 2024
@github-actions github-actions bot added fix Bug fix python Related to Python Polars and removed title needs formatting labels Aug 9, 2024
@EricTulowetzke EricTulowetzke marked this pull request as ready for review August 10, 2024 00:00
@@ -536,8 +536,40 @@ impl<'a> AnyValue<'a> {
(AnyValue::Float64(v), DataType::Boolean) => AnyValue::Boolean(*v != f64::default()),

// to string
(av, DataType::String) => {
AnyValue::StringOwned(format_smartstring!("{}", av.extract::<i64>()?))
(AnyValue::UInt8(_v), DataType::String) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need full code branches for every type.

We can check if the av is float and in that case extract f64. We should add a case for u64. For the other cases we can keep i64 extraction.

Copy link
Contributor Author

@EricTulowetzke EricTulowetzke Aug 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I can do

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner! Thanks @EricTulowetzke

@ritchie46 ritchie46 merged commit 4e15587 into pola-rs:main Aug 11, 2024
26 checks passed
@EricTulowetzke EricTulowetzke deleted the casting-literal-floats-fix branch August 11, 2024 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Casting literal floats to strings rounds the value down.
2 participants