|
1 | 1 | from warnings import catch_warnings
|
| 2 | +from itertools import combinations, product |
2 | 3 |
|
3 | 4 | import datetime as dt
|
4 | 5 | import dateutil
|
@@ -855,37 +856,92 @@ def test_append_same_columns_type(self, df_columns):
|
855 | 856 | columns=ser_index)
|
856 | 857 | assert_frame_equal(result, expected)
|
857 | 858 |
|
858 |
| - @pytest.mark.parametrize("df_columns", [ |
| 859 | + @pytest.mark.parametrize("df_columns, series_index", combinations([ |
859 | 860 | pd.RangeIndex(3),
|
860 | 861 | pd.CategoricalIndex('A B C'.split()),
|
861 | 862 | pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]),
|
862 | 863 | pd.IntervalIndex.from_breaks([0, 1, 2, 3]),
|
863 | 864 | pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
|
864 | 865 | dt.datetime(2013, 1, 3, 6, 10),
|
865 | 866 | dt.datetime(2013, 1, 3, 7, 12)]),
|
866 |
| - pd.Index([1, 2, 3]), |
867 |
| - ]) |
868 |
| - def test_append_different_columns_types(self, df_columns): |
| 867 | + pd.Index([4, 5, 6]), |
| 868 | + ], r=2)) |
| 869 | + def test_append_different_columns_types(self, df_columns, series_index): |
869 | 870 | # GH18359
|
870 | 871 |
|
871 |
| - # ser.index is a normal pd.Index, so result from df.append(ser) should |
872 |
| - # be pd.Index (but this is not possible for IntervalIndex and |
873 |
| - # MultiIndex) |
874 | 872 | df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=df_columns)
|
875 |
| - ser = pd.Series([7], index=['a'], name=2) |
876 |
| - if isinstance(df_columns, (pd.IntervalIndex, pd.MultiIndex)): |
877 |
| - with pytest.raises(TypeError): |
| 873 | + ser = pd.Series([7, 8, 9], index=series_index, name=2) |
| 874 | + |
| 875 | + # MultiIndex and IntervalIndex will raise if appended or appended to |
| 876 | + # a different type |
| 877 | + if (isinstance(df_columns, (pd.IntervalIndex, pd.MultiIndex)) or |
| 878 | + isinstance(series_index, (pd.IntervalIndex, pd.MultiIndex))): |
| 879 | + with pytest.raises((ValueError, TypeError)): |
878 | 880 | df.append(ser)
|
| 881 | + return |
| 882 | + result = df.append(ser) |
| 883 | + idx_diff = ser.index.difference(df_columns) |
| 884 | + combined_columns = Index(df_columns.tolist()).append(idx_diff) |
| 885 | + expected = pd.DataFrame([[1., 2., 3., np.nan, np.nan, np.nan], |
| 886 | + [4, 5, 6, np.nan, np.nan, np.nan], |
| 887 | + [np.nan, np.nan, np.nan, 7, 8, 9]], |
| 888 | + index=[0, 1, 2], |
| 889 | + columns=combined_columns) |
| 890 | + assert_frame_equal(result, expected) |
| 891 | + |
| 892 | + @pytest.mark.parametrize("other_type", [ |
| 893 | + pd.RangeIndex(3), |
| 894 | + pd.CategoricalIndex('A B C'.split()), |
| 895 | + pd.IntervalIndex.from_breaks([0, 1, 2, 3]), |
| 896 | + pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0), |
| 897 | + dt.datetime(2013, 1, 3, 6, 10), |
| 898 | + dt.datetime(2013, 1, 3, 7, 12)]), |
| 899 | + pd.Index([4, 5, 6]), |
| 900 | + ]) |
| 901 | + def test_append_multi_index_raises(self, other_type): |
| 902 | + # GH18359 |
| 903 | + # .append will raise if MultiIndex appends or is appended to a |
| 904 | + # different index type |
| 905 | + |
| 906 | + mi = pd.MultiIndex.from_arrays( ['A B C'.split(), 'D E F'.split()]) |
| 907 | + |
| 908 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=mi) |
| 909 | + ser = pd.Series([7, 8, 9], index=other_type, name=2) |
| 910 | + if isinstance(other_type, pd.IntervalIndex): |
| 911 | + pytest.raises(ValueError, df.append, ser) |
879 | 912 | else:
|
880 |
| - result = df.append(ser) |
881 |
| - idx_diff = ser.index.difference(df_columns) |
882 |
| - combined_columns = Index(df_columns.tolist()).append(idx_diff) |
883 |
| - expected = pd.DataFrame([[1., 2., 3., np.nan], |
884 |
| - [4, 5, 6, np.nan], |
885 |
| - [np.nan, np.nan, np.nan, 7]], |
886 |
| - index=[0, 1, 2], |
887 |
| - columns=combined_columns) |
888 |
| - assert_frame_equal(result, expected) |
| 913 | + pytest.raises(TypeError, df.append, ser) |
| 914 | + |
| 915 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type) |
| 916 | + ser = pd.Series([7, 8, 9], index=mi, name=2) |
| 917 | + with pytest.raises(TypeError): |
| 918 | + df.append(ser) |
| 919 | + |
| 920 | + @pytest.mark.parametrize("other_type", [ |
| 921 | + pd.RangeIndex(3), |
| 922 | + pd.CategoricalIndex('A B C'.split()), |
| 923 | + pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]), |
| 924 | + pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0), |
| 925 | + dt.datetime(2013, 1, 3, 6, 10), |
| 926 | + dt.datetime(2013, 1, 3, 7, 12)]), |
| 927 | + pd.Index([4, 5, 6]), |
| 928 | + ]) |
| 929 | + def test_append_interval_index_raises(self, other_type): |
| 930 | + # GH18359 |
| 931 | + # .append will raise if IntervalIndex appends or is appended to a |
| 932 | + # different index type |
| 933 | + |
| 934 | + ii = pd.IntervalIndex.from_breaks([0, 1, 2, 3]) |
| 935 | + |
| 936 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=ii) |
| 937 | + ser = pd.Series([7, 8, 9], index=other_type, name=2) |
| 938 | + with pytest.raises(TypeError): |
| 939 | + df.append(ser) |
| 940 | + |
| 941 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type) |
| 942 | + ser = pd.Series([7, 8, 9], index=ii, name=2) |
| 943 | + with pytest.raises(ValueError): |
| 944 | + df.append(ser) |
889 | 945 |
|
890 | 946 | def test_append_dtype_coerce(self):
|
891 | 947 |
|
|
0 commit comments