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: Unable to set StringArray for DataFrame with single column and dtype string #44103

Open
2 of 3 tasks
ace-e4s opened this issue Oct 19, 2021 · 3 comments
Open
2 of 3 tasks
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@ace-e4s
Copy link

ace-e4s commented Oct 19, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd


df_single = pd.DataFrame(data={"a": [None, "two"]}, index=[0, 1], dtype="string")
df_multi = pd.DataFrame(data={"a": [None, "two"], "b":["1", "2"]}, index=[0, 1], dtype="string")


# ok
df_single.loc[0, ["a"]] = "one"

# ok
df_multi.loc[0, ["a"]] = "one"

# not ok
df_single.loc[0, ["a"]] = ["one"]

# ok
df_multi.loc[0, ["a"]] = ["one"]

# not ok
df_single.loc[0, ["a"]] = pd.array(["one"], dtype="string")

# ok
df_multi.loc[0, ["a"]] = pd.array(["one"], dtype="string")

Issue Description

When attempting to set a single value (string array) into an DataFrame with single column and dtype string, exception is raised: ValueError: setting an array element with a sequence.

The code example shows same operation performed different ways with a DataFrame with single and multiple columns. No issue when DataFrame has more than one column.

Expected Behavior

Set the value as in DataFrame with multiple columns.

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.8.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : Norwegian Bokmål_Norway.1252

pandas : 1.3.4
numpy : 1.20.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.1.3
setuptools : 58.2.0
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.54.0

@ace-e4s ace-e4s added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 19, 2021
@jbrockmendel
Copy link
Member

Looks like this is driven by ExtensionBlock's special-casing of setitem, which ignores indexer[1].

Goes on the list of "will be fixed by 2D EAs"

@jbrockmendel jbrockmendel added Indexing Related to indexing on series/frames, not to indexes themselves ExtensionArray Extending pandas with custom dtypes or arrays. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 19, 2021
@ace-e4s
Copy link
Author

ace-e4s commented Oct 19, 2021

Is there a PR or issue I can track? Encountered this in production setting, and will be applying a hotfix while waiting on fix.

@jbrockmendel
Copy link
Member

Is there a PR or issue I can track?

This issue right here!

Encountered this in production setting, and will be applying a hotfix while waiting on fix.

Best guess (emphasis on guess) is that a kludge could be patched into ExtensionBlock.setitem inside the if isinstance(indexer, tuple) path to unpack exactly this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants