|
1 | 1 | from warnings import catch_warnings
|
2 | 2 |
|
| 3 | +import datetime as dt |
3 | 4 | import dateutil
|
4 | 5 | import numpy as np
|
5 | 6 | from numpy.random import randn
|
@@ -820,11 +821,59 @@ def test_append_preserve_index_name(self):
|
820 | 821 | result = df1.append(df2)
|
821 | 822 | assert result.index.name == 'A'
|
822 | 823 |
|
| 824 | + @pytest.mark.parametrize("df_columns", [ |
| 825 | + pd.RangeIndex(3), |
| 826 | + pd.CategoricalIndex('A B C'.split()), |
| 827 | + pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]), |
| 828 | + pd.IntervalIndex.from_breaks([0, 1, 2, 3]), |
| 829 | + pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0), |
| 830 | + dt.datetime(2013, 1, 3, 6, 10), |
| 831 | + dt.datetime(2013, 1, 3, 7, 12)]), |
| 832 | + pd.Index([1, 2, 3]), |
| 833 | + ]) |
| 834 | + def test_append_same_columns_type(self, df_columns): |
| 835 | + # GH18359 |
| 836 | + |
| 837 | + # ser.index is a normal pd.Index, result from df.append(ser) should be |
| 838 | + # pd.Index (but this is not possible for IntervalIndex and MultiIndex) |
| 839 | + if not isinstance(df_columns, (pd.IntervalIndex, pd.MultiIndex)): |
| 840 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=df_columns) |
| 841 | + ser = pd.Series([7], index=['a'], name=2) |
| 842 | + result = df.append(ser) |
| 843 | + idx_diff = ser.index.difference(df_columns) |
| 844 | + combined_columns = Index(df_columns.tolist()).append(idx_diff) |
| 845 | + expected = pd.DataFrame([[1., 2., 3., np.nan], |
| 846 | + [4, 5, 6, np.nan], |
| 847 | + [np.nan, np.nan, np.nan, 7]], |
| 848 | + index=[0, 1, 2], |
| 849 | + columns=combined_columns) |
| 850 | + assert_frame_equal(result, expected) |
| 851 | + |
| 852 | + # df wider than ser |
| 853 | + df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=df_columns) |
| 854 | + ser_index = df_columns[:2] |
| 855 | + ser = pd.Series([7, 8], index=ser_index, name=2) |
| 856 | + result = df.append(ser) |
| 857 | + expected = pd.DataFrame([[1., 2., 3.], [4, 5, 6], [7, 8, np.nan]], |
| 858 | + index=[0, 1, 2], |
| 859 | + columns=df_columns) |
| 860 | + assert_frame_equal(result, expected) |
| 861 | + |
| 862 | + # ser wider than df |
| 863 | + ser_index = df_columns |
| 864 | + df_columns = df_columns[:2] |
| 865 | + df = pd.DataFrame([[1, 2], [4, 5]], columns=df_columns) |
| 866 | + ser = pd.Series([7, 8, 9], index=ser_index, name=2) |
| 867 | + result = df.append(ser) |
| 868 | + expected = pd.DataFrame([[1, 2, np.nan], [4, 5, np.nan], [7, 8, 9]], |
| 869 | + index=[0, 1, 2], |
| 870 | + columns=ser_index) |
| 871 | + assert_frame_equal(result, expected) |
| 872 | + |
823 | 873 | def test_append_dtype_coerce(self):
|
824 | 874 |
|
825 | 875 | # GH 4993
|
826 | 876 | # appending with datetime will incorrectly convert datetime64
|
827 |
| - import datetime as dt |
828 | 877 | from pandas import NaT
|
829 | 878 |
|
830 | 879 | df1 = DataFrame(index=[1, 2], data=[dt.datetime(2013, 1, 1, 0, 0),
|
|
0 commit comments