The Python Oracle

Convert Pandas column containing NaNs to dtype `int`

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay

--

Chapters
00:00 Convert Pandas Column Containing Nans To Dtype `Int`
00:42 Accepted Answer Score 264
00:54 Answer 2 Score 82
01:13 Answer 3 Score 365
01:45 Answer 4 Score 13
02:05 Thank you

--

Full question
https://stackoverflow.com/questions/2128...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe #nan #dtype

#avk47



ANSWER 1

Score 365


In version 0.24.+ pandas has gained the ability to hold integer dtypes with missing values.

Nullable Integer Data Type.

Pandas can represent integer data with possibly missing values using arrays.IntegerArray. This is an extension types implemented within pandas. It is not the default dtype for integers, and will not be inferred; you must explicitly pass the dtype into array() or Series:

arr = pd.array([1, 2, np.nan], dtype=pd.Int64Dtype())
pd.Series(arr)

0      1
1      2
2    NaN
dtype: Int64

For convert column to nullable integers use:

df['myCol'] = df['myCol'].astype('Int64')



ACCEPTED ANSWER

Score 264


The lack of NaN rep in integer columns is a pandas "gotcha".

The usual workaround is to simply use floats.




ANSWER 3

Score 82


My use case is munging data prior to loading into a DB table:

df[col] = df[col].fillna(-1)
df[col] = df[col].astype(int)
df[col] = df[col].astype(str)
df[col] = df[col].replace('-1', np.nan)

Remove NaNs, convert to int, convert to str and then reinsert NANs.

It's not pretty but it gets the job done!




ANSWER 4

Score 13


It is now possible to create a pandas column containing NaNs as dtype int, since it is now officially added on pandas 0.24.0

pandas 0.24.x release notes Quote: "Pandas has gained the ability to hold integer dtypes with missing values