|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas import DataFrame, Index, Series |
| 6 | +import pandas._testing as tm |
| 7 | + |
| 8 | + |
| 9 | +class TestDataFrameAlign: |
| 10 | + def test_align_float(self, float_frame): |
| 11 | + af, bf = float_frame.align(float_frame) |
| 12 | + assert af._data is not float_frame._data |
| 13 | + |
| 14 | + af, bf = float_frame.align(float_frame, copy=False) |
| 15 | + assert af._data is float_frame._data |
| 16 | + |
| 17 | + # axis = 0 |
| 18 | + other = float_frame.iloc[:-5, :3] |
| 19 | + af, bf = float_frame.align(other, axis=0, fill_value=-1) |
| 20 | + |
| 21 | + tm.assert_index_equal(bf.columns, other.columns) |
| 22 | + |
| 23 | + # test fill value |
| 24 | + join_idx = float_frame.index.join(other.index) |
| 25 | + diff_a = float_frame.index.difference(join_idx) |
| 26 | + diff_b = other.index.difference(join_idx) |
| 27 | + diff_a_vals = af.reindex(diff_a).values |
| 28 | + diff_b_vals = bf.reindex(diff_b).values |
| 29 | + assert (diff_a_vals == -1).all() |
| 30 | + |
| 31 | + af, bf = float_frame.align(other, join="right", axis=0) |
| 32 | + tm.assert_index_equal(bf.columns, other.columns) |
| 33 | + tm.assert_index_equal(bf.index, other.index) |
| 34 | + tm.assert_index_equal(af.index, other.index) |
| 35 | + |
| 36 | + # axis = 1 |
| 37 | + other = float_frame.iloc[:-5, :3].copy() |
| 38 | + af, bf = float_frame.align(other, axis=1) |
| 39 | + tm.assert_index_equal(bf.columns, float_frame.columns) |
| 40 | + tm.assert_index_equal(bf.index, other.index) |
| 41 | + |
| 42 | + # test fill value |
| 43 | + join_idx = float_frame.index.join(other.index) |
| 44 | + diff_a = float_frame.index.difference(join_idx) |
| 45 | + diff_b = other.index.difference(join_idx) |
| 46 | + diff_a_vals = af.reindex(diff_a).values |
| 47 | + |
| 48 | + # TODO(wesm): unused? |
| 49 | + diff_b_vals = bf.reindex(diff_b).values # noqa |
| 50 | + |
| 51 | + assert (diff_a_vals == -1).all() |
| 52 | + |
| 53 | + af, bf = float_frame.align(other, join="inner", axis=1) |
| 54 | + tm.assert_index_equal(bf.columns, other.columns) |
| 55 | + |
| 56 | + af, bf = float_frame.align(other, join="inner", axis=1, method="pad") |
| 57 | + tm.assert_index_equal(bf.columns, other.columns) |
| 58 | + |
| 59 | + af, bf = float_frame.align( |
| 60 | + other.iloc[:, 0], join="inner", axis=1, method=None, fill_value=None |
| 61 | + ) |
| 62 | + tm.assert_index_equal(bf.index, Index([])) |
| 63 | + |
| 64 | + af, bf = float_frame.align( |
| 65 | + other.iloc[:, 0], join="inner", axis=1, method=None, fill_value=0 |
| 66 | + ) |
| 67 | + tm.assert_index_equal(bf.index, Index([])) |
| 68 | + |
| 69 | + # Try to align DataFrame to Series along bad axis |
| 70 | + msg = "No axis named 2 for object type DataFrame" |
| 71 | + with pytest.raises(ValueError, match=msg): |
| 72 | + float_frame.align(af.iloc[0, :3], join="inner", axis=2) |
| 73 | + |
| 74 | + # align dataframe to series with broadcast or not |
| 75 | + idx = float_frame.index |
| 76 | + s = Series(range(len(idx)), index=idx) |
| 77 | + |
| 78 | + left, right = float_frame.align(s, axis=0) |
| 79 | + tm.assert_index_equal(left.index, float_frame.index) |
| 80 | + tm.assert_index_equal(right.index, float_frame.index) |
| 81 | + assert isinstance(right, Series) |
| 82 | + |
| 83 | + left, right = float_frame.align(s, broadcast_axis=1) |
| 84 | + tm.assert_index_equal(left.index, float_frame.index) |
| 85 | + expected = {c: s for c in float_frame.columns} |
| 86 | + expected = DataFrame( |
| 87 | + expected, index=float_frame.index, columns=float_frame.columns |
| 88 | + ) |
| 89 | + tm.assert_frame_equal(right, expected) |
| 90 | + |
| 91 | + # see gh-9558 |
| 92 | + df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) |
| 93 | + result = df[df["a"] == 2] |
| 94 | + expected = DataFrame([[2, 5]], index=[1], columns=["a", "b"]) |
| 95 | + tm.assert_frame_equal(result, expected) |
| 96 | + |
| 97 | + result = df.where(df["a"] == 2, 0) |
| 98 | + expected = DataFrame({"a": [0, 2, 0], "b": [0, 5, 0]}) |
| 99 | + tm.assert_frame_equal(result, expected) |
| 100 | + |
| 101 | + def test_align_int(self, int_frame): |
| 102 | + # test other non-float types |
| 103 | + other = DataFrame(index=range(5), columns=["A", "B", "C"]) |
| 104 | + |
| 105 | + af, bf = int_frame.align(other, join="inner", axis=1, method="pad") |
| 106 | + tm.assert_index_equal(bf.columns, other.columns) |
| 107 | + |
| 108 | + def test_align_mixed_type(self, float_string_frame): |
| 109 | + |
| 110 | + af, bf = float_string_frame.align( |
| 111 | + float_string_frame, join="inner", axis=1, method="pad" |
| 112 | + ) |
| 113 | + tm.assert_index_equal(bf.columns, float_string_frame.columns) |
| 114 | + |
| 115 | + def test_align_mixed_float(self, mixed_float_frame): |
| 116 | + # mixed floats/ints |
| 117 | + other = DataFrame(index=range(5), columns=["A", "B", "C"]) |
| 118 | + |
| 119 | + af, bf = mixed_float_frame.align( |
| 120 | + other.iloc[:, 0], join="inner", axis=1, method=None, fill_value=0 |
| 121 | + ) |
| 122 | + tm.assert_index_equal(bf.index, Index([])) |
| 123 | + |
| 124 | + def test_align_mixed_int(self, mixed_int_frame): |
| 125 | + other = DataFrame(index=range(5), columns=["A", "B", "C"]) |
| 126 | + |
| 127 | + af, bf = mixed_int_frame.align( |
| 128 | + other.iloc[:, 0], join="inner", axis=1, method=None, fill_value=0 |
| 129 | + ) |
| 130 | + tm.assert_index_equal(bf.index, Index([])) |
| 131 | + |
| 132 | + def test_align_multiindex(self): |
| 133 | + # GH#10665 |
| 134 | + # same test cases as test_align_multiindex in test_series.py |
| 135 | + |
| 136 | + midx = pd.MultiIndex.from_product( |
| 137 | + [range(2), range(3), range(2)], names=("a", "b", "c") |
| 138 | + ) |
| 139 | + idx = pd.Index(range(2), name="b") |
| 140 | + df1 = pd.DataFrame(np.arange(12, dtype="int64"), index=midx) |
| 141 | + df2 = pd.DataFrame(np.arange(2, dtype="int64"), index=idx) |
| 142 | + |
| 143 | + # these must be the same results (but flipped) |
| 144 | + res1l, res1r = df1.align(df2, join="left") |
| 145 | + res2l, res2r = df2.align(df1, join="right") |
| 146 | + |
| 147 | + expl = df1 |
| 148 | + tm.assert_frame_equal(expl, res1l) |
| 149 | + tm.assert_frame_equal(expl, res2r) |
| 150 | + expr = pd.DataFrame([0, 0, 1, 1, np.nan, np.nan] * 2, index=midx) |
| 151 | + tm.assert_frame_equal(expr, res1r) |
| 152 | + tm.assert_frame_equal(expr, res2l) |
| 153 | + |
| 154 | + res1l, res1r = df1.align(df2, join="right") |
| 155 | + res2l, res2r = df2.align(df1, join="left") |
| 156 | + |
| 157 | + exp_idx = pd.MultiIndex.from_product( |
| 158 | + [range(2), range(2), range(2)], names=("a", "b", "c") |
| 159 | + ) |
| 160 | + expl = pd.DataFrame([0, 1, 2, 3, 6, 7, 8, 9], index=exp_idx) |
| 161 | + tm.assert_frame_equal(expl, res1l) |
| 162 | + tm.assert_frame_equal(expl, res2r) |
| 163 | + expr = pd.DataFrame([0, 0, 1, 1] * 2, index=exp_idx) |
| 164 | + tm.assert_frame_equal(expr, res1r) |
| 165 | + tm.assert_frame_equal(expr, res2l) |
| 166 | + |
| 167 | + def test_align_series_combinations(self): |
| 168 | + df = pd.DataFrame({"a": [1, 3, 5], "b": [1, 3, 5]}, index=list("ACE")) |
| 169 | + s = pd.Series([1, 2, 4], index=list("ABD"), name="x") |
| 170 | + |
| 171 | + # frame + series |
| 172 | + res1, res2 = df.align(s, axis=0) |
| 173 | + exp1 = pd.DataFrame( |
| 174 | + {"a": [1, np.nan, 3, np.nan, 5], "b": [1, np.nan, 3, np.nan, 5]}, |
| 175 | + index=list("ABCDE"), |
| 176 | + ) |
| 177 | + exp2 = pd.Series([1, 2, np.nan, 4, np.nan], index=list("ABCDE"), name="x") |
| 178 | + |
| 179 | + tm.assert_frame_equal(res1, exp1) |
| 180 | + tm.assert_series_equal(res2, exp2) |
| 181 | + |
| 182 | + # series + frame |
| 183 | + res1, res2 = s.align(df) |
| 184 | + tm.assert_series_equal(res1, exp2) |
| 185 | + tm.assert_frame_equal(res2, exp1) |
| 186 | + |
| 187 | + def _check_align(self, a, b, axis, fill_axis, how, method, limit=None): |
| 188 | + aa, ab = a.align( |
| 189 | + b, axis=axis, join=how, method=method, limit=limit, fill_axis=fill_axis |
| 190 | + ) |
| 191 | + |
| 192 | + join_index, join_columns = None, None |
| 193 | + |
| 194 | + ea, eb = a, b |
| 195 | + if axis is None or axis == 0: |
| 196 | + join_index = a.index.join(b.index, how=how) |
| 197 | + ea = ea.reindex(index=join_index) |
| 198 | + eb = eb.reindex(index=join_index) |
| 199 | + |
| 200 | + if axis is None or axis == 1: |
| 201 | + join_columns = a.columns.join(b.columns, how=how) |
| 202 | + ea = ea.reindex(columns=join_columns) |
| 203 | + eb = eb.reindex(columns=join_columns) |
| 204 | + |
| 205 | + ea = ea.fillna(axis=fill_axis, method=method, limit=limit) |
| 206 | + eb = eb.fillna(axis=fill_axis, method=method, limit=limit) |
| 207 | + |
| 208 | + tm.assert_frame_equal(aa, ea) |
| 209 | + tm.assert_frame_equal(ab, eb) |
| 210 | + |
| 211 | + @pytest.mark.parametrize("meth", ["pad", "bfill"]) |
| 212 | + @pytest.mark.parametrize("ax", [0, 1, None]) |
| 213 | + @pytest.mark.parametrize("fax", [0, 1]) |
| 214 | + @pytest.mark.parametrize("how", ["inner", "outer", "left", "right"]) |
| 215 | + def test_align_fill_method(self, how, meth, ax, fax, float_frame): |
| 216 | + df = float_frame |
| 217 | + self._check_align_fill(df, how, meth, ax, fax) |
| 218 | + |
| 219 | + def _check_align_fill(self, frame, kind, meth, ax, fax): |
| 220 | + left = frame.iloc[0:4, :10] |
| 221 | + right = frame.iloc[2:, 6:] |
| 222 | + empty = frame.iloc[:0, :0] |
| 223 | + |
| 224 | + self._check_align(left, right, axis=ax, fill_axis=fax, how=kind, method=meth) |
| 225 | + self._check_align( |
| 226 | + left, right, axis=ax, fill_axis=fax, how=kind, method=meth, limit=1 |
| 227 | + ) |
| 228 | + |
| 229 | + # empty left |
| 230 | + self._check_align(empty, right, axis=ax, fill_axis=fax, how=kind, method=meth) |
| 231 | + self._check_align( |
| 232 | + empty, right, axis=ax, fill_axis=fax, how=kind, method=meth, limit=1 |
| 233 | + ) |
| 234 | + |
| 235 | + # empty right |
| 236 | + self._check_align(left, empty, axis=ax, fill_axis=fax, how=kind, method=meth) |
| 237 | + self._check_align( |
| 238 | + left, empty, axis=ax, fill_axis=fax, how=kind, method=meth, limit=1 |
| 239 | + ) |
| 240 | + |
| 241 | + # both empty |
| 242 | + self._check_align(empty, empty, axis=ax, fill_axis=fax, how=kind, method=meth) |
| 243 | + self._check_align( |
| 244 | + empty, empty, axis=ax, fill_axis=fax, how=kind, method=meth, limit=1 |
| 245 | + ) |
0 commit comments