Skip to content

astype(np.int32) converts np.inf to int32 min value #14265

Closed
@JonathanMace

Description

@JonathanMace

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

output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Linux
OS-release: 4.0.9-63_fbk11_3989_g7d5ca22
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.4
pip: 8.1.2
setuptools: 25.1.6
Cython: 0.22
numpy: 1.11.1
scipy: 0.18.0
statsmodels: 0.6.1
xarray: None
IPython: 5.0.0
sphinx: 1.2.3
patsy: 0.3.0
dateutil: 2.4.1
pytz: 2015.2
blosc: None
bottleneck: None
tables: 3.1.1
numexpr: 2.6.1
matplotlib: 1.4.3
openpyxl: 1.8.5
xlrd: 0.9.3
xlwt: 0.7.5
xlsxwriter: 0.6.7
lxml: 3.4.2
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 0.9.9
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.36.0
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions