Skip to content

BUG: fillna with limit and no method ignores axis='columns' #40989

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

Closed
2 of 3 tasks
TheNeuralBit opened this issue Apr 16, 2021 · 1 comment · Fixed by #42856
Closed
2 of 3 tasks

BUG: fillna with limit and no method ignores axis='columns' #40989

TheNeuralBit opened this issue Apr 16, 2021 · 1 comment · Fixed by #42856
Labels
Bug Docs Error Reporting Incorrect or improved errors from pandas Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@TheNeuralBit
Copy link
Contributor

TheNeuralBit commented Apr 16, 2021

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

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

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


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

In [6]: df = pd.DataFrame([[np.nan, 2, np.nan, 0],
   ...:                    [3, 4, np.nan, 1],
   ...:                    [np.nan, np.nan, np.nan, 5],
   ...:                    [np.nan, 3, np.nan, 4]],
   ...:                    columns=list('ABCD'))

In [7]: df
Out[7]: 
     A    B   C  D
0  NaN  2.0 NaN  0
1  3.0  4.0 NaN  1
2  NaN  NaN NaN  5
3  NaN  3.0 NaN  4

In [8]: df.fillna(axis='columns', value=100, limit=2)
Out[8]: 
       A      B      C  D
0  100.0    2.0  100.0  0
1    3.0    4.0  100.0  1
2  100.0  100.0    NaN  5
3    NaN    3.0    NaN  4

In [9]: df.fillna(axis='columns', value=100, limit=1)
Out[9]: 
       A      B      C  D
0  100.0    2.0  100.0  0
1    3.0    4.0    NaN  1
2    NaN  100.0    NaN  5
3    NaN    3.0    NaN  4

Problem description

Based on the description of limit:

If method is not specified, this is the maximum number of entries along the entire axis where NaNs will be filled. Must be greater than 0 if not None.

I would expect it to apply along the columns axis in this case, but it seems to be applied along the index. It's possible this is intentional, but if that's the case I'd like to update the documentation

Expected Output

In [8]: df.fillna(axis='columns', value=100, limit=2)
Out[8]: 
       A      B      C  D
0  100.0    2.0  100.0  0
1    3.0    4.0  100.0  1
2  100.0  100.0    NaN  5
3  100.0    3.0  100.0  4

In [9]: df.fillna(axis='columns', value=100, limit=1)
Out[9]: 
       A      B      C  D
0  100.0    2.0    NaN  0
1    3.0    4.0  100.0  1
2  100.0    NaN    NaN  5
3  100.0    3.0    NaN  4

Output of pd.show_versions()

pandas : 1.2.4 numpy : 1.19.5 pytz : 2019.3 dateutil : 2.8.1 pip : 20.2.1 setuptools : 49.2.1 Cython : 0.29.13 pytest : 4.6.11 hypothesis : None sphinx : 1.8.5 blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : 2.8.6 (dt dec pq3 ext lo64) jinja2 : 2.11.2 IPython : 7.19.0 pandas_datareader: None bs4 : None bottleneck : None fsspec : None fastparquet : None gcsfs : None matplotlib : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : 3.0.0 pyxlsb : None s3fs : None scipy : 1.4.1 sqlalchemy : 1.3.20 tables : None tabulate : None xarray : None xlrd : None xlwt : None numba : None
@TheNeuralBit TheNeuralBit added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 16, 2021
@TheNeuralBit TheNeuralBit changed the title QST: fillna with limit and no method ignores axis='columns' BUG: fillna with limit and no method ignores axis='columns' Apr 16, 2021
@phofl
Copy link
Member

phofl commented Apr 16, 2021

Should probably raise a NotImplementedError like

df.fillna(axis='columns', value={1: 100}, limit=2)

I think adjustment to the docs would be good too

@phofl phofl added Docs Error Reporting Incorrect or improved errors from pandas Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 16, 2021
GYvan added a commit to GYvan/pandas that referenced this issue Aug 2, 2021
@jreback jreback added this to the 1.4 milestone Aug 20, 2021
phofl pushed a commit that referenced this issue Aug 20, 2021
…na. (#42856)

* solves issue #40989 by raising NotImplementedError and modifying docs

* Testing the NotImplementedError created to solve issue #40989

* pre-commit error fixed

* added modified generic.py

* added modified test_fillna.py

* reverted changes for test_frame.py

* added another test

* added into bug fixes in whatsnew
feefladder pushed a commit to feefladder/pandas that referenced this issue Sep 7, 2021
…na. (pandas-dev#42856)

* solves issue pandas-dev#40989 by raising NotImplementedError and modifying docs

* Testing the NotImplementedError created to solve issue pandas-dev#40989

* pre-commit error fixed

* added modified generic.py

* added modified test_fillna.py

* reverted changes for test_frame.py

* added another test

* added into bug fixes in whatsnew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Docs Error Reporting Incorrect or improved errors from pandas Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants