question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pandas groupby sum min_count misbehaves

See original GitHub issue

Code Sample, a copy-pastable example if possible

d=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
d['col2']=d['col2'].astype(str)
d['col1']=np.nan
d=d.groupby(lambda x:x, axis=1).sum(min_count=1)

Problem description

My hope is that, by using min_count=1, pandas will return NaN when all columns being summed up are NaN. However, now it is returning 0 instead of NaN. Any idea why?

Actual output:

 col1  col2
0   0.0   3.0
1   0.0   4.0

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

   col1  col2
0   Nan   3.0
1   Nan   4.0

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None python: 2.7.15.final.0 python-bits: 64 OS: Darwin OS-release: 18.2.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.23.4 pytest: 3.8.2 pip: 10.0.1 setuptools: 39.1.0 Cython: None numpy: 1.15.4 scipy: 1.1.0 pyarrow: None xarray: None IPython: 5.8.0 sphinx: None patsy: 0.5.0 dateutil: 2.7.3 pytz: 2018.5 blosc: None bottleneck: 1.2.1 tables: None numexpr: None feather: None matplotlib: 2.2.3 openpyxl: None xlrd: 1.1.0 xlwt: None xlsxwriter: None lxml: None bs4: 4.6.3 html5lib: 1.0.1 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
wbijstercommented, May 15, 2019

FYI, I have the same issue. I use groupby to sum data and I want to retain the NaNs if there is no data in a group but have a sum if the group does contain data, even if there are some NaNs. I use this workaround now:

def sumgroup(s):  
    s = s.sum(min_count=1)
    return(s)

dftest['new'] = dftest.groupby(level=['one', 'two'])['data'].apply(sumgroup) # pd.__version__ == 0.24.2
0reactions
sv1990commented, Jan 10, 2022

The min_count parameter still doesn’t work when used as

d=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
d['col2']=d['col2'].astype(str)
d['col1']=np.nan
d=d.groupby(lambda x:x, axis=1).agg({'col1': 'sum'}, min_count=1)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas groupby sum resulting in blanks - Stack Overflow
Right now I am using the below code which is working for the cumsum but not for the sum. The Cumulative Sub Budget...
Read more >
pandas.core.groupby.GroupBy.sum
If fewer than min_count non-NA values are present the result will be NA. Returns. Series or DataFrame. Computed sum of values within each...
Read more >
How to use Pandas Groupby Function [Detailed Tutorial]
0:00 - Pandas Groupby Aggregate. How to use pandas groupby function by "KEY" and aggregate measures. Pandas groupby sum.6:56 - Pandas ...
Read more >
PYTHON : Pandas sum by groupby, but exclude certain columns
PYTHON : Pandas sum by groupby, but exclude certain columns [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] ...
Read more >
Python:Pandas | GroupBy | .sum() - Codecademy
Produces a new Series or DataFrame with aggregate sums for the groups in a GroupBy object. ... groupbyobject.sum(numeric_only, min_count).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found