Skip to content

Commit f2fbdf4

Browse files
author
tp
committed
DataFrame.append test fixtures
1 parent f2a5c11 commit f2fbdf4

File tree

1 file changed

+35
-50
lines changed

1 file changed

+35
-50
lines changed

pandas/tests/reshape/test_concat.py

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -831,56 +831,62 @@ 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_I = [
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]),
842840
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
843841
dt.datetime(2013, 1, 3, 6, 10),
844842
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):
847860
# GH18359
848861

849862
# 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]
852865
ser = pd.Series([7, 8], index=ser_index, name=2)
853866
result = df.append(ser)
854867
expected = pd.DataFrame([[1., 2., 3.], [4, 5, 6], [7, 8, np.nan]],
855868
index=[0, 1, 2],
856-
columns=df_columns)
869+
columns=index_II)
857870
assert_frame_equal(result, expected)
858871

859872
# 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)
863876
ser = pd.Series([7, 8, 9], index=ser_index, name=2)
864877
result = df.append(ser)
865878
expected = pd.DataFrame([[1, 2, np.nan], [4, 5, np.nan], [7, 8, 9]],
866879
index=[0, 1, 2],
867880
columns=ser_index)
868881
assert_frame_equal(result, expected)
869882

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))
881886
def test_append_different_columns_types(self, df_columns, series_index):
882887
# GH18359
883888
# see also test 'test_append_different_columns_types_raises' below
889+
# for errors raised when appending
884890

885891
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=df_columns)
886892
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):
895901
columns=combined_columns)
896902
assert_frame_equal(result, expected)
897903

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):
916905
# GH18359
917906
# .append will raise if IntervalIndex/MultiIndex appends or is
918907
# appended to a different index type
919908
#
920909
# see also test 'test_append_different_columns_types' above for
921910
# appending without raising.
922911

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)
929914
with pytest.raises(TypeError):
930915
df.append(ser)
931916

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)
934919
with pytest.raises(TypeError):
935920
df.append(ser)
936921

0 commit comments

Comments
 (0)