0

I am trying to create an empty numpy array and then insert newly created arrays into than one. It is important for me not to shape the first numpy array and it has to be empty and then I can be able to add new numpy arrays with different sizes into that one. Something like the following:

A = numpy.array([])
B = numpy.array([1,2,3])
C = numpy.array([5,6])
A.append(B, axis=0)
A.append(C, axis=0)

and I want A to look like this:

[[1,2,3],[5,6]]

When I do the append command I get the following error:

AttributeError: 'numpy.ndarray' object has no attribute 'append'

Any idea how this can be done?

PS: This is not similar to the questions asked before because I am not trying to concatenate two numpy arrays. I am trying to insert a numpy array to another empty numpy array. I know how to do this using lists but it has to be numpy array.

Thanks

ahajib
  • 12,838
  • 29
  • 79
  • 120
  • possible duplicate of [**Append a NumPy array to a NumPy array**](http://stackoverflow.com/questions/9775297/append-a-numpy-array-to-a-numpy-array) – mbomb007 Jun 09 '15 at 21:53
  • 2
    Are you sure you want to create an `numpy.array` like this? When each row has different number of columns, you have `numpy.array` of type `object` that loses most of nice `numpy` features. For something likes consider using plain python `list`. – Akavall Jun 09 '15 at 21:54
  • Akavail is right. I assumed you wanted a python list as output. If you want a numpy array, it's a ragged array and you'd have to fill with something, e.g. `np.nan`: array([[1,2,3],[5,6,nan]]) – rjonnal Jun 09 '15 at 22:00
  • Please flag as a duplicate. Thanks. – mbomb007 Jun 09 '15 at 22:03
  • Would `A = numpy.array([B,C])` be an unacceptable workaround? – River Jun 09 '15 at 22:12
  • 1
    @mbomb007 This is not a duplicate and I explained why. Thanks – ahajib Jun 09 '15 at 22:34
  • 1
    Nimafl, you haven't answered Akavail's question, which is important: will an array of objects suffice? or are you wanting a ragged array with an empty placeholder like -1 or nan? – rjonnal Jun 09 '15 at 22:42
  • @rjonnal Having a nan should be fine. But the important thing here is that I do not know the size of any of those arrays beforehand. – ahajib Jun 09 '15 at 22:45
  • What aren't you working with lists, or lists of lists? What do you need to do with these objects that makes using `np.array` better? – hpaulj Jun 09 '15 at 22:57
  • @hpaulj There is this larger code that I am trying to insert mine into it. But it seems that it can not be done this way – ahajib Jun 09 '15 at 23:01

3 Answers3

3

You can't do that with numpy arrays, because a real 2D numpy is rectangular. For example, np.arange(6).reshape(2,3) return array([[0, 1, 2],[3, 4, 5]]). if you really want to do that, try array([array([1,2,3]),array([5,6])]) which create array([array([1, 2, 3]), array([5, 6])], dtype=object) But you will loose all the numpy power with misaligned data.

B. M.
  • 18,243
  • 2
  • 35
  • 54
2

You can do this by converting the arrays to lists:

In [21]: a = list(A)
In [22]: a.append(list(B))
In [24]: a.append(list(C))
In [25]: a
Out[25]: [[1, 2, 3], [5, 6]]

My intuition is that there's a much better solution (either more pythonic or more numpythonic) than this, which might be gleaned from a more complete description of your problem.

rjonnal
  • 1,137
  • 7
  • 17
0

Taken from here. Maybe search for existing questions first.

numpy.append(M, a)
Community
  • 1
  • 1
mbomb007
  • 3,788
  • 3
  • 39
  • 68