Convert Pandas column containing NaNs to dtype `int`
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 Question
00:59 Accepted answer (Score 257)
01:16 Answer 2 (Score 312)
02:06 Answer 3 (Score 70)
02:32 Answer 4 (Score 11)
02:49 Thank you
--
Full question
https://stackoverflow.com/questions/2128...
Accepted answer links:
[pandas "gotcha"]: http://pandas.pydata.org/pandas-docs/sta...
Answer 2 links:
[Nullable Integer Data Type]: http://pandas.pydata.org/pandas-docs/sta...
[arrays.IntegerArray]: http://pandas.pydata.org/pandas-docs/sta...
[array()]: http://pandas.pydata.org/pandas-docs/sta...
--
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.
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