Skip to content
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ Conversion
- Bug in metaclass of generic abstract dtypes causing :meth:`DataFrame.apply` and :meth:`Series.apply` to raise for the built-in function ``type`` (:issue:`46684`)
- Bug in :meth:`DataFrame.to_records` returning inconsistent numpy types if the index was a :class:`MultiIndex` (:issue:`47263`)
- Bug in :meth:`DataFrame.to_dict` for ``orient="list"`` or ``orient="index"`` was not returning native types (:issue:`46751`)
- Bug in :meth:`DataFrame.apply` returns a dataframe when empty dataframe instead of a series (:issue:`39111`)

Strings
^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,10 @@ def apply_empty_result(self):

if not should_reduce:
try:
r = self.f(Series([], dtype=np.float64))
if self.axis == 0:
r = self.f(Series([], dtype=np.float64))
else:
r = self.f(Series(index=self.columns, dtype=np.float64))
except Exception:
pass
else:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,3 +1577,11 @@ def test_apply_type():
result = df.apply(type, axis=1)
expected = Series({"a": Series, "b": Series, "c": Series})
tm.assert_series_equal(result, expected)


def test_apply_on_empty_dataframe():
# GH 39111
df = DataFrame({"a": [1, 2], "b": [3, 0]})
result = df.head(0).apply(lambda x: max(x["a"], x["b"]), axis=1)
expected = Series([])
tm.assert_series_equal(result, expected)