11from warnings import catch_warnings
2- from itertools import combinations
2+ from itertools import combinations , product
33
44import datetime as dt
55import dateutil
@@ -831,56 +831,59 @@ def test_append_preserve_index_name(self):
831831 result = df1 .append (df2 )
832832 assert result .index .name == 'A'
833833
834- @ pytest . mark . parametrize ( "df_columns" , [
834+ indexes_can_append = [
835835 pd .RangeIndex (3 ),
836- pd .Index ([1 , 2 , 3 ]),
836+ pd .Index ([4 , 5 , 6 ]),
837+ pd .Index ([4.5 , 5.5 , 6.5 ]),
837838 pd .Index (list ('abc' )),
838839 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 ]),
840+ pd .CategoricalIndex ('D E F' .split (), ordered = True ),
842841 pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
843842 dt .datetime (2013 , 1 , 3 , 6 , 10 ),
844843 dt .datetime (2013 , 1 , 3 , 7 , 12 )]),
845- ], ids = lambda x : str (x .dtype ))
846- def test_append_same_columns_type (self , df_columns ):
844+ ]
845+
846+ indexes_cannot_append_with_other = [
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+ all_indexes = indexes_can_append + indexes_cannot_append_with_other
852+
853+ @pytest .mark .parametrize ("index" ,
854+ all_indexes ,
855+ ids = lambda x : x .__class__ .__name__ )
856+ def test_append_same_columns_type (self , index ):
847857 # GH18359
848858
849859 # df wider than ser
850- df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
851- ser_index = df_columns [:2 ]
860+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = index )
861+ ser_index = index [:2 ]
852862 ser = pd .Series ([7 , 8 ], index = ser_index , name = 2 )
853863 result = df .append (ser )
854864 expected = pd .DataFrame ([[1. , 2. , 3. ], [4 , 5 , 6 ], [7 , 8 , np .nan ]],
855865 index = [0 , 1 , 2 ],
856- columns = df_columns )
866+ columns = index )
857867 assert_frame_equal (result , expected )
858868
859869 # 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 )
870+ ser_index = index
871+ index = index [:2 ]
872+ df = pd .DataFrame ([[1 , 2 ], [4 , 5 ]], columns = index )
863873 ser = pd .Series ([7 , 8 , 9 ], index = ser_index , name = 2 )
864874 result = df .append (ser )
865875 expected = pd .DataFrame ([[1 , 2 , np .nan ], [4 , 5 , np .nan ], [7 , 8 , 9 ]],
866876 index = [0 , 1 , 2 ],
867877 columns = ser_index )
868878 assert_frame_equal (result , expected )
869879
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 ))
880+ @pytest .mark .parametrize ("df_columns, series_index" ,
881+ combinations (indexes_can_append , r = 2 ),
882+ ids = lambda x : x .__class__ .__name__ )
881883 def test_append_different_columns_types (self , df_columns , series_index ):
882884 # GH18359
883- # see also test 'test_append_different_columns_types_raises' below
885+ # See also test 'test_append_different_columns_types_raises' below
886+ # for errors raised when appending
884887
885888 df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
886889 ser = pd .Series ([7 , 8 , 9 ], index = series_index , name = 2 )
@@ -895,42 +898,28 @@ def test_append_different_columns_types(self, df_columns, series_index):
895898 columns = combined_columns )
896899 assert_frame_equal (result , expected )
897900
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 ):
901+ @pytest .mark .parametrize (
902+ "index_can_append, index_cannot_append_with_other" ,
903+ product (indexes_can_append , indexes_cannot_append_with_other ),
904+ ids = lambda x : x .__class__ .__name__ )
905+ def test_append_different_columns_types_raises (
906+ self , index_can_append , index_cannot_append_with_other ):
916907 # GH18359
917- # .append will raise if IntervalIndex/MultiIndex appends or is
918- # appended to a different index type
908+ # Dataframe .append will raise if IntervalIndex/MultiIndex appends
909+ # or is appended to a different index type
919910 #
920- # see also test 'test_append_different_columns_types' above for
911+ # See also test 'test_append_different_columns_types' above for
921912 # appending without raising.
922913
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 )
914+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = index_can_append )
915+ ser = pd .Series ([7 , 8 , 9 ], index = index_cannot_append_with_other ,
916+ name = 2 )
929917 with pytest .raises (TypeError ):
930918 df .append (ser )
931919
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 )
920+ df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]],
921+ columns = index_cannot_append_with_other )
922+ ser = pd .Series ([7 , 8 , 9 ], index = index_can_append , name = 2 )
934923 with pytest .raises (TypeError ):
935924 df .append (ser )
936925
0 commit comments