Skip to content

Commit 00ec1ca

Browse files
author
tp
committed
DataFrame.append: add parametrized tests, different dtypes
1 parent c7e8f23 commit 00ec1ca

File tree

1 file changed

+71
-21
lines changed

1 file changed

+71
-21
lines changed

pandas/tests/reshape/test_concat.py

+71-21
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,86 @@ 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([
869+
pd.RangeIndex(3),
870+
pd.Index([4, 5, 6]),
871+
pd.Index([7.5, 8.5, 9.5]),
872+
pd.CategoricalIndex('A B C'.split()),
873+
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
874+
dt.datetime(2013, 1, 3, 6, 10),
875+
dt.datetime(2013, 1, 3, 7, 12)]),
876+
], r=2))
877+
def test_append_different_columns_types(self, df_columns, series_index):
878+
# GH18359
879+
# see also tests 'test_append_multi_index_raises' and
880+
# 'test_append_interval_index_raises' below
881+
882+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=df_columns)
883+
ser = pd.Series([7, 8, 9], index=series_index, name=2)
884+
885+
result = df.append(ser)
886+
idx_diff = ser.index.difference(df_columns)
887+
combined_columns = df_columns.astype(object).append(idx_diff)
888+
expected = pd.DataFrame([[1., 2., 3., np.nan, np.nan, np.nan],
889+
[4, 5, 6, np.nan, np.nan, np.nan],
890+
[np.nan, np.nan, np.nan, 7, 8, 9]],
891+
index=[0, 1, 2],
892+
columns=combined_columns)
893+
assert_frame_equal(result, expected)
894+
895+
@pytest.mark.parametrize("other_type", [
868896
pd.RangeIndex(3),
869897
pd.CategoricalIndex('A B C'.split()),
870-
pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]),
871898
pd.IntervalIndex.from_breaks([0, 1, 2, 3]),
872899
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
873900
dt.datetime(2013, 1, 3, 6, 10),
874901
dt.datetime(2013, 1, 3, 7, 12)]),
875-
pd.Index([1, 2, 3]),
902+
pd.Index([4, 5, 6]),
876903
])
877-
def test_append_different_columns_types(self, df_columns):
904+
def test_append_multi_index_raises(self, other_type):
878905
# GH18359
906+
# .append will raise if MultiIndex appends or is appended to a
907+
# different index type
879908

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)
883-
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):
887-
df.append(ser)
909+
mi = pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()])
910+
911+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=mi)
912+
ser = pd.Series([7, 8, 9], index=other_type, name=2)
913+
if isinstance(other_type, pd.IntervalIndex):
914+
pytest.raises(ValueError, df.append, ser)
888915
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)
916+
pytest.raises(TypeError, df.append, ser)
917+
918+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type)
919+
ser = pd.Series([7, 8, 9], index=mi, name=2)
920+
with pytest.raises(TypeError):
921+
df.append(ser)
922+
923+
@pytest.mark.parametrize("other_type", [
924+
pd.RangeIndex(3),
925+
pd.CategoricalIndex('A B C'.split()),
926+
pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]),
927+
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
928+
dt.datetime(2013, 1, 3, 6, 10),
929+
dt.datetime(2013, 1, 3, 7, 12)]),
930+
pd.Index([4, 5, 6]),
931+
])
932+
def test_append_interval_index_raises(self, other_type):
933+
# GH18359
934+
# .append will raise if IntervalIndex appends or is appended to a
935+
# different index type
936+
937+
ii = pd.IntervalIndex.from_breaks([0, 1, 2, 3])
938+
939+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=ii)
940+
ser = pd.Series([7, 8, 9], index=other_type, name=2)
941+
with pytest.raises(TypeError):
942+
df.append(ser)
943+
944+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type)
945+
ser = pd.Series([7, 8, 9], index=ii, name=2)
946+
with pytest.raises(ValueError):
947+
df.append(ser)
898948

899949
def test_append_dtype_coerce(self):
900950

0 commit comments

Comments
 (0)