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,12 +821,76 @@ 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
+ # df wider than ser
838
+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
839
+ ser_index = df_columns [:2 ]
840
+ ser = pd .Series ([7 , 8 ], index = ser_index , name = 2 )
841
+ result = df .append (ser )
842
+ expected = pd .DataFrame ([[1. , 2. , 3. ], [4 , 5 , 6 ], [7 , 8 , np .nan ]],
843
+ index = [0 , 1 , 2 ],
844
+ columns = df_columns )
845
+ assert_frame_equal (result , expected )
846
+
847
+ # ser wider than df
848
+ ser_index = df_columns
849
+ df_columns = df_columns [:2 ]
850
+ df = pd .DataFrame ([[1 , 2 ], [4 , 5 ]], columns = df_columns )
851
+ ser = pd .Series ([7 , 8 , 9 ], index = ser_index , name = 2 )
852
+ result = df .append (ser )
853
+ expected = pd .DataFrame ([[1 , 2 , np .nan ], [4 , 5 , np .nan ], [7 , 8 , 9 ]],
854
+ index = [0 , 1 , 2 ],
855
+ columns = ser_index )
856
+ assert_frame_equal (result , expected )
857
+
858
+ @pytest .mark .parametrize ("df_columns" , [
859
+ pd .RangeIndex (3 ),
860
+ pd .CategoricalIndex ('A B C' .split ()),
861
+ pd .MultiIndex .from_arrays (['A B C' .split (), 'D E F' .split ()]),
862
+ pd .IntervalIndex .from_breaks ([0 , 1 , 2 , 3 ]),
863
+ pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
864
+ dt .datetime (2013 , 1 , 3 , 6 , 10 ),
865
+ dt .datetime (2013 , 1 , 3 , 7 , 12 )]),
866
+ pd .Index ([1 , 2 , 3 ]),
867
+ ])
868
+ def test_append_different_columns_types (self , df_columns ):
869
+ # GH18359
870
+
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
+ 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 ):
878
+ 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 )
889
+
823
890
def test_append_dtype_coerce (self ):
824
891
825
892
# GH 4993
826
893
# appending with datetime will incorrectly convert datetime64
827
- import datetime as dt
828
- from pandas import NaT
829
894
830
895
df1 = DataFrame (index = [1 , 2 ], data = [dt .datetime (2013 , 1 , 1 , 0 , 0 ),
831
896
dt .datetime (2013 , 1 , 2 , 0 , 0 )],
@@ -836,7 +901,9 @@ def test_append_dtype_coerce(self):
836
901
dt .datetime (2013 , 1 , 4 , 7 , 10 )]],
837
902
columns = ['start_time' , 'end_time' ])
838
903
839
- expected = concat ([Series ([NaT , NaT , dt .datetime (2013 , 1 , 3 , 6 , 10 ),
904
+ expected = concat ([Series ([pd .NaT ,
905
+ pd .NaT ,
906
+ dt .datetime (2013 , 1 , 3 , 6 , 10 ),
840
907
dt .datetime (2013 , 1 , 4 , 7 , 10 )],
841
908
name = 'end_time' ),
842
909
Series ([dt .datetime (2013 , 1 , 1 , 0 , 0 ),
0 commit comments