Skip to content

Commit 464fdc2

Browse files
author
tp
committed
DataFrame.append: add parametrized tests, different dtypes
1 parent 1e0d977 commit 464fdc2

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
23

34
import datetime as dt
45
import dateutil
@@ -864,37 +865,92 @@ def test_append_same_columns_type(self, df_columns):
864865
columns=ser_index)
865866
assert_frame_equal(result, expected)
866867

867-
@pytest.mark.parametrize("df_columns", [
868+
@pytest.mark.parametrize("df_columns, series_index", combinations([
868869
pd.RangeIndex(3),
869870
pd.CategoricalIndex('A B C'.split()),
870871
pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]),
871872
pd.IntervalIndex.from_breaks([0, 1, 2, 3]),
872873
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
873874
dt.datetime(2013, 1, 3, 6, 10),
874875
dt.datetime(2013, 1, 3, 7, 12)]),
875-
pd.Index([1, 2, 3]),
876-
])
877-
def test_append_different_columns_types(self, df_columns):
876+
pd.Index([4, 5, 6]),
877+
], r=2))
878+
def test_append_different_columns_types(self, df_columns, series_index):
878879
# GH18359
879880

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

899955
def test_append_dtype_coerce(self):
900956

0 commit comments

Comments
 (0)