Q:

Fastest way to changes rows in a Series by slicing in pandas?

I have a large Series datastructure in pandas and I need to edit certain values using slicing. I start with the following code:
import numpy as np
import pandas as pd

vec = np.random.randn(1e7)
sr  = pd.Series(vec)
idx = (np.random.rand(10000)*1000).astype(int)

Replacing elements in vec using idx is much faster
timeit vec[idx] = 1.2
10000 loops, best of 3: 32.5 µs per loop

than for the pandas structure
timeit sr[idx] = 1.2
1 loops, best of 3: 391 µs per loop

timeit sr.iloc[idx] = 1.2
1000 loops, best of 3: 201 µs per loop

Is there any way to get the same performance with pandas as with numpy for this specific operation?

A:

sr.values[idx] = 1.2 has performance comparable to vec[idx] = 1.2:
In [101]: %timeit vec[idx] = 1.2
10000 loops, best of 3: 25.8 µs per loop

In [102]: %timeit sr.values[idx] = 1.2
10000 loops, best of 3: 27.3 µs per loop

