Skip to content

Commit 730ac3a

Browse files
author
tp
committed
DataFrame.append: add parametrized tests, different dtypes
1 parent ead911d commit 730ac3a

File tree

1 file changed

+75
-19
lines changed

1 file changed

+75
-19
lines changed

pandas/tests/reshape/test_concat.py

+75-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from warnings import catch_warnings
2+
from itertools import combinations, product
23

34
import datetime as dt
45
import dateutil
@@ -855,37 +856,92 @@ def test_append_same_columns_type(self, df_columns):
855856
columns=ser_index)
856857
assert_frame_equal(result, expected)
857858

858-
@pytest.mark.parametrize("df_columns", [
859+
@pytest.mark.parametrize("df_columns, series_index", combinations([
859860
pd.RangeIndex(3),
860861
pd.CategoricalIndex('A B C'.split()),
861862
pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]),
862863
pd.IntervalIndex.from_breaks([0, 1, 2, 3]),
863864
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
864865
dt.datetime(2013, 1, 3, 6, 10),
865866
dt.datetime(2013, 1, 3, 7, 12)]),
866-
pd.Index([1, 2, 3]),
867-
])
868-
def test_append_different_columns_types(self, df_columns):
867+
pd.Index([4, 5, 6]),
868+
], r=2))
869+
def test_append_different_columns_types(self, df_columns, series_index):
869870
# GH18359
870871

871-
# ser.index is a normal pd.Index, so result from df.append(ser) should
872-
# be pd.Index (but this is not possible for IntervalIndex and
873-
# MultiIndex)
874872
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=df_columns)
875-
ser = pd.Series([7], index=['a'], name=2)
876-
if isinstance(df_columns, (pd.IntervalIndex, pd.MultiIndex)):
877-
with pytest.raises(TypeError):
873+
ser = pd.Series([7, 8, 9], index=series_index, name=2)
874+
875+
# MultiIndex and IntervalIndex will raise if appended or appended to
876+
# a different type
877+
if (isinstance(df_columns, (pd.IntervalIndex, pd.MultiIndex)) or
878+
isinstance(series_index, (pd.IntervalIndex, pd.MultiIndex))):
879+
with pytest.raises((ValueError, TypeError)):
878880
df.append(ser)
881+
return
882+
result = df.append(ser)
883+
idx_diff = ser.index.difference(df_columns)
884+
combined_columns = Index(df_columns.tolist()).append(idx_diff)
885+
expected = pd.DataFrame([[1., 2., 3., np.nan, np.nan, np.nan],
886+
[4, 5, 6, np.nan, np.nan, np.nan],
887+
[np.nan, np.nan, np.nan, 7, 8, 9]],
888+
index=[0, 1, 2],
889+
columns=combined_columns)
890+
assert_frame_equal(result, expected)
891+
892+
@pytest.mark.parametrize("other_type", [
893+
pd.RangeIndex(3),
894+
pd.CategoricalIndex('A B C'.split()),
895+
pd.IntervalIndex.from_breaks([0, 1, 2, 3]),
896+
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
897+
dt.datetime(2013, 1, 3, 6, 10),
898+
dt.datetime(2013, 1, 3, 7, 12)]),
899+
pd.Index([4, 5, 6]),
900+
])
901+
def test_append_multi_index_raises(self, other_type):
902+
# GH18359
903+
# .append will raise if MultiIndex appends or is appended to a
904+
# different index type
905+
906+
mi = pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()])
907+
908+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=mi)
909+
ser = pd.Series([7, 8, 9], index=other_type, name=2)
910+
if isinstance(other_type, pd.IntervalIndex):
911+
pytest.raises(ValueError, df.append, ser)
879912
else:
880-
result = df.append(ser)
881-
idx_diff = ser.index.difference(df_columns)
882-
combined_columns = Index(df_columns.tolist()).append(idx_diff)
883-
expected = pd.DataFrame([[1., 2., 3., np.nan],
884-
[4, 5, 6, np.nan],
885-
[np.nan, np.nan, np.nan, 7]],
886-
index=[0, 1, 2],
887-
columns=combined_columns)
888-
assert_frame_equal(result, expected)
913+
pytest.raises(TypeError, df.append, ser)
914+
915+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type)
916+
ser = pd.Series([7, 8, 9], index=mi, name=2)
917+
with pytest.raises(TypeError):
918+
df.append(ser)
919+
920+
@pytest.mark.parametrize("other_type", [
921+
pd.RangeIndex(3),
922+
pd.CategoricalIndex('A B C'.split()),
923+
pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]),
924+
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
925+
dt.datetime(2013, 1, 3, 6, 10),
926+
dt.datetime(2013, 1, 3, 7, 12)]),
927+
pd.Index([4, 5, 6]),
928+
])
929+
def test_append_interval_index_raises(self, other_type):
930+
# GH18359
931+
# .append will raise if IntervalIndex appends or is appended to a
932+
# different index type
933+
934+
ii = pd.IntervalIndex.from_breaks([0, 1, 2, 3])
935+
936+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=ii)
937+
ser = pd.Series([7, 8, 9], index=other_type, name=2)
938+
with pytest.raises(TypeError):
939+
df.append(ser)
940+
941+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type)
942+
ser = pd.Series([7, 8, 9], index=ii, name=2)
943+
with pytest.raises(ValueError):
944+
df.append(ser)
889945

890946
def test_append_dtype_coerce(self):
891947

0 commit comments

Comments
 (0)