Closed
Description
From #11497. Found inconsistencyr regarding fillna
downcasting.
I understand fillna(downcast=None)
should downcast values appropriately. It doesn't work on float
dtype.
# NG
pd.Series([1, 2, np.nan, 4]).fillna(3)
#0 1
#1 2
#2 3
#3 4
# dtype: float64
# OK
pd.Series([1, 2, np.nan, 4], dtype=object).fillna(3)
#0 1
#1 2
#2 3
#3 4
# dtype: int64
Based on the internal logic, downcast
can accept dtype. It works on float
, but not on object
.
# OK
pd.Series([1, 2, np.nan, 4]).fillna(3, downcast='int')
#0 1
#1 2
#2 3
#3 4
# dtype: int64
# NG
pd.Series([1, 2, np.nan, 4], dtype=object).fillna(3, downcast='int')
#0 1
#1 2
#2 3
#3 4
# dtype: object
I understood the expected behavior as below:
- all dtypes should be downcast by default (downcast=None)
- if any dtype is passed via
downcast
kw, downcast to the specified dtype if possible. downcast=False
will not perform downcast.- Add
Index.fillna
to API (follow-up of ENH: Add Index.fillna #11343)