-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC/TST: Indexing with NA raises #30308
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
Changes from 2 commits
492f904
6444aa0
53f4f63
3bbf868
a5ac457
0dfe761
dac111d
d1f08d9
3dd59ca
151bdfe
d57b0ac
36be0f6
7bd6c2f
c5f3afb
76bb6ce
505112e
c73ae8e
3efe359
f94483f
953938d
8b1e567
f317c64
c656292
d4f0adc
37ea95e
816a47c
21fd589
3637070
61599f2
6a0eda6
e622826
5004d91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -289,6 +289,13 @@ def _from_factorized(cls, values, original: "BooleanArray"): | |
def _formatter(self, boxed=False): | ||
return str | ||
|
||
@property | ||
def _hasnans(self): | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Note: this is expensive right now! The hope is that we can | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# make this faster by having an optional mask, but not have to change | ||
# source code using it.. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could easily be cached (and then updated on setitem / other mutation) |
||
return self._mask.any() | ||
|
||
def __getitem__(self, item): | ||
if is_integer(item): | ||
if self._mask[item]: | ||
|
@@ -311,7 +318,7 @@ def _coerce_to_ndarray(self, dtype=None, na_value: "Scalar" = libmissing.NA): | |
if dtype is None: | ||
dtype = object | ||
if is_bool_dtype(dtype): | ||
if not self.isna().any(): | ||
if not self._hasnans: | ||
return self._data | ||
else: | ||
raise ValueError( | ||
|
@@ -485,7 +492,7 @@ def astype(self, dtype, copy=True): | |
|
||
if is_bool_dtype(dtype): | ||
# astype_nansafe converts np.nan to True | ||
if self.isna().any(): | ||
if self._hasnans: | ||
raise ValueError("cannot convert float NaN to bool") | ||
else: | ||
return self._data.astype(dtype, copy=copy) | ||
|
@@ -497,7 +504,7 @@ def astype(self, dtype, copy=True): | |
) | ||
# for integer, error if there are missing values | ||
if is_integer_dtype(dtype): | ||
if self.isna().any(): | ||
if self._hasnans: | ||
raise ValueError("cannot convert NA to integer") | ||
# for float dtype, ensure we use np.nan before casting (numpy cannot | ||
# deal with pd.NA) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas.util.testing as tm | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"values, dtype", | ||
[ | ||
([1, 2, 3], "int64"), | ||
([1.0, 2.0, 3.0], "float64"), | ||
(["a", "b", "c"], "object"), | ||
(["a", "b", "c"], "string"), | ||
([1, 2, 3], "datetime64[ns]"), | ||
([1, 2, 3], "datetime64[ns, CET]"), | ||
([1, 2, 3], "timedelta64[ns]"), | ||
(["2000", "2001", "2002"], "Period[D]"), | ||
([1, 0, 3], "Sparse"), | ||
([pd.Interval(0, 1), pd.Interval(1, 2), pd.Interval(3, 4)], "interval"), | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"mask", [[True, False, False], [True, True, True], [False, False, False]] | ||
) | ||
@pytest.mark.parametrize("box_mask", [True, False]) | ||
@pytest.mark.parametrize("frame", [True, False]) | ||
def test_series_mask_boolean(values, dtype, mask, box_mask, frame): | ||
ser = pd.Series(values, dtype=dtype, index=["a", "b", "c"]) | ||
if frame: | ||
ser = ser.to_frame() | ||
mask = pd.array(mask, dtype="boolean") | ||
if box_mask: | ||
mask = pd.Series(mask, index=ser.index) | ||
|
||
expected = ser[mask.astype("bool")] | ||
|
||
result = ser[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
if not box_mask: | ||
# Series.iloc[Series[bool]] isn't allowed | ||
result = ser.iloc[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
result = ser.loc[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
# empty | ||
mask = mask[:0] | ||
ser = ser.iloc[:0] | ||
expected = ser[mask.astype("bool")] | ||
result = ser[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
if not box_mask: | ||
# Series.iloc[Series[bool]] isn't allowed | ||
result = ser.iloc[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
result = ser.loc[mask] | ||
tm.assert_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("frame", [True, False]) | ||
def test_indexing_with_na_raises(frame): | ||
s = pd.Series([1, 2, 3], name="name") | ||
|
||
if frame: | ||
s = s.to_frame() | ||
mask = pd.array([True, False, None], dtype="boolean") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we parametrize this? IIRC There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be parametrized? The boolean array with missing values? |
||
match = "cannot index with vector containing NA / NaN values" | ||
with pytest.raises(ValueError, match=match): | ||
s[mask] | ||
|
||
with pytest.raises(ValueError, match=match): | ||
s.loc[mask] | ||
|
||
with pytest.raises(ValueError, match=match): | ||
s.iloc[mask] |
Uh oh!
There was an error while loading. Please reload this page.