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

.transform inconsistent / error-prone behavior for list #28246

Open
mglowacki100 opened this issue Sep 1, 2019 · 0 comments
Open

.transform inconsistent / error-prone behavior for list #28246

mglowacki100 opened this issue Sep 1, 2019 · 0 comments
Labels
Apply Apply, Aggregate, Transform Bug Groupby Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@mglowacki100
Copy link

mglowacki100 commented Sep 1, 2019

Code Sample, a copy-pastable example if possible

import pandas as pd


df = pd.DataFrame(data={'label': ['a', 'b', 'b', 'c'], 'wave': [1, 2, 3, 4], 'y': [0,0,0,0]})
###problem 1
df['n_tuple'] = df.groupby(['label'])[['wave']].transform(tuple)
df['n_set'] = df.groupby(['label'])[['wave']].transform(set)
df['n_frozenset'] = df.groupby(['label'])[['wave']].transform(frozenset)
df['n_dict'] = df.groupby(['label'])[['wave']].transform(dict)

df['n_list_problem'] = df.groupby(['label'])[['wave']].transform(list)
df['n_list_expected'] = df.groupby(['label'])[['wave']].transform(tuple)['wave'].apply(list)

###problem 2
df['n_sum'] = df.groupby(['label'])['wave'].transform(sum)
df['n_tuple_problem'] = df.groupby(['label'])['wave'].transform(tuple)
df['n_tuple'] = df.groupby(['label'])[['wave']].transform(tuple)

Problem description

I was hinted that it could be a bug: https://stackoverflow.com/questions/57743798/pandas-transform-inconsistent-behavior-for-list
There are two things, but let focus on 1 first:
Result of
df['n_list_problem'] = df.groupby(['label'])[['wave']].transform(list) is not consistent with similar operations like df['n_tuple'], df['n_set'] etc.
On stackoverflow I was pointed to issues regarding series lenght for rationale, but consensus was that, this behavior is confusing and error-prone.

The 2 problem:
df['n_sum'] = df.groupby(['label'])['wave'].transform(sum) works as expected, but
df['n_tuple_problem'] = df.groupby(['label'])['wave'].transform(tuple) gives unexpected result.
To get proper one, you need to coerce series into dataframe by [[]] instead of [] for wave.
df['n_tuple'] = df.groupby(['label'])[['wave']].transform(tuple)

Expected Output

df['n_list_expected'] = df.groupby(['label'])[['wave']].transform(tuple)['wave'].apply(list)

  label  wave  y n_tuple  n_list_problem n_list_expected  n_tuple_problem
0     a     1  0    (1,)               1             [1]                1
1     b     2  0  (2, 3)               2          [2, 3]                2
2     b     3  0  (2, 3)               3          [2, 3]                3
3     c     4  0    (4,)               4             [4]                4

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-141-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.1
numpy : 1.17.0
pytz : 2019.2
dateutil : 2.8.0
pip : 19.1.1
setuptools : 41.0.1
Cython : 0.29.13
pytest : None
hypothesis : None
sphinx : 2.1.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.7.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
s3fs : None
scipy : 1.3.1
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@mglowacki100 mglowacki100 changed the title pandas .transform incosistent behavior for list .transform inconsistent / error-prone behavior for list Sep 1, 2019
@jbrockmendel jbrockmendel added Apply Apply, Aggregate, Transform Groupby labels Oct 17, 2019
@mroeschke mroeschke added the Bug label Jun 28, 2020
@jbrockmendel jbrockmendel added List-Like Scalars Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). and removed List-Like Scalars labels Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Bug Groupby Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

No branches or pull requests

3 participants