Skip to content

Commit fa07729

Browse files
committed
Revert "DEPR: Deprecate returning a DataFrame in SeriesApply.apply_standard (pandas-dev#52123)"
This reverts commit fe415f5
1 parent 6826845 commit fa07729

File tree

7 files changed

+35
-69
lines changed

7 files changed

+35
-69
lines changed

doc/source/user_guide/cookbook.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,12 @@ Apply
794794
index=["I", "II", "III"],
795795
)
796796
797-
def make_df(ser):
798-
new_vals = [pd.Series(value, name=name) for name, value in ser.items()]
799-
return pd.DataFrame(new_vals)
800-
801-
df_orgz = pd.concat({ind: row.pipe(make_df) for ind, row in df.iterrows()})
797+
def SeriesFromSubList(aList):
798+
return pd.Series(aList)
802799
800+
df_orgz = pd.concat(
801+
{ind: row.apply(SeriesFromSubList) for ind, row in df.iterrows()}
802+
)
803803
df_orgz
804804
805805
`Rolling apply with a DataFrame returning a Series

doc/source/user_guide/groupby.rst

+13
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,19 @@ The dimension of the returned result can also change:
12131213
12141214
grouped.apply(f)
12151215
1216+
``apply`` on a Series can operate on a returned value from the applied function
1217+
that is itself a series, and possibly upcast the result to a DataFrame:
1218+
1219+
.. ipython:: python
1220+
1221+
def f(x):
1222+
return pd.Series([x, x ** 2], index=["x", "x^2"])
1223+
1224+
1225+
s = pd.Series(np.random.rand(5))
1226+
s
1227+
s.apply(f)
1228+
12161229
Similar to :ref:`groupby.aggregate.agg`, the resulting dtype will reflect that of the
12171230
apply function. If the results from different groups have different dtypes, then
12181231
a common dtype will be determined in the same way as ``DataFrame`` construction.

doc/source/whatsnew/v0.10.0.rst

+9-20
Original file line numberDiff line numberDiff line change
@@ -261,26 +261,15 @@ Convenience methods ``ffill`` and ``bfill`` have been added:
261261
function, that is itself a series, and possibly upcast the result to a
262262
DataFrame
263263

264-
.. code-block:: python
265-
266-
>>> def f(x):
267-
... return pd.Series([x, x ** 2], index=["x", "x^2"])
268-
>>>
269-
>>> s = pd.Series(np.random.rand(5))
270-
>>> s
271-
0 0.340445
272-
1 0.984729
273-
2 0.919540
274-
3 0.037772
275-
4 0.861549
276-
dtype: float64
277-
>>> s.apply(f)
278-
x x^2
279-
0 0.340445 0.115903
280-
1 0.984729 0.969691
281-
2 0.919540 0.845555
282-
3 0.037772 0.001427
283-
4 0.861549 0.742267
264+
.. ipython:: python
265+
266+
def f(x):
267+
return pd.Series([x, x ** 2], index=["x", "x^2"])
268+
269+
270+
s = pd.Series(np.random.rand(5))
271+
s
272+
s.apply(f)
284273
285274
- New API functions for working with pandas options (:issue:`2097`):
286275

doc/source/whatsnew/v2.1.0.rst

-8
Original file line numberDiff line numberDiff line change
@@ -548,14 +548,6 @@ Other Deprecations
548548
- Deprecated :meth:`.Styler.applymap`. Use the new :meth:`.Styler.map` method instead (:issue:`52708`)
549549
- Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`)
550550
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
551-
- Deprecated ``freq`` parameter in :class:`.PeriodArray` constructor, pass ``dtype`` instead (:issue:`52462`)
552-
- Deprecated allowing non-standard inputs in :func:`take`, pass either a ``numpy.ndarray``, :class:`.ExtensionArray`, :class:`Index`, or :class:`Series` (:issue:`52981`)
553-
- Deprecated allowing non-standard sequences for :func:`isin`, :func:`value_counts`, :func:`unique`, :func:`factorize`, case to one of ``numpy.ndarray``, :class:`Index`, :class:`.ExtensionArray`, or :class:`Series` before calling (:issue:`52986`)
554-
- Deprecated behavior of :class:`DataFrame` reductions ``sum``, ``prod``, ``std``, ``var``, ``sem`` with ``axis=None``, in a future version this will operate over both axes returning a scalar instead of behaving like ``axis=0``; note this also affects numpy functions e.g. ``np.sum(df)`` (:issue:`21597`)
555-
- Deprecated behavior of :func:`concat` when :class:`DataFrame` has columns that are all-NA, in a future version these will not be discarded when determining the resulting dtype (:issue:`40893`)
556-
- Deprecated behavior of :meth:`Series.dt.to_pydatetime`, in a future version this will return a :class:`Series` containing python ``datetime`` objects instead of an ``ndarray`` of datetimes; this matches the behavior of other :attr:`Series.dt` properties (:issue:`20306`)
557-
- Deprecated logical operations (``|``, ``&``, ``^``) between pandas objects and dtype-less sequences (e.g. ``list``, ``tuple``), wrap a sequence in a :class:`Series` or NumPy array before operating instead (:issue:`51521`)
558-
- Deprecated making :meth:`Series.apply` return a :class:`DataFrame` when the passed-in callable returns a :class:`Series` object. In the future this will return a :class:`Series` whose values are themselves :class:`Series`. This pattern was very slow and it's recommended to use alternative methods to archive the same goal (:issue:`52116`)
559551
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
560552
- Deprecated passing a dictionary to :meth:`.SeriesGroupBy.agg`; pass a list of aggregations instead (:issue:`50684`)
561553
- Deprecated the ``fastpath`` keyword in :class:`Categorical` constructor, use :meth:`Categorical.from_codes` instead (:issue:`20110`)

pandas/core/apply.py

-8
Original file line numberDiff line numberDiff line change
@@ -1326,14 +1326,6 @@ def curried(x):
13261326
)
13271327

13281328
if len(mapped) and isinstance(mapped[0], ABCSeries):
1329-
warnings.warn(
1330-
"Returning a DataFrame from Series.apply when the supplied function "
1331-
"returns a Series is deprecated and will be removed in a future "
1332-
"version.",
1333-
FutureWarning,
1334-
stacklevel=find_stack_level(),
1335-
) # GH52116
1336-
13371329
# GH#43986 Need to do list(mapped) in order to get treated as nested
13381330
# See also GH#25959 regarding EA support
13391331
return obj._constructor_expanddim(list(mapped), index=obj.index)

pandas/core/series.py

-5
Original file line numberDiff line numberDiff line change
@@ -4638,11 +4638,6 @@ def apply(
46384638
"""
46394639
Invoke function on values of Series.
46404640
4641-
.. deprecated:: 2.1.0
4642-
4643-
If the result from ``func`` is a ``Series``, wrapping the output in a
4644-
``DataFrame`` instead of a ``Series`` has been deprecated.
4645-
46464641
Can be ufunc (a NumPy function that applies to the entire Series)
46474642
or a Python function that only works on single values.
46484643

pandas/tests/apply/test_series_apply.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,15 @@ def test_agg_evaluate_lambdas(string_series):
420420
def test_with_nested_series(datetime_series, op_name):
421421
# GH 2316
422422
# .agg with a reducer and a transform, what to do
423-
msg = "Returning a DataFrame from Series.apply when the supplied function"
424-
with tm.assert_produces_warning(FutureWarning, match=msg):
425-
# GH52123
426-
result = getattr(datetime_series, op_name)(
427-
lambda x: Series([x, x**2], index=["x", "x^2"])
428-
)
423+
result = getattr(datetime_series, op_name)(
424+
lambda x: Series([x, x**2], index=["x", "x^2"])
425+
)
429426
expected = DataFrame({"x": datetime_series, "x^2": datetime_series**2})
430427
tm.assert_frame_equal(result, expected)
431428

429+
result = datetime_series.agg(lambda x: Series([x, x**2], index=["x", "x^2"]))
430+
tm.assert_frame_equal(result, expected)
431+
432432

433433
def test_replicate_describe(string_series):
434434
# this also tests a result set that is all scalars
@@ -512,10 +512,7 @@ def test_apply_series_on_date_time_index_aware_series(dti, exp, aware):
512512
index = dti.tz_localize("UTC").index
513513
else:
514514
index = dti.index
515-
msg = "Returning a DataFrame from Series.apply when the supplied function"
516-
with tm.assert_produces_warning(FutureWarning, match=msg):
517-
# GH52123
518-
result = Series(index).apply(lambda x: Series([1, 2]))
515+
result = Series(index).apply(lambda x: Series([1, 2]))
519516
tm.assert_frame_equal(result, exp)
520517

521518

@@ -662,19 +659,7 @@ def test_apply_dictlike_lambda(ops, by_row, expected):
662659
def test_apply_retains_column_name(by_row):
663660
# GH 16380
664661
df = DataFrame({"x": range(3)}, Index(range(3), name="x"))
665-
func = lambda x: Series(range(x + 1), Index(range(x + 1), name="y"))
666-
667-
if not by_row:
668-
# GH53400
669-
msg = "'Series' object cannot be interpreted as an integer"
670-
with pytest.raises(TypeError, match=msg):
671-
df.x.apply(func, by_row=by_row)
672-
return
673-
674-
msg = "Returning a DataFrame from Series.apply when the supplied function"
675-
with tm.assert_produces_warning(FutureWarning, match=msg):
676-
# GH52123
677-
result = df.x.apply(func, by_row=by_row)
662+
result = df.x.apply(lambda x: Series(range(x + 1), Index(range(x + 1), name="y")))
678663
expected = DataFrame(
679664
[[0.0, np.nan, np.nan], [0.0, 1.0, np.nan], [0.0, 1.0, 2.0]],
680665
columns=Index(range(3), name="y"),

0 commit comments

Comments
 (0)