You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that converting np.inf to int32 and int64 using pd.astype converts them to the min int value rather than throwing an exception. Presumably this is because they raise OverflowError and not ValueError
Code Sample, a copy-pastable example if possible
for typ in [np.int32, np.int64]:
for badval in [np.nan, np.inf]:
# Print the exception thrown by casting to numpy directly
try:
typ(badval)
except Exception as e:
print "np.%s(np.%s) throws %s: %s" % (typ.__name__, badval, type(e).__name__, e)
# Print the exception or resulting value by casting using pd.astype
try:
typed = pd.Series([badval]).astype(typ)
print "pd.Series([np.%s]).astype(np.%s) returns dataframe containing %s" % (badval, typ.__name__, typed[0])
print "Returned DataFrame:"
print typed
except Exception as e:
print "pd.Series([np.%s]).astype(np.%s) throws %s: %s" % (badval, typ.__name__, type(e).__name__, e)
print ""
Expected Output
np.int32(np.nan) throws ValueError: cannot convert float NaN to integer
pd.Series([np.nan]).astype(np.int32) throws ValueError: Cannot convert NA to integer
np.int32(np.inf) throws OverflowError: cannot convert float infinity to integer
pd.Series([np.inf]).astype(np.int32) returns dataframe containing -2147483648
Returned DataFrame:
0 -2147483648
dtype: int32
np.int64(np.nan) throws ValueError: cannot convert float NaN to integer
pd.Series([np.nan]).astype(np.int64) throws ValueError: Cannot convert NA to integer
np.int64(np.inf) throws OverflowError: cannot convert float infinity to integer
pd.Series([np.inf]).astype(np.int64) returns dataframe containing -9223372036854775808
Returned DataFrame:
0 -9223372036854775808
dtype: int64
It seems that converting np.inf to int32 and int64 using pd.astype converts them to the min int value rather than throwing an exception. Presumably this is because they raise OverflowError and not ValueError
Code Sample, a copy-pastable example if possible
Expected Output
output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: