|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pandas.errors import Pandas4Warning |
| 7 | + |
| 8 | +from pandas import ( |
| 9 | + NA, |
| 10 | + ArrowDtype, |
| 11 | + Series, |
| 12 | + StringDtype, |
| 13 | +) |
| 14 | +import pandas._testing as tm |
| 15 | + |
| 16 | + |
| 17 | +def test_reversed_logical_ops(any_string_dtype): |
| 18 | + # GH#60234 |
| 19 | + dtype = any_string_dtype |
| 20 | + warn = None if dtype == object else Pandas4Warning |
| 21 | + left = Series([True, False, False, True]) |
| 22 | + right = Series(["", "", "b", "c"], dtype=dtype) |
| 23 | + |
| 24 | + msg = "operations between boolean dtype and" |
| 25 | + with tm.assert_produces_warning(warn, match=msg): |
| 26 | + result = left | right |
| 27 | + expected = left | right.astype(bool) |
| 28 | + tm.assert_series_equal(result, expected) |
| 29 | + |
| 30 | + with tm.assert_produces_warning(warn, match=msg): |
| 31 | + result = left & right |
| 32 | + expected = left & right.astype(bool) |
| 33 | + tm.assert_series_equal(result, expected) |
| 34 | + |
| 35 | + with tm.assert_produces_warning(warn, match=msg): |
| 36 | + result = left ^ right |
| 37 | + expected = left ^ right.astype(bool) |
| 38 | + tm.assert_series_equal(result, expected) |
| 39 | + |
| 40 | + |
| 41 | +def test_pathlib_path_division(any_string_dtype, request): |
| 42 | + # GH#61940 |
| 43 | + if any_string_dtype == object: |
| 44 | + mark = pytest.mark.xfail( |
| 45 | + reason="with NA present we go through _masked_arith_op which " |
| 46 | + "raises TypeError bc Path is not recognized by lib.is_scalar." |
| 47 | + ) |
| 48 | + request.applymarker(mark) |
| 49 | + |
| 50 | + item = Path("/Users/Irv/") |
| 51 | + ser = Series(["A", "B", NA], dtype=any_string_dtype) |
| 52 | + |
| 53 | + result = item / ser |
| 54 | + expected = Series([item / "A", item / "B", ser.dtype.na_value], dtype=object) |
| 55 | + tm.assert_series_equal(result, expected) |
| 56 | + |
| 57 | + result = ser / item |
| 58 | + expected = Series(["A" / item, "B" / item, ser.dtype.na_value], dtype=object) |
| 59 | + tm.assert_series_equal(result, expected) |
| 60 | + |
| 61 | + |
| 62 | +def test_mixed_object_comparison(any_string_dtype): |
| 63 | + # GH#60228 |
| 64 | + dtype = any_string_dtype |
| 65 | + ser = Series(["a", "b"], dtype=dtype) |
| 66 | + |
| 67 | + mixed = Series([1, "b"], dtype=object) |
| 68 | + |
| 69 | + result = ser == mixed |
| 70 | + expected = Series([False, True], dtype=bool) |
| 71 | + if dtype == object: |
| 72 | + pass |
| 73 | + elif dtype.storage == "python" and dtype.na_value is NA: |
| 74 | + expected = expected.astype("boolean") |
| 75 | + elif dtype.storage == "pyarrow" and dtype.na_value is NA: |
| 76 | + expected = expected.astype("bool[pyarrow]") |
| 77 | + |
| 78 | + tm.assert_series_equal(result, expected) |
| 79 | + |
| 80 | + |
| 81 | +def test_pyarrow_numpy_string_invalid(): |
| 82 | + # GH#56008 |
| 83 | + pa = pytest.importorskip("pyarrow") |
| 84 | + ser = Series([False, True]) |
| 85 | + ser2 = Series(["a", "b"], dtype=StringDtype(na_value=np.nan)) |
| 86 | + result = ser == ser2 |
| 87 | + expected_eq = Series(False, index=ser.index) |
| 88 | + tm.assert_series_equal(result, expected_eq) |
| 89 | + |
| 90 | + result = ser != ser2 |
| 91 | + expected_ne = Series(True, index=ser.index) |
| 92 | + tm.assert_series_equal(result, expected_ne) |
| 93 | + |
| 94 | + with pytest.raises(TypeError, match="Invalid comparison"): |
| 95 | + ser > ser2 |
| 96 | + |
| 97 | + # GH#59505 |
| 98 | + ser3 = ser2.astype("string[pyarrow]") |
| 99 | + result3_eq = ser3 == ser |
| 100 | + tm.assert_series_equal(result3_eq, expected_eq.astype("bool[pyarrow]")) |
| 101 | + result3_ne = ser3 != ser |
| 102 | + tm.assert_series_equal(result3_ne, expected_ne.astype("bool[pyarrow]")) |
| 103 | + |
| 104 | + with pytest.raises(TypeError, match="Invalid comparison"): |
| 105 | + ser > ser3 |
| 106 | + |
| 107 | + ser4 = ser2.astype(ArrowDtype(pa.string())) |
| 108 | + result4_eq = ser4 == ser |
| 109 | + tm.assert_series_equal(result4_eq, expected_eq.astype("bool[pyarrow]")) |
| 110 | + result4_ne = ser4 != ser |
| 111 | + tm.assert_series_equal(result4_ne, expected_ne.astype("bool[pyarrow]")) |
| 112 | + |
| 113 | + with pytest.raises(TypeError, match="Invalid comparison"): |
| 114 | + ser > ser4 |
0 commit comments