Re: [Numpy-discussion] Can't seem to use np.insert() or np.append() for structured arrays

2014-08-30 Thread Sebastian Berg
On Fr, 2014-08-29 at 22:10 -0400, Benjamin Root wrote:
 Consider the following:
 
 a = np.array([(1, 'a'), (2, 'b'), (3, 'c')], dtype=[('foo', 'i'),
 ('bar', 'a1')])
 
 b = np.append(a, (4, 'd'))
 Traceback (most recent call last):
   File stdin, line 1, in module
   File
 /home/ben/miniconda/lib/python2.7/site-packages/numpy/lib/function_base.py, 
 line 3555, in append
 return concatenate((arr, values), axis=axis)
 TypeError: invalid type promotion
 b = np.insert(a, 4, (4, 'd'))
 Traceback (most recent call last):
   File stdin, line 1, in module
   File
 /home/ben/miniconda/lib/python2.7/site-packages/numpy/lib/function_base.py, 
 line 3464, in insert
 new[slobj] = values
 ValueError: could not convert string to float: d
 

Ooops, nice bug in there, might have been me :) (will open a PR). 

- Sebastian

 
 In my original code snippet I was developing which has a more involved
 dtype, I actually got a different exception:
 b = np.append(a, c)
 Traceback (most recent call last):
   File stdin, line 1, in module
   File
 /home/ben/miniconda/lib/python2.7/site-packages/numpy/lib/function_base.py, 
 line 3553, in append
 values = ravel(values)
   File
 /home/ben/miniconda/lib/python2.7/site-packages/numpy/core/fromnumeric.py, 
 line 1367, in ravel
 return asarray(a).ravel(order)
   File
 /home/ben/miniconda/lib/python2.7/site-packages/numpy/core/numeric.py, line 
 460, in asarray
 return array(a, dtype, copy=False, order=order)
 ValueError: setting an array element with a sequence.
 
 
 Luckily, this works as a work-around:
  b = np.append(a, np.array([(4, 'd')], dtype=a.dtype))
  b
 array([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')], 
   dtype=[('foo', 'i'), ('bar', 'S1')])
 
 
 
 The same happens whether I enclose the value with square bracket or
 not. I suspect that this array type just wasn't considered when its
 checking logic was developed. This is with 1.8.2 from miniconda.
 Should we consider this a bug or are structured arrays just not
 expected to be modified like this?
 
 
 Cheers!
 
 Ben Root
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can't seem to use np.insert() or np.append() for structured arrays

2014-08-30 Thread Sebastian Berg
On Sa, 2014-08-30 at 09:04 +0100, Sebastian Berg wrote:
 On Fr, 2014-08-29 at 22:10 -0400, Benjamin Root wrote:
  Consider the following:
  
  a = np.array([(1, 'a'), (2, 'b'), (3, 'c')], dtype=[('foo', 'i'),
  ('bar', 'a1')])
  
  b = np.append(a, (4, 'd'))
  Traceback (most recent call last):
File stdin, line 1, in module
File
  /home/ben/miniconda/lib/python2.7/site-packages/numpy/lib/function_base.py,
   line 3555, in append
  return concatenate((arr, values), axis=axis)
  TypeError: invalid type promotion
  b = np.insert(a, 4, (4, 'd'))
  Traceback (most recent call last):
File stdin, line 1, in module
File
  /home/ben/miniconda/lib/python2.7/site-packages/numpy/lib/function_base.py,
   line 3464, in insert
  new[slobj] = values
  ValueError: could not convert string to float: d
  
 

Actually, for insert it is easy to fix
(https://github.com/numpy/numpy/pull/5022), for append there are some
difficulties, because the dtype is not forced to be the arrays dtype,
but gotten from both the original and the appended value currently.

- Sebastian


 Ooops, nice bug in there, might have been me :) (will open a PR). 
 
 - Sebastian
 
  
  In my original code snippet I was developing which has a more involved
  dtype, I actually got a different exception:
  b = np.append(a, c)
  Traceback (most recent call last):
File stdin, line 1, in module
File
  /home/ben/miniconda/lib/python2.7/site-packages/numpy/lib/function_base.py,
   line 3553, in append
  values = ravel(values)
File
  /home/ben/miniconda/lib/python2.7/site-packages/numpy/core/fromnumeric.py,
   line 1367, in ravel
  return asarray(a).ravel(order)
File
  /home/ben/miniconda/lib/python2.7/site-packages/numpy/core/numeric.py, 
  line 460, in asarray
  return array(a, dtype, copy=False, order=order)
  ValueError: setting an array element with a sequence.
  
  
  Luckily, this works as a work-around:
   b = np.append(a, np.array([(4, 'd')], dtype=a.dtype))
   b
  array([(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')], 
dtype=[('foo', 'i'), ('bar', 'S1')])
  
  
  
  The same happens whether I enclose the value with square bracket or
  not. I suspect that this array type just wasn't considered when its
  checking logic was developed. This is with 1.8.2 from miniconda.
  Should we consider this a bug or are structured arrays just not
  expected to be modified like this?
  
  
  Cheers!
  
  Ben Root
  
  ___
  NumPy-Discussion mailing list
  NumPy-Discussion@scipy.org
  http://mail.scipy.org/mailman/listinfo/numpy-discussion
 
 
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion
 


___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] inplace unary operations?

2014-08-30 Thread Nathaniel Smith
On Sat, Aug 30, 2014 at 6:43 PM,  josef.p...@gmail.com wrote:
 Is there a way to negate a boolean, or to change the sign of a float inplace
 ?

np.logical_not(arr, out=arr)
np.negative(arr, out=arr)

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] inplace unary operations?

2014-08-30 Thread Benjamin Root
Random thoughts are the best kinds of thoughts! I didn't even know there
was a np.negative() function! I will keep this card up my sleeve at work
for one of those save-the-day moments in optimization.

Cheers!
Ben Root


On Sat, Aug 30, 2014 at 1:45 PM, Nathaniel Smith n...@pobox.com wrote:

 On Sat, Aug 30, 2014 at 6:43 PM,  josef.p...@gmail.com wrote:
  Is there a way to negate a boolean, or to change the sign of a float
 inplace
  ?

 np.logical_not(arr, out=arr)
 np.negative(arr, out=arr)

 -n

 --
 Nathaniel J. Smith
 Postdoctoral researcher - Informatics - University of Edinburgh
 http://vorpus.org
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] inplace unary operations?

2014-08-30 Thread Nathaniel Smith
On Sat, Aug 30, 2014 at 7:39 PM, Benjamin Root ben.r...@ou.edu wrote:
 Random thoughts are the best kinds of thoughts! I didn't even know there was
 a np.negative() function!

Me neither, I had to look it up :-)

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion