1
1
from warnings import catch_warnings
2
- from itertools import combinations
2
+ from itertools import combinations , product
3
3
4
4
import datetime as dt
5
5
import dateutil
@@ -831,56 +831,59 @@ 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_can_append = [
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 ]),
840
+ pd .CategoricalIndex ('D E F' .split (), ordered = True ),
842
841
pd .DatetimeIndex ([dt .datetime (2013 , 1 , 3 , 0 , 0 ),
843
842
dt .datetime (2013 , 1 , 3 , 6 , 10 ),
844
843
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 ):
847
857
# GH18359
848
858
849
859
# 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 ]
852
862
ser = pd .Series ([7 , 8 ], index = ser_index , name = 2 )
853
863
result = df .append (ser )
854
864
expected = pd .DataFrame ([[1. , 2. , 3. ], [4 , 5 , 6 ], [7 , 8 , np .nan ]],
855
865
index = [0 , 1 , 2 ],
856
- columns = df_columns )
866
+ columns = index )
857
867
assert_frame_equal (result , expected )
858
868
859
869
# 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 )
863
873
ser = pd .Series ([7 , 8 , 9 ], index = ser_index , name = 2 )
864
874
result = df .append (ser )
865
875
expected = pd .DataFrame ([[1 , 2 , np .nan ], [4 , 5 , np .nan ], [7 , 8 , 9 ]],
866
876
index = [0 , 1 , 2 ],
867
877
columns = ser_index )
868
878
assert_frame_equal (result , expected )
869
879
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__ )
881
883
def test_append_different_columns_types (self , df_columns , series_index ):
882
884
# 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
884
887
885
888
df = pd .DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = df_columns )
886
889
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):
895
898
columns = combined_columns )
896
899
assert_frame_equal (result , expected )
897
900
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 ):
916
907
# 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
919
910
#
920
- # see also test 'test_append_different_columns_types' above for
911
+ # See also test 'test_append_different_columns_types' above for
921
912
# appending without raising.
922
913
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 )
929
917
with pytest .raises (TypeError ):
930
918
df .append (ser )
931
919
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 )
934
923
with pytest .raises (TypeError ):
935
924
df .append (ser )
936
925
0 commit comments