|
4 | 4 | import pandas as pd
|
5 | 5 | from pandas import Series, date_range
|
6 | 6 | import pandas._testing as tm
|
| 7 | +from pandas.core.arrays import PeriodArray |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class TestSeriesIsIn:
|
@@ -90,6 +91,60 @@ def test_isin_read_only(self):
|
90 | 91 | expected = Series([True, True, True])
|
91 | 92 | tm.assert_series_equal(result, expected)
|
92 | 93 |
|
| 94 | + @pytest.mark.parametrize("dtype", [object, None]) |
| 95 | + def test_isin_dt64_values_vs_ints(self, dtype): |
| 96 | + # GH#36621 dont cast integers to datetimes for isin |
| 97 | + dti = date_range("2013-01-01", "2013-01-05") |
| 98 | + ser = Series(dti) |
| 99 | + |
| 100 | + comps = np.asarray([1356998400000000000], dtype=dtype) |
| 101 | + |
| 102 | + res = dti.isin(comps) |
| 103 | + expected = np.array([False] * len(dti), dtype=bool) |
| 104 | + tm.assert_numpy_array_equal(res, expected) |
| 105 | + |
| 106 | + res = ser.isin(comps) |
| 107 | + tm.assert_series_equal(res, Series(expected)) |
| 108 | + |
| 109 | + res = pd.core.algorithms.isin(ser, comps) |
| 110 | + tm.assert_numpy_array_equal(res, expected) |
| 111 | + |
| 112 | + def test_isin_tzawareness_mismatch(self): |
| 113 | + dti = date_range("2013-01-01", "2013-01-05") |
| 114 | + ser = Series(dti) |
| 115 | + |
| 116 | + other = dti.tz_localize("UTC") |
| 117 | + |
| 118 | + res = dti.isin(other) |
| 119 | + expected = np.array([False] * len(dti), dtype=bool) |
| 120 | + tm.assert_numpy_array_equal(res, expected) |
| 121 | + |
| 122 | + res = ser.isin(other) |
| 123 | + tm.assert_series_equal(res, Series(expected)) |
| 124 | + |
| 125 | + res = pd.core.algorithms.isin(ser, other) |
| 126 | + tm.assert_numpy_array_equal(res, expected) |
| 127 | + |
| 128 | + def test_isin_period_freq_mismatch(self): |
| 129 | + dti = date_range("2013-01-01", "2013-01-05") |
| 130 | + pi = dti.to_period("M") |
| 131 | + ser = Series(pi) |
| 132 | + |
| 133 | + # We construct another PeriodIndex with the same i8 values |
| 134 | + # but different dtype |
| 135 | + dtype = dti.to_period("Y").dtype |
| 136 | + other = PeriodArray._simple_new(pi.asi8, dtype=dtype) |
| 137 | + |
| 138 | + res = pi.isin(other) |
| 139 | + expected = np.array([False] * len(pi), dtype=bool) |
| 140 | + tm.assert_numpy_array_equal(res, expected) |
| 141 | + |
| 142 | + res = ser.isin(other) |
| 143 | + tm.assert_series_equal(res, Series(expected)) |
| 144 | + |
| 145 | + res = pd.core.algorithms.isin(ser, other) |
| 146 | + tm.assert_numpy_array_equal(res, expected) |
| 147 | + |
93 | 148 |
|
94 | 149 | @pytest.mark.slow
|
95 | 150 | def test_isin_large_series_mixed_dtypes_and_nan():
|
|
0 commit comments