Skip to content

BUG: Grouper.cov emits warning unlike DataFrame.cov #27570

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
ghost opened this issue Jul 24, 2019 · 2 comments
Closed

BUG: Grouper.cov emits warning unlike DataFrame.cov #27570

ghost opened this issue Jul 24, 2019 · 2 comments
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Warnings Warnings that appear or should be added to pandas

Comments

@ghost
Copy link

ghost commented Jul 24, 2019

Like #27470, another case where Grouper and DataFrame provide essentially the same method but diverge in behavior.

import pandas as pd

df=pd.DataFrame(dict(a=[0,4,8],b=[2,7,12],c=[3,9,11]))
g=df.groupby([0,1,1])

# single row frame silently emits NaNs
df.iloc[[0]].cov()

# single row group emits multiple warnings
g.cov()
RuntimeWarning: Degrees of freedom <= 0 for slice 
@mroeschke mroeschke added Numeric Operations Arithmetic, Comparison, and Logical operations Warnings Warnings that appear or should be added to pandas labels Nov 2, 2019
@jbrockmendel
Copy link
Member

I'm not seeing warnings produced for the first call but now the second. Is the behavior you expect?

@mroeschke
Copy link
Member

mroeschke commented Jul 10, 2021

I see warnings in both cases now and since they are both coming from numpy I don't necessarily thing we should be testing this. So closing

In [2]: df.iloc[[0]].cov()
pandas/core/frame.py:9516: RuntimeWarning: Degrees of freedom <= 0 for slice
  base_cov = np.cov(mat.T, ddof=ddof)
python3.8/site-packages/numpy/lib/function_base.py:2542: RuntimeWarning: divide by zero encountered in true_divide
  c *= np.true_divide(1, fact)
python3.8/site-packages/numpy/lib/function_base.py:2542: RuntimeWarning: invalid value encountered in multiply
  c *= np.true_divide(1, fact)
Out[2]:
    a   b   c
a NaN NaN NaN
b NaN NaN NaN
c NaN NaN NaN

In [3]: g.cov()
pandas/core/frame.py:9516: RuntimeWarning: Degrees of freedom <= 0 for slice
  base_cov = np.cov(mat.T, ddof=ddof)
python3.8/site-packages/numpy/lib/function_base.py:2542: RuntimeWarning: divide by zero encountered in true_divide
  c *= np.true_divide(1, fact)
python3.8/site-packages/numpy/lib/function_base.py:2542: RuntimeWarning: invalid value encountered in multiply
  c *= np.true_divide(1, fact)
Out[3]:
        a     b    c
0 a   NaN   NaN  NaN
  b   NaN   NaN  NaN
  c   NaN   NaN  NaN
1 a   8.0  10.0  4.0
  b  10.0  12.5  5.0
  c   4.0   5.0  2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Warnings Warnings that appear or should be added to pandas
Projects
None yet
Development

No branches or pull requests

2 participants