BUG: fillna with limit and no method ignores axis='columns' · Issue #40989 · pandas-dev/pandas (original) (raw)


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