BUG: Fix for fillna ignoring axis=1 parameter (issues #17399 #17409)
State: closed

- [x] closes #17399 #17409 
- [x] tests added / passed
- [ ] passes `black pandas`
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry


Labels:
name: Bug
name: Missing-data
description: np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments:
Hello @jorvis! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:





* In the file [`pandas/tests/frame/test_missing.py`](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py):

> [Line 675:9](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L675): [E265](https://duckduckgo.com/?q=pep8%20E265) block comment should start with '# '
> [Line 677:20](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L677): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 677:22](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L677): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 677:24](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L677): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 678:20](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L678): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 678:27](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L678): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 678:29](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L678): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 679:20](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L679): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 679:23](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L679): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 679:26](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L679): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 682:20](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L682): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 682:22](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L682): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 682:24](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L682): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 683:20](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L683): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 683:22](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L683): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 683:24](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L683): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 684:20](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L684): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 684:23](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L684): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','
> [Line 684:26](https://github.com/pandas-dev/pandas/blob/e1863faa822c45a927d924c86f35b044729d0d61/pandas/tests/frame/test_missing.py#L684): [E231](https://duckduckgo.com/?q=pep8%20E231) missing whitespace after ','



##### Comment last updated at 2019-09-19 21:56:54 UTC
Here's an example of this issue in Jupyter:

https://imgur.com/a/3ZrXqSv
When you do `DataFrame.fillna(Series)` (with no `axis=1`) the indices are
aligned. In your test example,
nothing is filled because the index of the Series is the original columns
from the DataFrame.

On Thu, Sep 19, 2019 at 5:52 PM Joshua Orvis <notifications@github.com>
wrote:

> *@jorvis* commented on this pull request.
> ------------------------------
>
> In pandas/tests/frame/test_missing.py
> <https://github.com/pandas-dev/pandas/pull/28441#discussion_r326414395>:
>
> > @@ -671,6 +671,21 @@ def test_fillna_columns(self):
>          expected = df.astype(float).fillna(method="ffill", axis=1)
>          assert_frame_equal(result, expected)
>
> +    def test_fillna_rows(self):
> +        #GH17399
> +        df = pd.DataFrame({
> +            "a": [1,2,3,4],
> +            "b": [5,np.nan,7,8],
> +            "c": [9,10,11,np.nan]})
> +
> +        expected = pd.DataFrame({
> +            "a": [1,2,3,4],
> +            "b": [5,6,7,8],
> +            "c": [9,10,11,6]})
> +
> +        result = df.fillna(df.mean(axis=1))
>
> Maybe we should back up and make sure the behavior I'm seeing is expected
> then an not a bug. In this notebook <https://imgur.com/a/3ZrXqSv> I first
> try to replace NaNs with the column average, then the row average. Doing
> the first works, but the second silently does nothing.
>
> Setting axis=1 on fillna when using the mean where axis=1 causes an error:
> Currently only can fill with dict/Series column by column.
>
> It seems like calling fillna on a frame with NaNs which doesn't fill them
> is an issue.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/pull/28441?email_source=notifications&email_token=AAKAOIVGDMDSGU4CO7NQQBTQKP7CJA5CNFSM4IWVUUTKYY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOCFLIMWA#discussion_r326414395>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAKAOIWKL2FSS47XJ5KLIELQKP7CJANCNFSM4IWVUUTA>
> .
>

Can you give an example of the syntax to use to fill using the row means?  
Thanks for the PR @jorvis but I think this has gone stale. If not and you'd like to pick back up just ping and can continue
Question. I reported this bug (#17399) back in 2017, I was going to work on fixing this back then but it seems that pratapvardhan worked on the problem.  Again someone else found this issue and worked on it.  I am not clear what needs to be done to close this issue.   It appears that the last test case missed the mark to address the original problem.  

What do we need to close this?   Write a new test case?