Skip to content

BUG: DataFrame.transform(dict) drops columns not mentioned in the dict #38296

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
2 of 3 tasks
tilusnet opened this issue Dec 4, 2020 · 6 comments
Closed
2 of 3 tasks

Comments

@tilusnet
Copy link

tilusnet commented Dec 4, 2020

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

I may have a dataframe with 100 columns, of which I want to transform 3.
DataFrame.transform() accepts a dict with column -> function mappings, therefore my intuition is to provide 3 mappings for the 3 columns I want transformed. However, upon providing such a mapping dict, the function drops the non mentioned 97 columns.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# create a dataframe with 100 columns
d = {'col%d' % i: [0] for i in range(100)}
df = pandas.DataFrame(d)

# transform 3 arbitrary columns with a function
func42 = lambda x: 42
df.transform({
    'col10': func42,
    'col20': func42,
    'col42': func42
})

Problem description

The transformation lost the 97 columns for which there was no transformation mapping provided.
The natural intuition is to preserve all columns that were not mentioned in the transformation.

Expected Output

A dataframe containing all 100 columns: the 3 transformed columns with the rest of the 97 columns untouched.
The current implementation returns 3 columns.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 67a3d42
python : 3.7.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-123-generic
Version : #126~16.04.1-Ubuntu SMP Wed Oct 21 13:48:05 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.1.4
numpy : 1.19.4
pytz : 2020.4
dateutil : 2.8.1
pip : 20.3
setuptools : 49.6.0.post20201009
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 : 5.8.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@tilusnet tilusnet added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 4, 2020
@rhshadrach
Copy link
Member

Returning only 3 columns is the desired behavior, you can perform your operation as:

df[['col10', 'col20', 'col42']] = df.transform({
    'col10': func42,
    'col20': func42,
    'col42': func42
})

@rhshadrach rhshadrach added Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 5, 2020
@tilusnet
Copy link
Author

tilusnet commented Dec 5, 2020

Thanks, but I consider this a rather unfavourable workaround.
It doesn't even work with method chaining. Consider this common use case scenario:

df = pandas.read_csv(...)\
     ...
     .transform(...)\
     .apply(...)\
     .drop(...)\
     .fillna(...)\
     ...

Out of curiosity why did you say this is not a Bug?
It clearly doesn't work in line with the documentation: "Produced DataFrame will have same axis length as self." Contradiction: self had 100 length (columns), produced dataframe has only 3. => Bug.

@jreback
Copy link
Contributor

jreback commented Dec 5, 2020

@tilusnet axis is rows

this is defined behavior and not a bug

you can use .assign to achieve fluent behavior easily

@jreback jreback added this to the No action milestone Dec 5, 2020
@jreback jreback closed this as completed Dec 5, 2020
@tilusnet
Copy link
Author

tilusnet commented Dec 5, 2020

Thanks @jreback , I wasn't aware of .assign's side effect achieving data transformation.

I am sorry to insist on how misleading/incomplete the .transform documentation is.
First line:
"Call func on self producing a DataFrame with transformed values." It says "values" only, not even mentioning structure transformation. When I read about transforming values I expect no structural change.

Is there a better official documentation about the Transform feature apart from this article here?
I witness significant confusion about it online.

@jreback
Copy link
Contributor

jreback commented Dec 5, 2020

you are welcome to submit a doc update if you would like though there is extensive documentation

@tilusnet
Copy link
Author

tilusnet commented Dec 5, 2020

I'll consider helping with the docs once I truly understand it myself first.

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

No branches or pull requests

3 participants