|
1 | 1 | from warnings import catch_warnings |
| 2 | +from itertools import combinations |
2 | 3 |
|
3 | 4 | import datetime as dt |
4 | 5 | import dateutil |
@@ -855,37 +856,38 @@ 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) |
879 | | - 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) |
| 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) |
889 | 891 |
|
890 | 892 | def test_append_dtype_coerce(self): |
891 | 893 |
|
|
0 commit comments