1

Is there a way to convert a numpy ndarray (numpy.array) to a standard-library array (array.array) without reallocating the data?

For the record it is possible to convert an array.array to a ndarray using the buffer interface, so I hope the way round is possible:

import numpy
import array
a_std = array.array('d', [1, 2, 3])
a_np = numpy.ndarray(shape=(3, ), buffer=a_std, dtype='d')
a_np[0] = 666.
assert a_std[0] == 666.
1
  • try using python-c api, I know you can get the direct adress of where numpy saves the array data from c Commented Dec 23, 2016 at 12:45

1 Answer 1

1

My best guess so far is that it is not possible: memory reallocation cannot be avoided.

The fastest way I have found to convert my numpy array to an array.array is to use ndarray.tobytes():

import numpy
import array
a_np = np.random.uniform(size=(10 * 1000 * 1000))  # 76 MiB
a_std = array.array('d', a_np.tobytes())

numpy.testing.assert_allclose(a_std, a_np)

Quick benchmark with other methods (using IPython):

%timeit a_std = array.array('d', a_np.tobytes())
10 loops, best of 3: 56.8 ms per loop

%timeit a_std = array.array('d', a_np.data)
1 loop, best of 3: 946 ms per loop

%timeit a_std = array.array('d', a_np)
1 loop, best of 3: 1.17 s per loop
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.