pint-pandas icon indicating copy to clipboard operation
pint-pandas copied to clipboard

`clip` fails on `NA`/`nan` values

Open burnpanck opened this issue 11 months ago • 3 comments

The following code fails in the last line with TypeError: boolean value of NA is ambiguous:

import numpy as np
import pandas as pd
import pint
import pint_pandas

print(f"{pd.__version__=}")
print(f"{pint.__version__=}")
print(f"{pint_pandas.__version__=}")

u = pint.get_application_registry()

a = np.r_[1,2,np.nan,4,10]
print(f"{a=}")
print(f"{np.clip(a,3,5)=}")

s = pd.Series(data=a)
print(f"{s=}")
print(f"{np.clip(s,3,5)=}")

qs = pd.Series(data=pint_pandas.PintArray.from_1darray_quantity(a*u.m))
print(f"{qs=}")
print(f"{np.clip(qs,3*u.m,5*u.m)=}")

It's output before failing in my environment is:

pd.__version__='2.2.3'
pint.__version__='0.24.4'
pint_pandas.__version__='0.6.2'
a=array([ 1.,  2., nan,  4., 10.])
np.clip(a,3,5)=array([ 3.,  3., nan,  4.,  5.])
s=0     1.0
1     2.0
2     NaN
3     4.0
4    10.0
dtype: float64
np.clip(s,3,5)=0    3.0
1    3.0
2    NaN
3    4.0
4    5.0
dtype: float64
qs=0     1.0
1     2.0
2     nan
3     4.0
4    10.0
dtype: pint[meter]

So, clearly, numpy chose to pass-through nan values in np.clip, and so does pandas.

burnpanck avatar Nov 11 '24 11:11 burnpanck