1

I'm trying to open up a pickle file of a large data set, but am running into an issue with the resultant data type. The result gives a list with an array inside, I need to unpack the list to get the array inside. I think it can be boiled down to the following example. Say I have

x = [array([1,1,1], [1,1,1])]

(type(x) = list)

I want to unpack this list so that it's just the array inside. I.e., I want

y = array([1,1,1], [1,1,1])

(type(y) = numpy.ndarray)

I'm relatively new to Python programming and could use a hand on how to do this easily. Please let me know if I need to clarify my question.

I'm not sure if it matters here, but I'm using Python 3.7.6.


EDIT

Looks like I made a mistake when putting in the array for x. To clarify, this is what I'm getting when I unpack the pickle file.

x = pandas.read_pickle(data_source)
print(x)
> [array([1,1,1],[1,1,1])]
0

2 Answers 2

1

With array, I presume you are having a numpy array and you can't have something like:

np.array([1,1,1], [1,1,1])

which throws a TypeError: data type not understood.

You have to wrap a square bracket from outside like so:

np.array([[1,1,1], [1,1,1]])

To answer your question, you can do:

y = x[0]

which makes y a <class 'numpy.ndarray'>.

1
  • Whoops, I knew that about the brackets. It was an oversight, sorry for the confusion.
    – NoVa
    Commented Apr 4, 2020 at 5:04
1

so for this case of :

x = [array([1,1,1], [1,1,1])]
np.concatenate(x).ravel()

this should give you this:

array([1,1,1], [1,1,1])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.