28

I have a list like this:

l=[(1,2),(3,4)]

I want to convert it to a numpy array,and keep array item type as tuple:

array([(1,2),(3,4)])

but numpy.array(l) will give:

array([[1,2],[3,4)]])

and item type has been changed from tuple to numpy.ndarray,then I specified item types

numpy.array(l,numpy.dtype('float,float'))

this gives:

 array([(1,2),(3,4)])

but item type isn't tuple but numpy.void,so question is:

 how to convert it to a numpy.array of tuple,not of numpy.void? 

4 Answers 4

30

You can have an array of object dtype, letting each element of the array being a tuple, like so -

out = np.empty(len(l), dtype=object)
out[:] = l

Sample run -

In [163]: l = [(1,2),(3,4)]

In [164]: out = np.empty(len(l), dtype=object)

In [165]: out[:] = l

In [172]: out
Out[172]: array([(1, 2), (3, 4)], dtype=object)

In [173]: out[0]
Out[173]: (1, 2)

In [174]: type(out[0])
Out[174]: tuple
Sign up to request clarification or add additional context in comments.

2 Comments

Why does that work but np.array([(1,2),(3,4)], dtype=object) doesn't?
Recall that you manually create 2d numpy arrays (matrices) with the notation np.array([ [1,2], [2,3] ]) so you want np.array to be able to recursively unpack lists. Unfortunately this is not desired in this case. With the other method you avoid the np.array initialization and this way numpy does not recurse into the hierarchy
10

For some reason you can't simply do this if you're looking for a single line of code (even though Divakar's answer ultimately leaves you with dtype=object):

np.array([(1,2),(3,4)], dtype=object)

Instead you have to do this:

np.array([(1,2),(3,4)], dtype="f,f")

"f,f" signals to the array that it's receiving tuples of two floats (or you could use "i,i" for integers). If you wanted, you could cast back to an object by adding .astype(object) to the end of the line above).

1 Comment

To give an example of when the "f,f" works but object does not: I had a numpy array of tuples and wanted to check if each tuple existed in another array of tuples with np.isin(). Using object array did not work but using "f,f"array did work.
2
>>> np.fromiter(l, object)
array([(1, 2), (3, 4)], dtype=object)

np.fromiter supports object dtype since NumPy 1.23. https://numpy.org/doc/stable/reference/generated/numpy.fromiter.html

Comments

0

Just discovered that pandas has a way to take care of this. You can use their MultiIndex class to create an array of tuples since all pandas Indexes are wrapped 1-D numpy arrays. It's as easy as calling the Index constructor on a list of tuples.

>>> import pandas as pd
>>> tups = [(1, 2), (1, 3)]
>>> tup_array = pd.Index(tups).values
>>> print(type(tup_array), tup_array)
<class 'numpy.ndarray'> [(1, 2) (1, 3)]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.