@@ -831,56 +831,62 @@ def test_append_preserve_index_name(self):
831
831
result = df1 .append (df2 )
832
832
assert result .index .name == 'A'
833
833
834
- @ pytest . mark . parametrize ( "df_columns" , [
834
+ indexes_I = [
835
835
pd .RangeIndex (3 ),
836
- pd .Index ([1 , 2 , 3 ]),
836
+ pd .Index ([4 , 5 , 6 ]),
837
+ pd .Index ([4.5 , 5.5 , 6.5 ]),
837
838
pd .Index (list ('abc' )),
838
839
pd .CategoricalIndex ('A B C' .split ()),
839
- pd .CategoricalIndex ('A B C' .split (), ordered = True ),
840
- pd .MultiIndex .from_arrays (['A B C' .split (), 'D E F' .split ()]),
841
- pd .IntervalIndex .from_breaks ([0 , 1 , 2 , 3 ]),
842
840
pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
843
841
dt .datetime (2013 , 1 , 3 , 6 , 10 ),
844
842
dt .datetime (2013 , 1 , 3 , 7 , 12 )]),
845
- ], ids = lambda x : str (x .dtype ))
846
- def test_append_same_columns_type (self , df_columns ):
843
+ ]
844
+ indexes_II = indexes_I + [pd .CategoricalIndex ('A B C' .split (),
845
+ ordered = True )]
846
+ indexes_III = [
847
+ pd .IntervalIndex .from_breaks ([0 , 1 , 2 , 3 ]),
848
+ pd .MultiIndex .from_arrays (['A B C' .split (), 'D E F' .split ()]),
849
+ ]
850
+
851
+ @pytest .fixture (params = indexes_II )
852
+ def index_II (self , request ):
853
+ return request .param
854
+
855
+ @pytest .fixture (params = indexes_III )
856
+ def index_III (self , request ):
857
+ return request .param
858
+
859
+ def test_append_same_columns_type (self , index_II ):
847
860
# GH18359
848
861
849
862
# df wider than ser
850
- df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
851
- ser_index = df_columns [:2 ]
863
+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = index_II )
864
+ ser_index = index_II [:2 ]
852
865
ser = pd .Series ([7 , 8 ], index = ser_index , name = 2 )
853
866
result = df .append (ser )
854
867
expected = pd .DataFrame ([[1. , 2. , 3. ], [4 , 5 , 6 ], [7 , 8 , np .nan ]],
855
868
index = [0 , 1 , 2 ],
856
- columns = df_columns )
869
+ columns = index_II )
857
870
assert_frame_equal (result , expected )
858
871
859
872
# ser wider than df
860
- ser_index = df_columns
861
- df_columns = df_columns [:2 ]
862
- df = pd .DataFrame ([[1 , 2 ], [4 , 5 ]], columns = df_columns )
873
+ ser_index = index_II
874
+ index_II = index_II [:2 ]
875
+ df = pd .DataFrame ([[1 , 2 ], [4 , 5 ]], columns = index_II )
863
876
ser = pd .Series ([7 , 8 , 9 ], index = ser_index , name = 2 )
864
877
result = df .append (ser )
865
878
expected = pd .DataFrame ([[1 , 2 , np .nan ], [4 , 5 , np .nan ], [7 , 8 , 9 ]],
866
879
index = [0 , 1 , 2 ],
867
880
columns = ser_index )
868
881
assert_frame_equal (result , expected )
869
882
870
- @pytest .mark .parametrize ("df_columns, series_index" , combinations ([
871
- pd .RangeIndex (3 ),
872
- pd .Index ([4 , 5 , 6 ]),
873
- pd .Index ([7.5 , 8.5 , 9.5 ]),
874
- pd .Index (list ('abc' )),
875
- pd .CategoricalIndex ('A B C' .split (), ordered = True ),
876
- # pd.CategoricalIndex('A B C'.split()),
877
- pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
878
- dt .datetime (2013 , 1 , 3 , 6 , 10 ),
879
- dt .datetime (2013 , 1 , 3 , 7 , 12 )]),
880
- ], r = 2 ), ids = lambda x : str (x .dtype ))
883
+ @pytest .mark .parametrize ("df_columns, series_index" ,
884
+ combinations (indexes_I , r = 2 ),
885
+ ids = lambda x : str (x .dtype ))
881
886
def test_append_different_columns_types (self , df_columns , series_index ):
882
887
# GH18359
883
888
# see also test 'test_append_different_columns_types_raises' below
889
+ # for errors raised when appending
884
890
885
891
df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
886
892
ser = pd .Series ([7 , 8 , 9 ], index = series_index , name = 2 )
@@ -895,42 +901,21 @@ def test_append_different_columns_types(self, df_columns, series_index):
895
901
columns = combined_columns )
896
902
assert_frame_equal (result , expected )
897
903
898
- @pytest .mark .parametrize ("this_type" , [
899
- pd .IntervalIndex .from_breaks ([0 , 1 , 2 , 3 ]),
900
- pd .MultiIndex .from_arrays (['A B C' .split (), 'D E F' .split ()]),
901
- ])
902
- @pytest .mark .parametrize ("other_type" , [
903
- pd .RangeIndex (3 ),
904
- pd .Index ([4 , 5 , 6 ]),
905
- pd .Index (list ("abc" )),
906
- pd .CategoricalIndex ('A B C' .split ()),
907
- pd .CategoricalIndex ('A B C' .split (), ordered = True ),
908
- pd .IntervalIndex .from_breaks ([0 , 1 , 2 , 3 ]),
909
- pd .MultiIndex .from_arrays (['A B C' .split (), 'D E F' .split ()]),
910
- pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
911
- dt .datetime (2013 , 1 , 3 , 6 , 10 ),
912
- dt .datetime (2013 , 1 , 3 , 7 , 12 )]),
913
- ], ids = lambda x : str (x .dtype ))
914
- def test_append_different_columns_types_raises (self ,
915
- this_type , other_type ):
904
+ def test_append_different_columns_types_raises (self , index_II , index_III ):
916
905
# GH18359
917
906
# .append will raise if IntervalIndex/MultiIndex appends or is
918
907
# appended to a different index type
919
908
#
920
909
# see also test 'test_append_different_columns_types' above for
921
910
# appending without raising.
922
911
923
- if type (this_type ) is type (other_type ):
924
- # don't test same type
925
- return
926
-
927
- df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = this_type )
928
- ser = pd .Series ([7 , 8 , 9 ], index = other_type , name = 2 )
912
+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = index_II )
913
+ ser = pd .Series ([7 , 8 , 9 ], index = index_III , name = 2 )
929
914
with pytest .raises (TypeError ):
930
915
df .append (ser )
931
916
932
- df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = other_type )
933
- ser = pd .Series ([7 , 8 , 9 ], index = this_type , name = 2 )
917
+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = index_III )
918
+ ser = pd .Series ([7 , 8 , 9 ], index = index_II , name = 2 )
934
919
with pytest .raises (TypeError ):
935
920
df .append (ser )
936
921
0 commit comments