Skip to content

Commit bf23643

Browse files
committed
Fix bug GH23823
1 parent 85572de commit bf23643

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Performance Improvements
105105
Bug Fixes
106106
~~~~~~~~~
107107

108-
-
108+
- :func:`maybe_upcast_putmask` does not replace the element of the ``ndarray`` correctly (:issue:`23823`)
109109

110110
Categorical
111111
^^^^^^^^^^^

pandas/core/dtypes/cast.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@ def changeit():
209209
om = other[mask]
210210
om_at = om.astype(result.dtype)
211211
if (om == om_at).all():
212-
new_result = result.values.copy()
212+
if isinstance(result, np.ndarray):
213+
new_result = result.copy()
214+
else:
215+
new_result = result.values.copy()
213216
new_result[mask] = om_at
214217
result[:] = new_result
215218
return result, False
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import numpy as np
4+
5+
from pandas.core.dtypes.cast import maybe_upcast_putmask
6+
7+
from pandas import Series
8+
from pandas.util import testing as tm
9+
10+
11+
def test_upcast_series():
12+
result = Series([10, 11, 12])
13+
mask = Series([False, True, False])
14+
other = Series([np.nan, 61, np.nan])
15+
expected = Series([10, 61, 12])
16+
17+
result, _ = maybe_upcast_putmask(result, mask, other)
18+
tm.assert_series_equal(result, expected)
19+
20+
21+
def test_upcast_np_array():
22+
result = np.array([10, 11, 12])
23+
mask = np.array([False, True, False])
24+
other = np.array([np.nan, 61, np.nan])
25+
expected = np.array([10, 61, 12])
26+
27+
result, _ = maybe_upcast_putmask(result, mask, other)
28+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)