Skip to content

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

Closed
@anjanibhat

Description

@anjanibhat
  • 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

Metadata

Metadata

Assignees

Labels

BugGroupbyMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions