|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pandas import DatetimeIndex, Index, Timestamp, date_range, to_datetime |
| 7 | +import pandas._testing as tm |
| 8 | + |
| 9 | +from pandas.tseries.offsets import BDay, BMonthEnd |
| 10 | + |
| 11 | + |
| 12 | +class TestJoin: |
| 13 | + def test_does_not_convert_mixed_integer(self): |
| 14 | + df = tm.makeCustomDataframe( |
| 15 | + 10, |
| 16 | + 10, |
| 17 | + data_gen_f=lambda *args, **kwargs: np.random.randn(), |
| 18 | + r_idx_type="i", |
| 19 | + c_idx_type="dt", |
| 20 | + ) |
| 21 | + cols = df.columns.join(df.index, how="outer") |
| 22 | + joined = cols.join(df.columns) |
| 23 | + assert cols.dtype == np.dtype("O") |
| 24 | + assert cols.dtype == joined.dtype |
| 25 | + tm.assert_numpy_array_equal(cols.values, joined.values) |
| 26 | + |
| 27 | + def test_join_self(self, join_type): |
| 28 | + index = date_range("1/1/2000", periods=10) |
| 29 | + joined = index.join(index, how=join_type) |
| 30 | + assert index is joined |
| 31 | + |
| 32 | + def test_join_with_period_index(self, join_type): |
| 33 | + df = tm.makeCustomDataframe( |
| 34 | + 10, |
| 35 | + 10, |
| 36 | + data_gen_f=lambda *args: np.random.randint(2), |
| 37 | + c_idx_type="p", |
| 38 | + r_idx_type="dt", |
| 39 | + ) |
| 40 | + s = df.iloc[:5, 0] |
| 41 | + |
| 42 | + expected = df.columns.astype("O").join(s.index, how=join_type) |
| 43 | + result = df.columns.join(s.index, how=join_type) |
| 44 | + tm.assert_index_equal(expected, result) |
| 45 | + |
| 46 | + def test_join_object_index(self): |
| 47 | + rng = date_range("1/1/2000", periods=10) |
| 48 | + idx = Index(["a", "b", "c", "d"]) |
| 49 | + |
| 50 | + result = rng.join(idx, how="outer") |
| 51 | + assert isinstance(result[0], Timestamp) |
| 52 | + |
| 53 | + def test_join_utc_convert(self, join_type): |
| 54 | + rng = date_range("1/1/2011", periods=100, freq="H", tz="utc") |
| 55 | + |
| 56 | + left = rng.tz_convert("US/Eastern") |
| 57 | + right = rng.tz_convert("Europe/Berlin") |
| 58 | + |
| 59 | + result = left.join(left[:-5], how=join_type) |
| 60 | + assert isinstance(result, DatetimeIndex) |
| 61 | + assert result.tz == left.tz |
| 62 | + |
| 63 | + result = left.join(right[:-5], how=join_type) |
| 64 | + assert isinstance(result, DatetimeIndex) |
| 65 | + assert result.tz.zone == "UTC" |
| 66 | + |
| 67 | + @pytest.mark.parametrize("sort", [None, False]) |
| 68 | + def test_datetimeindex_union_join_empty(self, sort): |
| 69 | + dti = date_range(start="1/1/2001", end="2/1/2001", freq="D") |
| 70 | + empty = Index([]) |
| 71 | + |
| 72 | + result = dti.union(empty, sort=sort) |
| 73 | + expected = dti.astype("O") |
| 74 | + tm.assert_index_equal(result, expected) |
| 75 | + |
| 76 | + result = dti.join(empty) |
| 77 | + assert isinstance(result, DatetimeIndex) |
| 78 | + tm.assert_index_equal(result, dti) |
| 79 | + |
| 80 | + def test_join_nonunique(self): |
| 81 | + idx1 = to_datetime(["2012-11-06 16:00:11.477563", "2012-11-06 16:00:11.477563"]) |
| 82 | + idx2 = to_datetime(["2012-11-06 15:11:09.006507", "2012-11-06 15:11:09.006507"]) |
| 83 | + rs = idx1.join(idx2, how="outer") |
| 84 | + assert rs.is_monotonic |
| 85 | + |
| 86 | + @pytest.mark.parametrize("freq", ["B", "C"]) |
| 87 | + def test_outer_join(self, freq): |
| 88 | + # should just behave as union |
| 89 | + start, end = datetime(2009, 1, 1), datetime(2010, 1, 1) |
| 90 | + rng = date_range(start=start, end=end, freq=freq) |
| 91 | + |
| 92 | + # overlapping |
| 93 | + left = rng[:10] |
| 94 | + right = rng[5:10] |
| 95 | + |
| 96 | + the_join = left.join(right, how="outer") |
| 97 | + assert isinstance(the_join, DatetimeIndex) |
| 98 | + |
| 99 | + # non-overlapping, gap in middle |
| 100 | + left = rng[:5] |
| 101 | + right = rng[10:] |
| 102 | + |
| 103 | + the_join = left.join(right, how="outer") |
| 104 | + assert isinstance(the_join, DatetimeIndex) |
| 105 | + assert the_join.freq is None |
| 106 | + |
| 107 | + # non-overlapping, no gap |
| 108 | + left = rng[:5] |
| 109 | + right = rng[5:10] |
| 110 | + |
| 111 | + the_join = left.join(right, how="outer") |
| 112 | + assert isinstance(the_join, DatetimeIndex) |
| 113 | + |
| 114 | + # overlapping, but different offset |
| 115 | + other = date_range(start, end, freq=BMonthEnd()) |
| 116 | + |
| 117 | + the_join = rng.join(other, how="outer") |
| 118 | + assert isinstance(the_join, DatetimeIndex) |
| 119 | + assert the_join.freq is None |
| 120 | + |
| 121 | + def test_naive_aware_conflicts(self): |
| 122 | + start, end = datetime(2009, 1, 1), datetime(2010, 1, 1) |
| 123 | + naive = date_range(start, end, freq=BDay(), tz=None) |
| 124 | + aware = date_range(start, end, freq=BDay(), tz="Asia/Hong_Kong") |
| 125 | + |
| 126 | + msg = "tz-naive.*tz-aware" |
| 127 | + with pytest.raises(TypeError, match=msg): |
| 128 | + naive.join(aware) |
| 129 | + |
| 130 | + with pytest.raises(TypeError, match=msg): |
| 131 | + aware.join(naive) |
| 132 | + |
| 133 | + @pytest.mark.parametrize("tz", [None, "US/Pacific"]) |
| 134 | + def test_join_preserves_freq(self, tz): |
| 135 | + # GH#32157 |
| 136 | + dti = date_range("2016-01-01", periods=10, tz=tz) |
| 137 | + result = dti[:5].join(dti[5:], how="outer") |
| 138 | + assert result.freq == dti.freq |
| 139 | + tm.assert_index_equal(result, dti) |
| 140 | + |
| 141 | + result = dti[:5].join(dti[6:], how="outer") |
| 142 | + assert result.freq is None |
| 143 | + expected = dti.delete(5) |
| 144 | + tm.assert_index_equal(result, expected) |
0 commit comments