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

Merged
merged 10 commits into from
Nov 18, 2024
Merged

feat: add Series|Expr.is_finite method #1341

merged 10 commits into from
Nov 18, 2024

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

Sorry, something went wrong.

@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
Member Author

Choose a reason for hiding this comment

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

@MarcoGorelli considering the new page we have on booleans, how would you move forward with this?
I am asking just to discuss it, I am ok with keeping the inconsistencies between different pandas dtype backends, and let the use handle those. At the same time, I would aim at some sort of unification towards polars behavior, although in this specific context it seems unfeasible

Copy link
Member

Choose a reason for hiding this comment

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

does it work to do (s > float('-inf')) & (s < float('inf'))?

like this we'd preserve nulls for nullable dtype backends, and we'd get False for the classical numpy ones, which would be in line with the rest of the boolean document

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks good, I will resolve conflicts with main and adjust

Copy link
Collaborator

@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)

@MarcoGorelli
Copy link
Member

just noticed that we're already somewhat inconsistent when the resulting column is boolean:

In [6]: nw.from_native(pd.DataFrame(data)).select(nw.col('a')>1).to_native()
Out[6]: 
       a
0  False
1  False
2   True

In [7]: nw.from_native(pl.DataFrame(data)).select(nw.col('a')>1).to_native()
Out[7]: 
shape: (3, 1)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚ a     β”‚
β”‚ ---   β”‚
β”‚ bool  β”‚
β•žβ•β•β•β•β•β•β•β•‘
β”‚ false β”‚
β”‚ null  β”‚
β”‚ true  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜

This may be fine, not sure there's too much we can do to work around this, but we probably just need a page alongside https://narwhals-dev.github.io/narwhals/other/column_names/ to explain what to expect from boolean columns

@MarcoGorelli
Copy link
Member

I'd put up a page about booleans - if we can agree on it, then I think it unblocks this PR #1392

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

thanks @FBruzzesi !

@MarcoGorelli MarcoGorelli merged commit 2784596 into main Nov 18, 2024
22 checks passed
@MarcoGorelli MarcoGorelli deleted the feat/is-finite branch November 18, 2024 10:26
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