Skip to content

BUG: fillna(method="ffill") and ffill() on DataFrameGroupBy gives different results #34725

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
anjanibhat opened this issue Jun 12, 2020 · 2 comments · Fixed by #36790
Closed
2 of 3 tasks
Assignees
Labels
Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@anjanibhat
Copy link

  • 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

# Your code here

import numpy as np
import pandas as pd
df = pd.DataFrame({'a':[1, 1, np.NaN, np.NaN], 'b':[10, np.NaN, 40, np.NaN]})
df
     a     b
0  1.0  10.0
1  1.0   NaN
2  NaN  40.0
3  NaN   NaN
# Case 1 - Forward fill using fillna
df.groupby(["a"]).fillna(method="ffill")

df
      b
0  10.0
1  10.0
2   NaN
3   NaN
# Case 2 - Forward fill using ffill
df.groupby(["a"]).ffill()

df
      b
0  10.0
1  10.0
2  40.0
3  40.0

Problem description

df.groupby(["a"]).ffill() is not ignoring the NaN group while performing forward fill.

Expected Output

df.groupby(["a"]).ffill() should give the same output as df.groupby(["a"]).fillna(method="ffill") since NaN groups are ignored as per https://pandas.pydata.org/pandas-docs/stable/user_guide/missing_data.html#na-values-in-groupby.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 16.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.0.4
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 44.0.0.post20200106
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.2
numexpr : None
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 0.17.0
pytables : None
pytest : 5.4.3
pyxlsb : None
s3fs : 0.4.2
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.8.6
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@anjanibhat anjanibhat added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 12, 2020
@anjanibhat anjanibhat changed the title BUG: fillna(method="ffill") and ffill() on DataFrameGroupBy gives inconsistent results BUG: fillna(method="ffill") and ffill() on DataFrameGroupBy gives different results Jun 12, 2020
@TomAugspurger
Copy link
Contributor

Thanks for the report. These should match but this looks pretty tricky to fix.

@TomAugspurger TomAugspurger added Groupby 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 Jun 12, 2020
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Jun 12, 2020
@smithto1
Copy link
Member

smithto1 commented Aug 8, 2020

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants