def test_raise_nullable_string_dtype(nullable_string_dtype): indexer = pd.array(["a", "b"], dtype=nullable_string_dtype) arr = np.array([1, 2, 3]) msg = "arrays used as indices must be of integer or boolean type" with pytest.raises(IndexError, match=msg): check_array_indexer(arr, indexer)
def __setitem__(self, key, value): if isinstance(value, (pd.Index, pd.Series)): value = value.to_numpy() if isinstance(value, type(self)): value = value.to_numpy() key = check_array_indexer(self, key) scalar_key = is_scalar(key) scalar_value = is_scalar(value) if scalar_key and not scalar_value: raise ValueError("setting an array element with a sequence.") # validate new items if scalar_value: if pd.isna(value): value = None elif not isinstance(value, str): raise ValueError( f"Cannot set non-string value '{value}' into a ArrowStringArray." ) else: if not is_array_like(value): value = np.asarray(value, dtype=object) if len(value) and not lib.is_string_array(value, skipna=True): raise ValueError("Must provide strings.") if self._use_arrow: string_array = np.asarray(self._arrow_array.to_pandas()) string_array[key] = value self._arrow_array = pa.chunked_array([pa.array(string_array)]) else: self._ndarray[key] = value
def test_boolean_na_returns_indexer(indexer): # https://github.com/pandas-dev/pandas/issues/31503 arr = np.array([1, 2, 3]) result = check_array_indexer(arr, indexer) expected = np.array([True, False, False], dtype=bool) tm.assert_numpy_array_equal(result, expected)
def _check_array_key(array, key): """ Checks a getitem key. """ key = check_array_indexer(array, key) if isinstance(key, numbers.Integral): # To accept also numpy ints key = int(key) key = range(len(array))[key] return slice(key, key + 1) else: return key
def __setitem__(self, key, value): if isinstance(value, (pd.Index, pd.Series)): value = value.to_numpy() key = check_array_indexer(self, key) scalar_key = is_scalar(key) # validate new items if scalar_key: if pd.isna(value): value = None elif not is_list_like(value): raise ValueError('Must provide list.') array = np.asarray(self._arrow_array.to_pandas()) array[key] = value self._arrow_array = pa.chunked_array( [pa.array(array, type=self.dtype.arrow_type)])
def test_raise_invalid_array_dtypes(indexer): arr = np.array([1, 2, 3]) msg = "arrays used as indices must be of integer or boolean type" with pytest.raises(IndexError, match=msg): check_array_indexer(arr, indexer)
def test_int_raise_missing_values(indexer): arr = np.array([1, 2, 3]) msg = "Cannot index with an integer indexer containing NA values" with pytest.raises(ValueError, match=msg): check_array_indexer(arr, indexer)
def test_bool_raise_length(indexer): arr = np.array([1, 2, 3]) msg = "Boolean index has wrong length" with pytest.raises(IndexError, match=msg): check_array_indexer(arr, indexer)
def test_valid_input(indexer, expected): arr = np.array([1, 2, 3]) result = check_array_indexer(arr, indexer) tm.assert_numpy_array_equal(result, expected)
def test_pass_through_non_array_likes(indexer): arr = np.array([1, 2, 3]) result = check_array_indexer(arr, indexer) assert result == indexer
def test_bool_raise_missing_values(indexer): array = np.array([1, 2, 3]) msg = "Cannot mask with a boolean indexer containing NA values" with pytest.raises(ValueError, match=msg): check_array_indexer(array, indexer)