Skip to content

Commit fa6299d

Browse files
committed
Add whatsnew, small CR refactor
1 parent 7ee1faa commit fa6299d

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

doc/source/whatsnew/v1.1.0.rst

+59
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,65 @@ Using :meth:`DataFrame.groupby` with ``as_index=False`` and the function ``idxma
630630
631631
df.groupby("a", as_index=False).nunique()
632632
633+
.. _whatsnew_110.api_breaking.apply_applymap_first_once:
634+
635+
apply and applymap on ``DataFrame`` evaluates first row/column only once
636+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
637+
638+
.. ipython:: python
639+
640+
In [1]: import pandas as pd
641+
...: df = pd.DataFrame({'a': [1,2], 'b': [3,6]})
642+
...:
643+
644+
In [2]: df
645+
Out[2]:
646+
a b
647+
0 1 3
648+
1 2 6
649+
650+
In [3]: def func(row):
651+
...: print(row)
652+
...: return row
653+
...:
654+
655+
656+
*Previous behavior*:
657+
658+
.. ipython:: python
659+
660+
In [4]: df.apply(func, axis=1)
661+
a 1
662+
b 3
663+
Name: 0, dtype: int64
664+
a 1
665+
b 3
666+
Name: 0, dtype: int64
667+
a 2
668+
b 6
669+
Name: 1, dtype: int64
670+
Out[4]:
671+
a b
672+
0 1 3
673+
1 2 6
674+
675+
*New behavior*:
676+
677+
.. ipython:: python
678+
679+
In [4]: df.apply(func, axis=1)
680+
a 1
681+
b 3
682+
Name: 0, dtype: int64
683+
a 2
684+
b 6
685+
Name: 1, dtype: int64
686+
Out[4]:
687+
a b
688+
0 1 3
689+
1 2 6
690+
691+
633692
.. _whatsnew_110.deprecations:
634693

635694
Deprecations

pandas/core/apply.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ def apply_broadcast(self, target: "DataFrame") -> "DataFrame":
263263

264264
def apply_standard(self):
265265

266-
partial_result = None # partial result that may be returned from reduction.
266+
# partial result that may be returned from reduction
267+
partial_result = None
267268

268269
# try to reduce first (by default)
269270
# this only matters if the reduction in values is of different dtype
@@ -305,12 +306,12 @@ def apply_standard(self):
305306
else:
306307
if reduction_success:
307308
return self.obj._constructor_sliced(result, index=labels)
308-
else:
309-
# no exceptions - however reduction was unsuccessful,
310-
# use the computed function result for first element
311-
partial_result = result[0]
312-
if isinstance(partial_result, ABCSeries):
313-
partial_result = partial_result.infer_objects()
309+
310+
# no exceptions - however reduction was unsuccessful,
311+
# use the computed function result for first element
312+
partial_result = result[0]
313+
if isinstance(partial_result, ABCSeries):
314+
partial_result = partial_result.infer_objects()
314315

315316
# compute the result using the series generator,
316317
# use the result computed while trying to reduce if available.
@@ -323,7 +324,6 @@ def apply_series_generator(self, partial_result=None) -> Tuple[ResType, "Index"]
323324
series_gen = self.series_generator
324325
res_index = self.result_index
325326

326-
keys = []
327327
results = {}
328328

329329
# If a partial result was already computed,
@@ -332,7 +332,6 @@ def apply_series_generator(self, partial_result=None) -> Tuple[ResType, "Index"]
332332
if partial_result is not None:
333333
i, v = next(series_gen_enumeration)
334334
results[i] = partial_result
335-
keys.append(v.name)
336335

337336
if self.ignore_failures:
338337
successes = []
@@ -342,7 +341,6 @@ def apply_series_generator(self, partial_result=None) -> Tuple[ResType, "Index"]
342341
except Exception:
343342
pass
344343
else:
345-
keys.append(v.name)
346344
successes.append(i)
347345

348346
# so will work with MultiIndex
@@ -353,7 +351,6 @@ def apply_series_generator(self, partial_result=None) -> Tuple[ResType, "Index"]
353351
for i, v in series_gen_enumeration:
354352

355353
results[i] = self.f(v)
356-
keys.append(v.name)
357354

358355
return results, res_index
359356

0 commit comments

Comments
 (0)