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

feat: add Series|Expr.is_finite method #1341

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

FBruzzesi
Copy link
Member

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below.

As mentioned in the issue itself, pandas and dask treat nan's and null's as same. Actually, even worse, for non nullable backend, np.isfinite returns False and for nullable-backends will return <NA>. I made the opinionated choice to be consistent across different pandas backends and always return False for nulls and nans. I hope the warning in the docstring is enough

@github-actions github-actions bot added the enhancement New feature or request label Nov 9, 2024
Comment on lines +768 to +770
return self._from_native_series(
np.isfinite(self._native_series) & ~self._native_series.isna()
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is a opinionated choice that na is not finite

Copy link
Member

Choose a reason for hiding this comment

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

πŸ€” no sure, wouldn't we want to preserve null values?

Copy link
Member Author

Choose a reason for hiding this comment

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

Behavior is different for different pandas backend dtype. Let me come back with an example

Copy link
Member

Choose a reason for hiding this comment

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

hmmm actually, for classical pandas types, we wouldn't have the option of returning a nullable boolean (if we want to preserve the dtype backend)

πŸ€” gonna think about this a little longer

Copy link
Member Author

@FBruzzesi FBruzzesi Nov 13, 2024

Choose a reason for hiding this comment

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

These would be the output:

data = [float("nan"), float("inf"), 2.0, None]

s = pd.Series(data)
np.isfinite(s)

0    False
1    False
2     True
3    False
dtype: bool
np.isfinite(s.convert_dtypes(dtype_backend="numpy_nullable"))

0     <NA>
1    False
2     True
3     <NA>
dtype: boolean
np.isfinite(s.convert_dtypes(dtype_backend="pyarrow"))

0    False
1    False
2     True
3    False
dtype: bool

While for polars:

pl.Series(data).is_finite()

shape: (4,)
Series: '' [bool]
[
	false
	false
	true
	null
]

Copy link
Contributor

@EdAbati EdAbati left a comment

Choose a reason for hiding this comment

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

Thanks! Given the difference with NaN/Null across backends, your implementation looks good to me πŸ‘ŒπŸ‘Œ


(backends arguing about nulls)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enh]: Add support for Series|Expr.is_finite
3 participants