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

BUG: One item lists are assigned into dataframes as single items #16864

Open
elielhojman opened this issue Jul 9, 2017 · 1 comment
Open
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@elielhojman
Copy link

Code Sample, a copy-pastable example if possible

df = pd.DataFrame(data={'A':[1, 2, 3, 4], 'B':[5, 6, 7, 8], 'C':[8, 9, 10, 13]})
df['new_col'] = np.nan
df.loc[[0,1], 'new_col'] = [['one_item'], ['one_item']]
df.loc[[2,3], 'new_col'] = [['one_item'], ['two', 'items']]
print(df)
#     A  B   C         new_col
# 0  1  5   8           one_item
# 1  2  6   9           one_item
# 2  3  7  10          [one_item]
# 3  4  8  13          [two, items]

Problem description

I would like to create a new column and load the values into it gradually, however in the first assignment to the 'new_col' as the list has only one item they are assigned as items and not as lists.

Expected Output

#    A  B   C         new_col
# 0  1  5   8          [one_item]
# 1  2  6   9          [one_item]
# 2  3  7  10          [one_item]
# 3  4  8  13          [two, items]

Output of pd.show_versions()

INSTALLED_VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.20.2
pytest: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.13.0
scipy: 0.19.1
xarray: None
IPython: 5.4.1
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Jul 10, 2017

this is as expected. Putting lists and array-like inside a DataFrame cell is non-idiomatic / non-performant and has little support.

I'll mark it as a bug. But would take an enterprising user to try to fix this w/o breaking anything else.

@jreback jreback changed the title One item lists are assigned into dataframes as single items BUG: One item lists are assigned into dataframes as single items Jul 10, 2017
@jreback jreback added Bug Difficulty Intermediate Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 10, 2017
@jreback jreback added this to the Next Major Release milestone Jul 10, 2017
@jbrockmendel jbrockmendel added the Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). label Sep 22, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
PsypherPunk added a commit to DFE-Digital/education-benchmarking-and-insights that referenced this issue Oct 1, 2024
PsypherPunk added a commit to DFE-Digital/education-benchmarking-and-insights that referenced this issue Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
Development

No branches or pull requests

4 participants