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

API: can not use array as table item #18955

Open
semigodking opened this issue Dec 27, 2017 · 2 comments
Open

API: can not use array as table item #18955

semigodking opened this issue Dec 27, 2017 · 2 comments
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

@semigodking
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd
df = pd.DataFrame()
df['s1'] = (0, 1, 2)
df['s2'] = None
df.loc[0, 's2'] = [1,]
df.loc[0, 's2'] = [1,2]

Problem description

Inconsistent behavior.
For above code, the last line fails while the previous line successes.

Expected Output

The last line successes or the last two lines fail.

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-101-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_HK.UTF-8
LOCALE: en_HK.UTF-8

pandas: 0.21.0
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.4.0
Cython: None
numpy: 1.13.3
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: 2.4.8
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: 1.1.15
pymysql: None
psycopg2: 2.7.3.1 (dt dec pq3 ext lo64)
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Dec 28, 2017

xref #15695 , #7787

I am pretty sure this is a duplicate, but these things are sometimes hidden and/or not labeled fully.

We raise if an iterable is passed that does not match the shape of the assigned, here you are setting a single cell, so we are expecting a scalar. I think relaxing this would be ok. Welcome to investigate / submit a patch.

@jreback jreback changed the title Bug: can not use array as table item API: can not use array as table item Dec 28, 2017
@jreback jreback added Difficulty Intermediate Indexing Related to indexing on series/frames, not to indexes themselves labels Dec 28, 2017
@jreback jreback added this to the Next Major Release milestone Dec 28, 2017
@jreback jreback added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Dec 28, 2017
@kittoku
Copy link
Contributor

kittoku commented Jun 27, 2018

similar problem and another inconsistency between int and str.

import pandas as pd

int_list = [0]
str_list = ['a']


df = pd.DataFrame([[1, 2], [3, 4]])
df.loc[:, 0] = int_list  # raise ValueError
df.loc[0, :] = int_list  # broadcasted


df = pd.DataFrame([[1, 2], [3, 4]])
df.loc[:, 0] = str_list  # broadcasted
df.loc[0, :] = str_list  # raise ValueError

It could be a option to set a function or block like is_single_cell which convert list-like to scalar in the upper of _setitem_with_indexer, couldn't it? (ignore me if @semigodking already started to make a PR)

But it seems to be slightly lower performance than catching single-cell object in deep places of conditional tree.

@jbrockmendel jbrockmendel added the Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). label Sep 22, 2020
@mroeschke mroeschke added Bug and removed Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jun 12, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
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

5 participants