DOC: behavior of DataFrame.assign for dictionary inputs

This issue has been created since 2022-11-21.

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/docs/dev/reference/api/pandas.DataFrame.assign.html

Documentation problem

pandas.DataFrame.assign seems to take dict (dictionary) objects and assign new columns consisting of the dictionary values corresponding to the DataFrame's index.
However, I cannot find the documentation of the behavior.

  • Example 1
import pandas as pd
df1 = pd.DataFrame({"c1": [0,1,3,5]})
dict1 = {0: 100, 1: 102, 2: 104, 3: 106}
df1.assign(c2 = dict1)
c1 c2
0 0 100
1 1 102
2 3 104
3 5 106
  • Example 2
df2 = pd.DataFrame({"c3": ["a", "b", "a", "x"], "c4": ["b", "a", "c", "d"]}).set_index("c3")
dict2 = {"a": "AAA", "b": "BBB", "c": "CCC", "y": "YYY"}
df2.assign(c5 = dict2)
c4 c5
c3
a b AAA
b a BBB
a c AAA
x d NaN

Suggested fix for documentation

I would like to use stable and documented features.
Therefore, If this result is expected, I would like the developers to include the behavior in the documentation.
If not, I would like to get a warning (or error) from the function.

rhshadrach wrote this answer on 2022-11-22

Thanks for the report! My expectation would be that

df = df.assign(x=obj)

is equivalent to

df["x"] = obj

with the exception for when obj is callable (where there is some special behavior). This is already the implementation of assign. I'm +1 on accepting any values and updating the documentation in this manner.

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-12-07
Star Count 36164
Watcher Count 1118
Fork Count 15472
Issue Count 3683

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Add cifs-utils package to bundles 2 2022-08-10 2022-11-26
Add `okd` to bundle name in case of OKD bundles 1 2022-07-28 2022-11-26
Add preset info to bundle metadata 11 2022-07-28 2022-11-26
[Feature request] Default expiration time changable 2 2022-09-28 2022-11-30
Datei "worker.php" um POST-Paramter ergänzen 5 2021-05-15 2022-05-14
[prediction-markets] Should the oracle count as correct reporter? 2 2022-05-05 2022-09-14
Configure runtime constants 2 2022-05-04 2022-10-15
Checklist for 0.3.2 release 0 2022-05-05 2022-10-15
Comment Button is still Shown and fails without error when Comments are Disabled in the site settings. 3 2021-06-23 2022-11-13
The "HAAC_Input_HasRequired" rule missing help file ? #3799 1 2022-06-03 2022-10-14
[Hardening] Rootless base-image 0 2021-06-07 2022-09-29
Chapter 9 Cycle GAN Colab Implementation Error 1 2022-04-05 2022-11-12
Provide support for a default site if no header is provided 0 2021-09-01 2022-11-05
retryAttempts: { "MPD": 99 } on mpd 404 not working latest dash 4.3.0 2 2022-04-06 2022-10-03
FileDialogManager.OpenFileDialog can't select more than one file 0 2021-10-09 2022-09-21
Cannot find module '@redux-saga/is' 1 2020-05-27 2022-11-05
seebug里下载poc时有pocsuite和pocsuite3两种脚本类型标识,请问这两种类型的脚本都能在pocsuite3框架下使用吗? 1 2021-06-28 2022-09-23
Upgrade to newest CLJS, include cljs.math as module 0 2022-05-13 2022-11-21
ERGO LHR unlock for LHR v1 cards 7 2021-09-28 2022-11-13
Test: Add end 2 end tests for redis storage 0 2021-08-26 2022-10-30
Tutanota Mail Bad Secret 19 2021-06-15 2022-10-07
UnitsNet installs System.ValueTuple into net47 or higher targets 0 2021-08-09 2022-08-14
v2.1.0 -> 3.1.2 Migration 1 2022-07-20 2022-12-03
Something like Astro.js 2 2022-11-25 2022-12-02
fixed image size with object-fit: contain 1 2019-10-04 2022-11-06
Error when using precompile function 0 2022-05-23 2022-07-13
Storage optimisation - 'file list' file type 1 2022-02-16 2022-09-29
Modify the margin of the text of the articles 0 2022-03-13 2022-11-17
Unable to subscribe to RSS feeds 0 2022-03-01 2022-11-17
AI abuses whirlpool usage too much leading to a huge loss of army 1 2022-09-25 2022-12-04
segfaulting with 1.27.4+ (including 2.0) and ruby 2.5.3 2 2019-03-06 2022-11-03
[Feature Request] Continue playing audio from the last time 1 2022-06-12 2022-11-30
Images do not have publicURL option 6 2020-11-30 2022-12-02
[Feature Request] Add return_format to oEmbed field 0 2021-08-27 2022-10-15
[Enhancement]: Remove queryCoord-deltaChannel meta from querycoord 1 2022-07-01 2022-08-23
Invalid Catalog Entries get stuck in refresh loop 5 2022-06-29 2022-08-30
Add support for interactive animations 1 2020-07-20 2022-11-24
Widgets don't show the first time in Jupyter Lab with akernel 2 2021-07-03 2022-11-27
F# Pipe Operator as Syntactic Sugar 5 2021-10-07 2022-11-28
"Welcome to the new hue app" 1 2022-07-21 2022-12-02
Psping no longer needed 0 2022-07-24 2022-09-06
special characters in column names 1 2021-08-02 2022-10-07
New sharing button on mobile is hidden behind QR code 1 2022-06-16 2022-09-13
[Bug] Unity forces ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES for UnityFramework since Firebase 9.0.0 1 2022-06-25 2022-11-27
[Question] Slideshow with max-height for images 1 2021-02-09 2022-12-04
No module named 'keras_yolo3' 3 2020-02-24 2022-11-26
esp32: gnrc_networking crashing 1 2022-05-10 2022-10-12
Add gradle wrapper validation action to the build pipeline 0 2022-12-02 2022-12-06
Disable TextEditor without polyhedron and improve updating temporary … 0 2019-03-31 2022-11-07
when shortcut contains ',' unbind will error 0 2021-10-26 2022-11-27