Skip to content

Commit 2d9ca9d

Browse files
authored
REGR: sample modifying weights inplace (#42843)
1 parent 17c6798 commit 2d9ca9d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas/core/sample.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ def preprocess_weights(obj: FrameOrSeries, weights, axis: int) -> np.ndarray:
6363
if (weights < 0).any():
6464
raise ValueError("weight vector many not include negative values")
6565

66-
weights[np.isnan(weights)] = 0
66+
missing = np.isnan(weights)
67+
if missing.any():
68+
# Don't modify weights in place
69+
weights = weights.copy()
70+
weights[missing] = 0
6771
return weights
6872

6973

pandas/tests/frame/methods/test_sample.py

+18
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,24 @@ def test_sample_is_copy(self):
339339
with tm.assert_produces_warning(None):
340340
df2["d"] = 1
341341

342+
def test_sample_does_not_modify_weights(self):
343+
# GH-42843
344+
result = np.array([np.nan, 1, np.nan])
345+
expected = result.copy()
346+
ser = Series([1, 2, 3])
347+
348+
# Test numpy array weights won't be modified in place
349+
ser.sample(weights=result)
350+
tm.assert_numpy_array_equal(result, expected)
351+
352+
# Test DataFrame column won't be modified in place
353+
df = DataFrame({"values": [1, 1, 1], "weights": [1, np.nan, np.nan]})
354+
expected = df["weights"].copy()
355+
356+
df.sample(frac=1.0, replace=True, weights="weights")
357+
result = df["weights"]
358+
tm.assert_series_equal(result, expected)
359+
342360
def test_sample_ignore_index(self):
343361
# GH 38581
344362
df = DataFrame(

0 commit comments

Comments
 (0)