Code Sample, a copy-pastable example if possible
df = pd.DataFrame(data={'a':[0,0]})
# df['b'] = None
# df['b'] = df['b'].astype('object')
df.at[0, 'b'] = ['a']
df.at[1, 'b'] = ['a']
>>> df
a b
0 0 a
1 0 [a]
Problem description
df.at[0, 'b'] should be ['a'] instead of 'a'. I understand that it does not work when while adding the column but it feels inconsistent anyways. When adding line 2 and 3 it works as expected.
Expected Output
>>> df
a b
0 0 [a]
1 0 [a]
Output of pd.show_versions()
Comment From: ellequelle
DataFrame.at isn't intended for creating new columns. From the docs:
DataFrame.at
...
Raises
------
KeyError
If 'label' does not exist in DataFrame.
It should work if you uncomment your second line:
df = pd.DataFrame(data={'a':[0,0]})
df['b'] = None
df.at[0, 'b'] = ['a']
df.at[1, 'b'] = ['a']
>>> df
a b
0 0 [a]
1 0 [a]
Comment From: TomAugspurger
That line of docs was added in https://github.com/pandas-dev/pandas/pull/20290, and doesn't appear to be tested. It's not clear that that's the intentional behavior.
Comment From: phofl
This seems to work now, may need tests
Comment From: FactorizeD
take
Comment From: topper-123
This isn't actually fixed in v2.0.0rc1 AFAIKS, i.e. I get the same result as OP.
In general, the support in Pandas for non-scalar values is not all that good, so maybe not so surprising this is happening.