I came across some behavior that I find odd and replicated it. simply, why does
np.int64(np.float64(np.nan))
output
-9223372036854775808
(as pointed out in comments, yes this is -2^63, the maximum negative value for a two-sided int64)
In case it is relevant or of interest, my original use case was looking at dataframe indices of type np.float64 and converting to np.int64 (I don't normally nest types for no reason as in the simplified example above). Starting with an example dataframe:
0 1
NaN 1 2
1.0 3 4
NaN 5 6
then running:
print(df.index.values[0])
print(type(df.index.values[0]))
print(df.index.values[0].astype(np.int64))
print(type(df.index.values[0].astype(np.int64)))
prints:
nan
<class 'numpy.float64'>
-9223372036854775808
<class 'numpy.int64'>
However, using python types you can't convert float nan to int:
print(np.nan)
print(type(np.nan))
print(np.nan.astype(np.int64))
out:
nan
<class 'float'>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-130-0d779433eac7> in <module>
1 print(np.nan)
2 print(type(np.nan))
----> 3 print(np.nan.astype(np.int64))
AttributeError: 'float' object has no attribute 'astype'
Although in practice I was able to just change the nans to a value I knew would not be a key (0) - I was curious why do class np.float64 types behave this way?