|
17 | 17 | Index, |
18 | 18 | MultiIndex, |
19 | 19 | PeriodIndex, |
| 20 | + RangeIndex, |
20 | 21 | Series, |
21 | 22 | concat, |
22 | 23 | date_range, |
@@ -395,6 +396,29 @@ def test_concat_keys_with_none(self): |
395 | 396 | expected = concat([df0, df0[:2], df0[:1], df0], keys=["b", "c", "d", "e"]) |
396 | 397 | tm.assert_frame_equal(result, expected) |
397 | 398 |
|
| 399 | + @pytest.mark.parametrize("klass", [range, RangeIndex]) |
| 400 | + @pytest.mark.parametrize("include_none", [True, False]) |
| 401 | + def test_concat_preserves_rangeindex(self, klass, include_none): |
| 402 | + df = DataFrame([1, 2]) |
| 403 | + df2 = DataFrame([3, 4]) |
| 404 | + data = [df, None, df2, None] if include_none else [df, df2] |
| 405 | + keys_length = 4 if include_none else 2 |
| 406 | + result = concat(data, keys=klass(keys_length)) |
| 407 | + expected = DataFrame( |
| 408 | + [1, 2, 3, 4], |
| 409 | + index=MultiIndex( |
| 410 | + levels=( |
| 411 | + RangeIndex(start=0, stop=keys_length, step=keys_length / 2), |
| 412 | + RangeIndex(start=0, stop=2, step=1), |
| 413 | + ), |
| 414 | + codes=( |
| 415 | + np.array([0, 0, 1, 1], dtype=np.int8), |
| 416 | + np.array([0, 1, 0, 1], dtype=np.int8), |
| 417 | + ), |
| 418 | + ), |
| 419 | + ) |
| 420 | + tm.assert_frame_equal(result, expected) |
| 421 | + |
398 | 422 | def test_concat_bug_1719(self): |
399 | 423 | ts1 = Series( |
400 | 424 | np.arange(10, dtype=np.float64), index=date_range("2020-01-01", periods=10) |
@@ -705,7 +729,7 @@ def test_concat_multiindex_with_empty_rangeindex(): |
705 | 729 | # GH#41234 |
706 | 730 | mi = MultiIndex.from_tuples([("B", 1), ("C", 1)]) |
707 | 731 | df1 = DataFrame([[1, 2]], columns=mi) |
708 | | - df2 = DataFrame(index=[1], columns=pd.RangeIndex(0)) |
| 732 | + df2 = DataFrame(index=[1], columns=RangeIndex(0)) |
709 | 733 |
|
710 | 734 | result = concat([df1, df2]) |
711 | 735 | expected = DataFrame([[1, 2], [np.nan, np.nan]], columns=mi) |
@@ -830,14 +854,14 @@ def test_concat_mismatched_keys_length(): |
830 | 854 | sers = [ser + n for n in range(4)] |
831 | 855 | keys = ["A", "B", "C"] |
832 | 856 |
|
833 | | - msg = r"The behavior of pd.concat with len\(keys\) != len\(objs\) is deprecated" |
834 | | - with tm.assert_produces_warning(FutureWarning, match=msg): |
| 857 | + msg = r"The length of the keys" |
| 858 | + with pytest.raises(ValueError, match=msg): |
835 | 859 | concat(sers, keys=keys, axis=1) |
836 | | - with tm.assert_produces_warning(FutureWarning, match=msg): |
| 860 | + with pytest.raises(ValueError, match=msg): |
837 | 861 | concat(sers, keys=keys, axis=0) |
838 | | - with tm.assert_produces_warning(FutureWarning, match=msg): |
| 862 | + with pytest.raises(ValueError, match=msg): |
839 | 863 | concat((x for x in sers), keys=(y for y in keys), axis=1) |
840 | | - with tm.assert_produces_warning(FutureWarning, match=msg): |
| 864 | + with pytest.raises(ValueError, match=msg): |
841 | 865 | concat((x for x in sers), keys=(y for y in keys), axis=0) |
842 | 866 |
|
843 | 867 |
|
|
0 commit comments