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
@@ -829,12 +830,76 @@ def test_append_preserve_index_name(self):
829
830
result = df1 .append (df2 )
830
831
assert result .index .name == 'A'
831
832
833
+ @pytest .mark .parametrize ("df_columns" , [
834
+ pd .RangeIndex (3 ),
835
+ pd .CategoricalIndex ('A B C' .split ()),
836
+ pd .MultiIndex .from_arrays (['A B C' .split (), 'D E F' .split ()]),
837
+ pd .IntervalIndex .from_breaks ([0 , 1 , 2 , 3 ]),
838
+ pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
839
+ dt .datetime (2013 , 1 , 3 , 6 , 10 ),
840
+ dt .datetime (2013 , 1 , 3 , 7 , 12 )]),
841
+ pd .Index ([1 , 2 , 3 ]),
842
+ ])
843
+ def test_append_same_columns_type (self , df_columns ):
844
+ # GH18359
845
+
846
+ # df wider than ser
847
+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
848
+ ser_index = df_columns [:2 ]
849
+ ser = pd .Series ([7 , 8 ], index = ser_index , name = 2 )
850
+ result = df .append (ser )
851
+ expected = pd .DataFrame ([[1. , 2. , 3. ], [4 , 5 , 6 ], [7 , 8 , np .nan ]],
852
+ index = [0 , 1 , 2 ],
853
+ columns = df_columns )
854
+ assert_frame_equal (result , expected )
855
+
856
+ # ser wider than df
857
+ ser_index = df_columns
858
+ df_columns = df_columns [:2 ]
859
+ df = pd .DataFrame ([[1 , 2 ], [4 , 5 ]], columns = df_columns )
860
+ ser = pd .Series ([7 , 8 , 9 ], index = ser_index , name = 2 )
861
+ result = df .append (ser )
862
+ expected = pd .DataFrame ([[1 , 2 , np .nan ], [4 , 5 , np .nan ], [7 , 8 , 9 ]],
863
+ index = [0 , 1 , 2 ],
864
+ columns = ser_index )
865
+ assert_frame_equal (result , expected )
866
+
867
+ @pytest .mark .parametrize ("df_columns" , [
868
+ pd .RangeIndex (3 ),
869
+ pd .CategoricalIndex ('A B C' .split ()),
870
+ pd .MultiIndex .from_arrays (['A B C' .split (), 'D E F' .split ()]),
871
+ pd .IntervalIndex .from_breaks ([0 , 1 , 2 , 3 ]),
872
+ pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
873
+ dt .datetime (2013 , 1 , 3 , 6 , 10 ),
874
+ dt .datetime (2013 , 1 , 3 , 7 , 12 )]),
875
+ pd .Index ([1 , 2 , 3 ]),
876
+ ])
877
+ def test_append_different_columns_types (self , df_columns ):
878
+ # GH18359
879
+
880
+ # ser.index is a normal pd.Index, so result from df.append(ser) should
881
+ # be pd.Index (but this is not possible for IntervalIndex and
882
+ # MultiIndex)
883
+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
884
+ ser = pd .Series ([7 ], index = ['a' ], name = 2 )
885
+ if isinstance (df_columns , (pd .IntervalIndex , pd .MultiIndex )):
886
+ with pytest .raises (TypeError ):
887
+ df .append (ser )
888
+ else :
889
+ result = df .append (ser )
890
+ idx_diff = ser .index .difference (df_columns )
891
+ combined_columns = Index (df_columns .tolist ()).append (idx_diff )
892
+ expected = pd .DataFrame ([[1. , 2. , 3. , np .nan ],
893
+ [4 , 5 , 6 , np .nan ],
894
+ [np .nan , np .nan , np .nan , 7 ]],
895
+ index = [0 , 1 , 2 ],
896
+ columns = combined_columns )
897
+ assert_frame_equal (result , expected )
898
+
832
899
def test_append_dtype_coerce (self ):
833
900
834
901
# GH 4993
835
902
# appending with datetime will incorrectly convert datetime64
836
- import datetime as dt
837
- from pandas import NaT
838
903
839
904
df1 = DataFrame (index = [1 , 2 ], data = [dt .datetime (2013 , 1 , 1 , 0 , 0 ),
840
905
dt .datetime (2013 , 1 , 2 , 0 , 0 )],
@@ -845,7 +910,9 @@ def test_append_dtype_coerce(self):
845
910
dt .datetime (2013 , 1 , 4 , 7 , 10 )]],
846
911
columns = ['start_time' , 'end_time' ])
847
912
848
- expected = concat ([Series ([NaT , NaT , dt .datetime (2013 , 1 , 3 , 6 , 10 ),
913
+ expected = concat ([Series ([pd .NaT ,
914
+ pd .NaT ,
915
+ dt .datetime (2013 , 1 , 3 , 6 , 10 ),
849
916
dt .datetime (2013 , 1 , 4 , 7 , 10 )],
850
917
name = 'end_time' ),
851
918
Series ([dt .datetime (2013 , 1 , 1 , 0 , 0 ),
0 commit comments