|
2 | 2 | from datetime import datetime
|
3 | 3 | from itertools import chain
|
4 | 4 | import operator
|
5 |
| -from unittest.mock import Mock |
6 | 5 | import warnings
|
7 | 6 |
|
8 | 7 | import numpy as np
|
@@ -729,25 +728,40 @@ def test_apply_noreduction_tzaware_object(self):
|
729 | 728 |
|
730 | 729 | def test_apply_function_runs_once(self):
|
731 | 730 | # https://github.com/pandas-dev/pandas/issues/30815
|
732 |
| - non_reducing_mock = Mock(side_effect=lambda x: x) |
733 |
| - reducing_mock = Mock(return_value=1) |
734 | 731 |
|
735 | 732 | df = pd.DataFrame({"a": [1, 2, 3]})
|
| 733 | + names = [] # Save row names function is applied to |
736 | 734 |
|
737 |
| - # no reduction |
738 |
| - df.apply(non_reducing_mock, axis=1) |
739 |
| - assert non_reducing_mock.call_count == 3 |
| 735 | + def reducing_function(row): |
| 736 | + names.append(row.name) |
| 737 | + |
| 738 | + def non_reducing_function(row): |
| 739 | + names.append(row.name) |
| 740 | + return row |
| 741 | + |
| 742 | + for func in [reducing_function, non_reducing_function]: |
| 743 | + del names[:] |
740 | 744 |
|
741 |
| - # reduction |
742 |
| - df.apply(reducing_mock, axis=1) |
743 |
| - assert reducing_mock.call_count == 3 |
| 745 | + df.apply(func, axis=1) |
| 746 | + assert names == list(df.index) |
744 | 747 |
|
745 | 748 | def test_applymap_function_runs_once(self):
|
746 |
| - reducing_mock = Mock(return_value=1) |
747 | 749 |
|
748 | 750 | df = pd.DataFrame({"a": [1, 2, 3]})
|
749 |
| - df.applymap(reducing_mock) |
750 |
| - assert reducing_mock.call_count == 3 |
| 751 | + values = [] # Save values function is applied to |
| 752 | + |
| 753 | + def reducing_function(val): |
| 754 | + values.append(val) |
| 755 | + |
| 756 | + def non_reducing_function(val): |
| 757 | + values.append(val) |
| 758 | + return val |
| 759 | + |
| 760 | + for func in [reducing_function, non_reducing_function]: |
| 761 | + del values[:] |
| 762 | + |
| 763 | + df.applymap(func) |
| 764 | + assert values == df.a.to_list() |
751 | 765 |
|
752 | 766 |
|
753 | 767 | class TestInferOutputShape:
|
|
0 commit comments