Skip to content

Commit 8f8b140

Browse files
author
tp
committed
DataFrame.append: add parametrized tests, different dtypes
1 parent 0d55da6 commit 8f8b140

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

pandas/tests/reshape/test_concat.py

+22-20
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
@@ -855,37 +856,38 @@ 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)
879-
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)
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)
889891

890892
def test_append_dtype_coerce(self):
891893

0 commit comments

Comments
 (0)