From 1f522b6690260b0ffc0338323138e01b3bb3e063 Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 4 Jun 2022 18:02:00 +0800 Subject: [PATCH 1/4] add test --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/apply.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 6bf6fd65f5633..3706d8fcd6af4 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -747,6 +747,7 @@ Conversion - Bug when comparing string and datetime64ns objects causing ``OverflowError`` exception. (:issue:`45506`) - 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_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 ^^^^^^^ diff --git a/pandas/core/apply.py b/pandas/core/apply.py index d37080fce6e1c..0cccea5a28e30 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -782,7 +782,7 @@ def apply_empty_result(self): if not should_reduce: try: - r = self.f(Series([], dtype=np.float64)) + r = self.f(Series(index=self.columns, dtype=np.float64)) except Exception: pass else: From aa14a7cc4ee2a102b50a7550f806be0e2c962201 Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 4 Jun 2022 18:02:25 +0800 Subject: [PATCH 2/4] add test --- pandas/tests/apply/test_frame_apply.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index ef7ab4a469865..72a9d8723d34c 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -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) From 2ed9a809c2570d6e13107785ef7230ab7c9f2cff Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 18 Jun 2022 18:24:20 +0800 Subject: [PATCH 3/4] add --- pandas/core/apply.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 92a1573ae4197..18a0f9b7aa2ce 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -790,7 +790,10 @@ def apply_empty_result(self): if not should_reduce: try: - r = self.f(Series(index=self.columns, 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: From 3fb819ab696051583dc44a68e201f2e2d904ab05 Mon Sep 17 00:00:00 2001 From: weikhor Date: Sat, 2 Jul 2022 21:40:05 +0800 Subject: [PATCH 4/4] update rst --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 2849ded8b1048..c117cf1437a83 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -795,7 +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`) +- Bug in :meth:`DataFrame.apply` that returns a :class:`DataFrame` instead of a :class:`Series` when applied to an empty :class:`DataFrame` and ``axis=1`` (:issue:`39111`) Strings ^^^^^^^