Skip to content

Commit 4d50974

Browse files
jorisvandenbosschenofarmish
authored andcommitted
REGR: fix numpy accumulate ufuncs for DataFrame (pandas-dev#39260)
1 parent 5bc2006 commit 4d50974

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

doc/source/whatsnew/v1.2.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Fixed regressions
3232
- Fixed regression in :meth:`DataFrame.replace` raising ``ValueError`` when :class:`DataFrame` has dtype ``bytes`` (:issue:`38900`)
3333
- Fixed regression in :meth:`Series.fillna` that raised ``RecursionError`` with ``datetime64[ns, UTC]`` dtype (:issue:`38851`)
3434
- Fixed regression in comparisons between ``NaT`` and ``datetime.date`` objects incorrectly returning ``True`` (:issue:`39151`)
35+
- Fixed regression in calling NumPy :func:`~numpy.ufunc.accumulate` ufuncs on DataFrames, e.g. ``np.maximum.accumulate(df)`` (:issue:`39259`)
3536
- Fixed regression in repr of float-like strings of an ``object`` dtype having trailing 0's truncated after the decimal (:issue:`38708`)
3637
- Fixed regression that raised ``AttributeError`` with PyArrow versions [0.16.0, 1.0.0) (:issue:`38801`)
3738
- Fixed regression in :func:`pandas.testing.assert_frame_equal` raising ``TypeError`` with ``check_like=True`` when :class:`Index` or columns have mixed dtype (:issue:`39168`)

pandas/core/arraylike.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,14 @@ def reconstruct(result):
274274
result = getattr(ufunc, method)(*inputs, **kwargs)
275275
else:
276276
# ufunc(dataframe)
277-
mgr = inputs[0]._mgr
278-
result = mgr.apply(getattr(ufunc, method))
277+
if method == "__call__":
278+
# for np.<ufunc>(..) calls
279+
mgr = inputs[0]._mgr
280+
result = mgr.apply(getattr(ufunc, method))
281+
else:
282+
# otherwise specific ufunc methods (eg np.<ufunc>.accumulate(..))
283+
# Those can have an axis keyword and thus can't be called block-by-block
284+
result = getattr(ufunc, method)(np.asarray(inputs[0]), **kwargs)
279285

280286
if ufunc.nout > 1: # type: ignore[attr-defined]
281287
result = tuple(reconstruct(x) for x in result)

pandas/tests/frame/test_ufunc.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ def test_binary_frame_series_raises():
118118
np.logaddexp(df["A"], df)
119119

120120

121+
def test_unary_accumulate_axis():
122+
# https://github.com/pandas-dev/pandas/issues/39259
123+
df = pd.DataFrame({"a": [1, 3, 2, 4]})
124+
result = np.maximum.accumulate(df)
125+
expected = pd.DataFrame({"a": [1, 3, 3, 4]})
126+
tm.assert_frame_equal(result, expected)
127+
128+
df = pd.DataFrame({"a": [1, 3, 2, 4], "b": [0.1, 4.0, 3.0, 2.0]})
129+
result = np.maximum.accumulate(df)
130+
# in theory could preserve int dtype for default axis=0
131+
expected = pd.DataFrame({"a": [1.0, 3.0, 3.0, 4.0], "b": [0.1, 4.0, 4.0, 4.0]})
132+
tm.assert_frame_equal(result, expected)
133+
134+
result = np.maximum.accumulate(df, axis=0)
135+
tm.assert_frame_equal(result, expected)
136+
137+
result = np.maximum.accumulate(df, axis=1)
138+
expected = pd.DataFrame({"a": [1.0, 3.0, 2.0, 4.0], "b": [1.0, 4.0, 3.0, 4.0]})
139+
tm.assert_frame_equal(result, expected)
140+
141+
121142
def test_frame_outer_deprecated():
122143
df = pd.DataFrame({"A": [1, 2]})
123144
with tm.assert_produces_warning(FutureWarning):

0 commit comments

Comments
 (0)