Skip to content

BUG: groupby-transform produces NaN for series keys with as_index=False #37093

Closed
@thomas-reineking-by

Description

@thomas-reineking-by
  • 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.


Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame({"X": [1.0]})
df.groupby(pd.Series(["group"]), as_index=False).transform("sum")

Problem description

With version 1.1.3 this produces a dataframe containing NaN.

Expected Output

Expected is 1.0 and this is also what older versions produce (1.0.5).

It seems the following conditions have to be satisfied to trigger this bug:

  • grouping by a series which does not contain indices corresponding to the original dataframe (grouping by pd.Series([0]) works though it seems accidental)
  • using as_index=False
  • using transform

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.6.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.76-linuxkit
Version : #1 SMP Tue May 26 11:42:35 UTC 2020
machine : x86_64
processor :
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.2.0
Cython : 0.29.15
pytest : 5.4.3
hypothesis : 5.6.0
sphinx : 1.8.5
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : 2.8.5 (dt dec pq3 ext lo64)
jinja2 : 2.10.3
IPython : 7.9.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fsspec : 0.6.2
fastparquet : None
gcsfs : None
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 0.17.1.post2
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.2.1
sqlalchemy : 1.3.15
tables : None
tabulate : 0.8.6
xarray : None
xlrd : 1.2.0
xlwt : None
numba : 0.48.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions