123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- """
- Tests shared by MaskedArray subclasses.
- """
- import numpy as np
- import pytest
- import pandas as pd
- import pandas._testing as tm
- from pandas.tests.extension.base import BaseOpsUtil
- class ComparisonOps(BaseOpsUtil):
- def _compare_other(self, data, op, other):
- # array
- result = pd.Series(op(data, other))
- expected = pd.Series(op(data._data, other), dtype="boolean")
- # fill the nan locations
- expected[data._mask] = pd.NA
- tm.assert_series_equal(result, expected)
- # series
- ser = pd.Series(data)
- result = op(ser, other)
- expected = op(pd.Series(data._data), other)
- # fill the nan locations
- expected[data._mask] = pd.NA
- expected = expected.astype("boolean")
- tm.assert_series_equal(result, expected)
- # subclass will override to parametrize 'other'
- def test_scalar(self, other, comparison_op, dtype):
- op = comparison_op
- left = pd.array([1, 0, None], dtype=dtype)
- result = op(left, other)
- if other is pd.NA:
- expected = pd.array([None, None, None], dtype="boolean")
- else:
- values = op(left._data, other)
- expected = pd.arrays.BooleanArray(values, left._mask, copy=True)
- tm.assert_extension_array_equal(result, expected)
- # ensure we haven't mutated anything inplace
- result[0] = pd.NA
- tm.assert_extension_array_equal(left, pd.array([1, 0, None], dtype=dtype))
- class NumericOps:
- # Shared by IntegerArray and FloatingArray, not BooleanArray
- def test_searchsorted_nan(self, dtype):
- # The base class casts to object dtype, for which searchsorted returns
- # 0 from the left and 10 from the right.
- arr = pd.array(range(10), dtype=dtype)
- assert arr.searchsorted(np.nan, side="left") == 10
- assert arr.searchsorted(np.nan, side="right") == 10
- def test_no_shared_mask(self, data):
- result = data + 1
- assert not tm.shares_memory(result, data)
- def test_array(self, comparison_op, dtype):
- op = comparison_op
- left = pd.array([0, 1, 2, None, None, None], dtype=dtype)
- right = pd.array([0, 1, None, 0, 1, None], dtype=dtype)
- result = op(left, right)
- values = op(left._data, right._data)
- mask = left._mask | right._mask
- expected = pd.arrays.BooleanArray(values, mask)
- tm.assert_extension_array_equal(result, expected)
- # ensure we haven't mutated anything inplace
- result[0] = pd.NA
- tm.assert_extension_array_equal(
- left, pd.array([0, 1, 2, None, None, None], dtype=dtype)
- )
- tm.assert_extension_array_equal(
- right, pd.array([0, 1, None, 0, 1, None], dtype=dtype)
- )
- def test_compare_with_booleanarray(self, comparison_op, dtype):
- op = comparison_op
- left = pd.array([True, False, None] * 3, dtype="boolean")
- right = pd.array([0] * 3 + [1] * 3 + [None] * 3, dtype=dtype)
- other = pd.array([False] * 3 + [True] * 3 + [None] * 3, dtype="boolean")
- expected = op(left, other)
- result = op(left, right)
- tm.assert_extension_array_equal(result, expected)
- # reversed op
- expected = op(other, left)
- result = op(right, left)
- tm.assert_extension_array_equal(result, expected)
- def test_compare_to_string(self, dtype):
- # GH#28930
- ser = pd.Series([1, None], dtype=dtype)
- result = ser == "a"
- expected = pd.Series([False, pd.NA], dtype="boolean")
- self.assert_series_equal(result, expected)
- def test_ufunc_with_out(self, dtype):
- arr = pd.array([1, 2, 3], dtype=dtype)
- arr2 = pd.array([1, 2, pd.NA], dtype=dtype)
- mask = arr == arr
- mask2 = arr2 == arr2
- result = np.zeros(3, dtype=bool)
- result |= mask
- # If MaskedArray.__array_ufunc__ handled "out" appropriately,
- # `result` should still be an ndarray.
- assert isinstance(result, np.ndarray)
- assert result.all()
- # result |= mask worked because mask could be cast losslessly to
- # boolean ndarray. mask2 can't, so this raises
- result = np.zeros(3, dtype=bool)
- msg = "Specify an appropriate 'na_value' for this dtype"
- with pytest.raises(ValueError, match=msg):
- result |= mask2
- # addition
- res = np.add(arr, arr2)
- expected = pd.array([2, 4, pd.NA], dtype=dtype)
- tm.assert_extension_array_equal(res, expected)
- # when passing out=arr, we will modify 'arr' inplace.
- res = np.add(arr, arr2, out=arr)
- assert res is arr
- tm.assert_extension_array_equal(res, expected)
- tm.assert_extension_array_equal(arr, expected)
- def test_mul_td64_array(self, dtype):
- # GH#45622
- arr = pd.array([1, 2, pd.NA], dtype=dtype)
- other = np.arange(3, dtype=np.int64).view("m8[ns]")
- result = arr * other
- expected = pd.array([pd.Timedelta(0), pd.Timedelta(2), pd.NaT])
- tm.assert_extension_array_equal(result, expected)
|