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.

BUG: Same function calls on the same DataFrameGroupBy object give different results

See original GitHub issue
  • 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.

Source codes

import pandas as pd                                    
inp = pd.DataFrame([[1, 2, 3, 4],
                    [5, 6, 7, 8],
                    [1, 10, 11, 12],
                    [5, 14, 15, 16],
                    [1, 18, 19, 20],
                    [21, 22, 23, 24],
                    [5, 26, 27, 28]],
                columns=['group', 'fa', 'fb', 'fc'])
print('pandas version:', pd.__version__)
grps = inp.groupby('group', as_index=True)
print('Column number in all groups:',
    grps.apply(lambda x: x.shape[1]).unique())
print('Column number in all groups:',
    grps.apply(lambda x: x.shape[1]).unique())
print('ID:', id(grps))
print('Shape of first dataframe in the group:', grps.first().shape)
print('ID:', id(grps))
print('Column number in all groups:',
    grps.apply(lambda x: x.shape[1]).unique())

Problem description

In above codes, same function calls grps.apply(lambda x: x.shape[1]).unique() give different results:

In the first 2 times before grps.first().shape is called, it returns 4. While after grps.first().shape is called, it returns 3.

Running output

Column number in all groups: [4]
Column number in all groups: [4]
ID: 140686222651664
Shape of first dataframe in the group: (3, 3)
ID: 140686222651664
Column number in all groups: [3]

Expected Output

Column number in all groups: [4]
Column number in all groups: [4]
ID: 140686222651664
Shape of first dataframe in the group: (3, 3)
ID: 140686222651664
Column number in all groups: [4]

Environments

Python 3.7.7, Ubuntu 18.04.

Output of pd.show_versions():

INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.7.final.0
python-bits      : 64
OS               : Linux
OS-release       : 4.15.0-96-generic
machine          : x86_64
processor        : x86_64
byteorder        : little
LC_ALL           : en_US.UTF-8
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.0.2
numpy            : 1.18.1
pytz             : 2019.3
dateutil         : 2.8.1
pip              : 20.1
setuptools       : 41.2.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : 7.13.0
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : None
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
pytest           : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
numba            : None

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
manasjoshi14commented, May 26, 2020

Confirmed on Ubuntu 18.04, Python 3.8.2, Pandas 1.0.3.

  • 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.

The issue lies in function “first()”. It works inplace and modifies the GroubBy object removing the “group” column. Same happens with the function “nth”.

I can take this if this is a bug and not by design.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas: Aggregate of DataFrameGroupby - Stack Overflow
This has the upside that you need only one function call. Use this, if you want the same operations to be calculated with...
Read more >
Group by: split-apply-combine — pandas 1.5.2 documentation
If the results from different groups have different dtypes, then a common dtype will be determined in the same way as DataFrame construction....
Read more >
Pandas' groupby explained in detail | by Fabian Bosler
Learn how to master all Pandas' groupby functionalities, like agg(regation), transform and filter — code-along guide plenty of examples.
Read more >
Group and Aggregate your Data Better using Pandas Groupby
Python tuples are used to provide the column name on which to work on, along with the function to apply. For example: data[data['item']...
Read more >
pandas GroupBy: Your Guide to Grouping Data in Python
groupby() is smart and can handle a lot of different input types. Any of these would produce the same result because all of...
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