Skip to content

Commit 860a5d2

Browse files
author
tp
committed
DataFrame.append preserves columns dtype if possible
1 parent 9630f8d commit 860a5d2

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Other Enhancements
202202
- ``Resampler`` objects now have a functioning :attr:`~pandas.core.resample.Resampler.pipe` method.
203203
Previously, calls to ``pipe`` were diverted to the ``mean`` method (:issue:`17905`).
204204
- :func:`~pandas.api.types.is_scalar` now returns ``True`` for ``DateOffset`` objects (:issue:`18943`).
205+
- :meth:`DataFrame.append` now preserves the type of the calling dataframe's columns, when possible (:issue:`18359`)
205206
- Added :func:`pandas.api.extensions.register_dataframe_accessor`,
206207
:func:`pandas.api.extensions.register_series_accessor`, and
207208
:func:`pandas.api.extensions.register_index_accessor`, accessor for libraries downstream of pandas

pandas/tests/reshape/test_concat.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,6 @@ def test_append_preserve_index_name(self):
834834
@pytest.mark.parametrize("df_columns", [
835835
pd.RangeIndex(3),
836836
pd.CategoricalIndex('A B C'.split()),
837-
pd.CategoricalIndex('A B C'.split(), ordered=True),
838837
pd.MultiIndex.from_arrays(['A B C'.split(), 'D E F'.split()]),
839838
pd.IntervalIndex.from_breaks([0, 1, 2, 3]),
840839
pd.DatetimeIndex([dt.datetime(2013, 1, 3, 0, 0),
@@ -913,10 +912,8 @@ def test_append_multi_index_raises(self, other_type):
913912

914913
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=mi)
915914
ser = pd.Series([7, 8, 9], index=other_type, name=2)
916-
if isinstance(other_type, pd.IntervalIndex):
917-
pytest.raises(ValueError, df.append, ser)
918-
else:
919-
pytest.raises(TypeError, df.append, ser)
915+
with pytest.raises(TypeError):
916+
df.append(ser)
920917

921918
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type)
922919
ser = pd.Series([7, 8, 9], index=mi, name=2)
@@ -947,7 +944,7 @@ def test_append_interval_index_raises(self, other_type):
947944

948945
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=other_type)
949946
ser = pd.Series([7, 8, 9], index=ii, name=2)
950-
with pytest.raises(ValueError):
947+
with pytest.raises(TypeError):
951948
df.append(ser)
952949

953950
def test_append_dtype_coerce(self):

0 commit comments

Comments
 (0)