"Cannot cast array data" when using numpy functions on Series

4,410 views
Skip to first unread message

Brendan Barnwell

unread,
Oct 31, 2013, 5:46:12 PM10/31/13
to pyd...@googlegroups.com
The following fails on 32-bit windows with pandas 0.12:

>>> import pandas
>>> import numpy as np
>>> x = pandas.Series([1, 2, 3])
>>> np.repeat([1, 2, 3], x)
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    np.repeat([1, 2, 3], x)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 343, in repeat
    return _wrapit(a, 'repeat', repeats, axis)
  File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 38, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'

Needless to say this is surprising behavior.  Is this a variation of https://github.com/pydata/pandas/issues/3579 or something else?  It works on 64-bit windows, so it seems to be a similar issue with conversion to platform int size.

Jeff

unread,
Nov 1, 2013, 11:24:13 AM11/1/13
to pyd...@googlegroups.com
This IMHO is a numpy bug because numpy does not handle the type conversion (the series is int64) properly. Its in a c-function, so not sure what they are doing
 
you can do this
 
np.repeat([1,2,3],x.astype(int32))
 
I think that numpy on a 32-bit platform doesn't deal well with the upcasting to int64 (or is not expecting it).
 
np.repeat([1,2,3],np.array([1,2,3],dtype=np.int64) yields the same error, so definitely a numpy bug
pls file a report with numpy! (it also maybe that this is undefined/unimplemented behavior)
Reply all
Reply to author
Forward
0 new messages