Skip to content

Commit 16b4ca5

Browse files
owenlamontJulianWgs
authored andcommitted
BUG: fix convert_dtypes to handle empty df (pandas-dev#40402)
1 parent d91fcfc commit 16b4ca5

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ Other
627627
- Bug in :class:`Styler` where multiple elements in CSS-selectors were not correctly added to ``table_styles`` (:issue:`39942`)
628628
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
629629
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
630+
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)
630631

631632

632633
.. ---------------------------------------------------------------------------

pandas/core/generic.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -6343,7 +6343,10 @@ def convert_dtypes(
63436343
)
63446344
for col_name, col in self.items()
63456345
]
6346-
return concat(results, axis=1, copy=False)
6346+
if len(results) > 0:
6347+
return concat(results, axis=1, copy=False)
6348+
else:
6349+
return self.copy()
63476350

63486351
# ----------------------------------------------------------------------
63496352
# Filling NA's

pandas/tests/frame/methods/test_convert_dtypes.py

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ def test_convert_dtypes(self, convert_integer, expected):
2626
}
2727
)
2828
tm.assert_frame_equal(result, expected)
29+
30+
def test_convert_empty(self):
31+
# Empty DataFrame can pass convert_dtypes, see GH#40393
32+
empty_df = pd.DataFrame()
33+
tm.assert_frame_equal(empty_df, empty_df.convert_dtypes())

0 commit comments

Comments
 (0)