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

Infinity np.inf behavior is not consistent with not a number np.nan behavior. #2078

Open
lendres opened this issue Nov 20, 2024 · 2 comments

Comments

@lendres
Copy link

lendres commented Nov 20, 2024

It seems that np.inf behaves differently than np.nan when using Pint Quanties in a Series/DataFrame. Finding and replacing seem to work transparently for np.nan, but np.inf can only be found when searching on the magnitudes of Pint Quantities. It would be convent if np.inf search/replaced worked like np.nan.

import numpy                  as     np
import pandas                 as     pd
import pint_pandas

series = pd.Series([np.nan, 6, np.inf])
pintSeries = pd.Series(series, dtype="pint[meter]")

print("\nOriginal Series:\n{0}".format(series))
print("Not a number check:", series.isna().sum())
print("Infinity check:", series.isin([np.inf]).sum())
series = series.replace(np.nan, 0)
series = series.replace(np.inf, 0)
print("\nSeries After Replacement:\n{0}".format(series))


print("\nOriginal Pint Series:\n{0}".format(pintSeries))
print("Not a number check succeeds:", pintSeries.isna().sum())
print("Standard infinity check fails:", pintSeries.isin([np.inf]).sum())
print("Infinity check on magnitudes succeeds:", pintSeries.pint.magnitude.isin([np.inf]).sum())
pintSeries = pintSeries.replace(np.nan, 0)
pintSeries = pintSeries.replace(np.inf, 0)
print("\nPint Series After Replacement:\n{0}".format(pintSeries))

Output:

Original Series:
0    NaN
1    6.0
2    inf
dtype: float64
Not a number check: 1
Infinity check: 1

Series After Replacement:
0    0.0
1    6.0
2    0.0
dtype: float64

Original Pint Series:
0    nan
1    6.0
2    inf
dtype: pint[meter]
Not a number check succeeds: 1
Standard infinity check fails: 0
Infinity check on magnitudes succeeds: 1

Pint Series After Replacement:
0    0.0
1    6.0
2    inf
dtype: pint[meter]
@jules-ch
Copy link
Collaborator

Probably a pint-pandas issue, can you file this in https://github.com/hgrecco/pint-pandas

@lendres
Copy link
Author

lendres commented Dec 11, 2024

Ah, yes, I will do that.

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

No branches or pull requests

2 participants