Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: .fillna({}) doesn't work #44030

Open
3 tasks done
q121212 opened this issue Oct 14, 2021 · 2 comments
Open
3 tasks done

BUG: .fillna({}) doesn't work #44030

q121212 opened this issue Oct 14, 2021 · 2 comments
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@q121212
Copy link

q121212 commented Oct 14, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame([1,2,np.nan, pd.NA], columns=['a'])

# case 1
df.fillna({})

# case 2
df.fillna({np.nan: {}, pd.NA: {}})

Issue Description

.fillna() doesn't work as expected for case then value = {}

as written in pandas.DataFrame.fillna:

DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)

Fill NA/NaN values using the specified method.

Parameters

value scalar, dict, Series, or DataFrame

Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). Values not in the dict/Series/DataFrame will not be filled. This value cannot be a list.

Expected Behavior

all values (np.nan and pd.NA) should be changed to {}

Installed Versions

INSTALLED VERSIONS

commit : 73c6825
python : 3.9.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.47-linuxkit
Version : #1 SMP Sat Jul 3 21:51:47 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.3
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : 0.29.24
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.27.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
fsspec : 2021.08.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.23
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : 0.54.0

@q121212 q121212 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 14, 2021
@ShivnarenSrinivasan
Copy link
Contributor

I don't think this will be considered a bug—have you tried inserting an empty set/dictionary into a dataframe?

To your example:

>>> df = pd.DataFrame([1,2,np.nan, pd.NA], columns=['a'])
>>> df
      a
0     1
1     2
2   NaN
3  <NA>

# appending a dict
>>> df.loc[4] = {}
>>> df
      a
0     1
1     2
2   NaN
3  <NA>
4   NaN

The empty dict is considered as NaN.

An empty set, however, is stored as is.

@q121212
Copy link
Author

q121212 commented Oct 15, 2021

The empty dict is NaN after type casting only. But imho in considered case .fillna({}) should be works without type casting and just replace NaN with the specified value, whatever it is.

I used workaround:

>>> df.loc[df['a'].isnull(), 'a'] = [{}]
>>> df
      a
0     1
1     2
2    {}
3    {}

Also in @ShivnarenSrinivasan example works in the same manner:

>>> df.loc[4] = [{}]
>>> df
      a
0     1
1     2
2    {}
3    {}
4    {}

@rhshadrach rhshadrach added the Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate label Oct 16, 2021
@mroeschke mroeschke added Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

No branches or pull requests

4 participants