Skip to content
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

BUG: inconsistent behaviour of Groupby (probably a regression) #29051

Closed
TheoSimier opened this issue Oct 17, 2019 · 1 comment
Closed

BUG: inconsistent behaviour of Groupby (probably a regression) #29051

TheoSimier opened this issue Oct 17, 2019 · 1 comment

Comments

@TheoSimier
Copy link

First, thank you very much for your work ! I love pandas !

The problem

Code Sample

import pandas as pd
# intialise data of lists. 
data = {'Fruit':['Apple', 'Orange', 'Apple'],
        'Origin':['France', 'France', 'Spain'],
        'Price':[10, 15, 20]}
  
# Create DataFrame without categorical variable
df = pd.DataFrame(data)
# Same dataframe with categorical variable
df_category = df.copy()
df_category['Origin'] = df_category['Origin'].astype('category')
df

# Normal behavior
df.groupby(['Fruit','Origin'])['Price'].mean()

# Abnormal behavior
df_category.groupby(['Fruit','Origin'])['Price'].mean()

Return

# Normal behavior
Fruit   Origin
Apple   France    10
        Spain     20
Orange  France    15
Name: Price, dtype: int64

# Abnormal behavior
Fruit   Origin
Apple   France    10.0
        Spain     20.0
Orange  France    15.0
        Spain      NaN
Name: Price, dtype: float64

Problem description

Bug that should have been solved with #20583. I supposed it is a regression. No new discussion since May 2018, so I prefer to reopen a issue.

Brief description: When a column of a groupby is a categorical variable, the output include rows with NA.

Expected Output

Fruit   Origin
Apple   France    10
        Spain     20
Orange  France    15
Name: Price, dtype: int64

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.0.final.0 python-bits : 64 OS : Darwin OS-release : 18.7.0 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : fr_FR.UTF-8 LOCALE : fr_FR.UTF-8

pandas : 0.25.1
numpy : 1.16.5
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.2
setuptools : 41.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : None
pymysql : None
psycopg2 : 2.7.5 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : 4.8.0
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : 2.6.2
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.1
sqlalchemy : None
tables : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None

@TheoSimier
Copy link
Author

After taking more time to read the documentation the answer is in the documentation of the groupby function: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html. Seems strange to me that the defaut value of observed is False but okay.

observed : bool, default False

    This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers.

    New in version 0.23.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant