From 3384144c7eb590bd1ee6bdb90dcdadf49a481264 Mon Sep 17 00:00:00 2001 From: Drew <101425679+pinkgradient@users.noreply.github.com> Date: Tue, 3 Jun 2025 22:16:14 -0700 Subject: [PATCH 1/5] DOC: Add note on inference behavior of apply with result_type='expand' --- pandas/core/frame.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index bf919c6fe8a42..812266b1c7d9e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10378,6 +10378,7 @@ def apply( ---------- func : function Function to apply to each column or row. + axis : {0 or 'index', 1 or 'columns'}, default 0 Axis along which the function is applied: @@ -10396,18 +10397,25 @@ def apply( result_type : {'expand', 'reduce', 'broadcast', None}, default None These only act when ``axis=1`` (columns): - + * 'expand' : list-like results will be turned into columns. + Note: The output type is inferred from the first function return value. + If the first return value is not list-like (e.g., None or NaN), expansion + will not occur, and the result may be a Series instead of a DataFrame. + To avoid inconsistent output types, ensure your function returns consistent + list-like objects (e.g., an empty dict {}) for missing or NaN-like values. + * 'reduce' : returns a Series if possible rather than expanding list-like results. This is the opposite of 'expand'. * 'broadcast' : results will be broadcast to the original shape of the DataFrame, the original index and columns will be retained. - + The default behaviour (None) depends on the return value of the applied function: list-like results will be returned as a Series of those. However if the apply function returns a Series these - are expanded to columns. + are expanded to columns. + args : tuple Positional arguments to pass to `func` in addition to the array/series. From 8ef959ccc7e1f58039893beef7e0ce17ea3ede1a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Jun 2025 05:26:40 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/core/frame.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 812266b1c7d9e..24af68c66a93d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10378,7 +10378,7 @@ def apply( ---------- func : function Function to apply to each column or row. - + axis : {0 or 'index', 1 or 'columns'}, default 0 Axis along which the function is applied: @@ -10397,25 +10397,25 @@ def apply( result_type : {'expand', 'reduce', 'broadcast', None}, default None These only act when ``axis=1`` (columns): - + * 'expand' : list-like results will be turned into columns. Note: The output type is inferred from the first function return value. If the first return value is not list-like (e.g., None or NaN), expansion will not occur, and the result may be a Series instead of a DataFrame. To avoid inconsistent output types, ensure your function returns consistent list-like objects (e.g., an empty dict {}) for missing or NaN-like values. - + * 'reduce' : returns a Series if possible rather than expanding list-like results. This is the opposite of 'expand'. * 'broadcast' : results will be broadcast to the original shape of the DataFrame, the original index and columns will be retained. - + The default behaviour (None) depends on the return value of the applied function: list-like results will be returned as a Series of those. However if the apply function returns a Series these - are expanded to columns. - + are expanded to columns. + args : tuple Positional arguments to pass to `func` in addition to the array/series. From a07be00f7f5854899e49f7e76e417d48351988f7 Mon Sep 17 00:00:00 2001 From: Drew <101425679+pinkgradient@users.noreply.github.com> Date: Tue, 3 Jun 2025 22:31:23 -0700 Subject: [PATCH 3/5] Updated line length --- pandas/core/frame.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 24af68c66a93d..352abb71db636 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10399,11 +10399,13 @@ def apply( These only act when ``axis=1`` (columns): * 'expand' : list-like results will be turned into columns. - Note: The output type is inferred from the first function return value. - If the first return value is not list-like (e.g., None or NaN), expansion - will not occur, and the result may be a Series instead of a DataFrame. - To avoid inconsistent output types, ensure your function returns consistent - list-like objects (e.g., an empty dict {}) for missing or NaN-like values. + Note: The output type is inferred from the first function return value. + If the first return value is not list-like (e.g., None or NaN), expansion + will not occur, and the result may be a Series instead of a DataFrame. + To avoid inconsistent output types, ensure your function returns + consistent list-like objects (e.g., an empty dict {}) for missing or + NaN-like values. + * 'reduce' : returns a Series if possible rather than expanding list-like results. This is the opposite of 'expand'. From 9b6547361a82a96de0f4cb80a238d311b6ebc055 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Jun 2025 05:37:36 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/core/frame.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 352abb71db636..03a5065f22246 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10399,11 +10399,11 @@ def apply( These only act when ``axis=1`` (columns): * 'expand' : list-like results will be turned into columns. - Note: The output type is inferred from the first function return value. - If the first return value is not list-like (e.g., None or NaN), expansion - will not occur, and the result may be a Series instead of a DataFrame. - To avoid inconsistent output types, ensure your function returns - consistent list-like objects (e.g., an empty dict {}) for missing or + Note: The output type is inferred from the first function return value. + If the first return value is not list-like (e.g., None or NaN), expansion + will not occur, and the result may be a Series instead of a DataFrame. + To avoid inconsistent output types, ensure your function returns + consistent list-like objects (e.g., an empty dict {}) for missing or NaN-like values. From c084ad0b644517d7712d1668a92177af5fda4a92 Mon Sep 17 00:00:00 2001 From: Drew <101425679+pinkgradient@users.noreply.github.com> Date: Fri, 6 Jun 2025 09:31:29 -0700 Subject: [PATCH 5/5] Update frame.py Removed extra blank rows. --- pandas/core/frame.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 03a5065f22246..d242291fa58c4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -10406,7 +10406,6 @@ def apply( consistent list-like objects (e.g., an empty dict {}) for missing or NaN-like values. - * 'reduce' : returns a Series if possible rather than expanding list-like results. This is the opposite of 'expand'. * 'broadcast' : results will be broadcast to the original shape