NumPy or Pandas: Keeping array type as integer while having a NaN value

int, numpy, pandas, python, type-conversion

Solution

This capability has been added to pandas beginning with version 0.24.

At this point, it requires the use of extension dtype `'Int64'` (capitalized), rather than the default dtype `'int64'` (lowercase).

Problem

Is there a preferred way to keep the data type of a `numpy` array fixed as `int` (or `int64` or whatever), while still having an element inside listed as `numpy.NaN`? In particular, I am converting an in-house data structure to a Pandas DataFrame. In our structure, we have integer-type columns that still have NaN's (but the dtype of the column is int). It seems to recast everything as a float if we make this a DataFrame, but we'd really like to be `int`. Thoughts? Things tried: I tried using the `from_records()` function under pandas.DataFrame, with `coerce_float=False` and this did not help. I also tried using NumPy masked arrays, with NaN fill_value, which also did not work. All of these caused the column data type to become a float.

Original source

Related problems