Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
    
possible duplicate of Append a NumPy array to a NumPy array – mbomb007 Jun 9 '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 9 '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 9 '15 at 22:00
1  
@mbomb007 This is not a duplicate and I explained why. Thanks – nimafl Jun 9 '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 9 '15 at 22:42
up vote 3 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
No I don't want to use lists. It has to be a numpy array but thanks. – nimafl Jun 9 '15 at 22:39
    
It seems that I have to take care of using lists somehow but thanks for the effort anyways. – nimafl Jun 9 '15 at 23:14

Taken from here. Maybe search for existing questions first.

numpy.append(M, a)
share|improve this answer
    
These don't work. concatenate gives array([1.,2.,3.]) and vstack throws an error because of unequal sizes. – rjonnal Jun 9 '15 at 21:57
    
@rjonnal Then look at the next answer in the linked question. This question is a duplicate. – mbomb007 Jun 9 '15 at 22:03
    
mbomb007, it's not a duplicate. Look at the OP's desired output. – rjonnal Jun 9 '15 at 22:05
    
Where does the M come from? – rjonnal Jun 9 '15 at 22:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.