question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: DataFrame iloc does not accept Series assignment

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

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


Code Sample, a copy-pastable example

df = pd.DataFrame(columns=[0], index=[0])

df.iloc[0,0] = pd.Series([1,2,3])    # this raises a ValueError
df.iloc[0] = [pd.Series([1,2,3])]    # this works, but is ugly

Problem description

When trying to assign a pandas.Series to one cell of a pandas.DataFrame, the following error is raised: ValueError: Incompatible indexer with Series Series in DataFrames are a common thing e.g. in sktime, when working with multivariate time series data and (as the working example above shows) it is unproblematically supported by pandas. Just the assignment to one cell via .iloc seems to be erroneous.

INSTALLED VERSIONS

commit : db08276bc116c438d3fdee492026f8223584c477 python : 3.8.5.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.18362 machine : AMD64 processor : Intel64 Family 6 Model 61 Stepping 4, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : de_DE.cp1252

pandas : 1.1.3 numpy : 1.19.2 pytz : 2020.1 dateutil : 2.8.1 pip : 20.2.4 setuptools : 50.3.0.post20201006 Cython : None pytest : None hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : None html5lib : None pymysql : None psycopg2 : None jinja2 : 2.11.2 IPython : 7.18.1 pandas_datareader: None bs4 : None bottleneck : None fsspec : None fastparquet : None gcsfs : None matplotlib : 3.3.1 numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pytables : None pyxlsb : None s3fs : None scipy : 1.5.0 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None xlwt : None numba : None

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
cedricdoniecommented, May 29, 2022

Workaround for older pandas versions: use df.at instead of df.loc. I tested this with pandas 1.1.5 and don’t get the ValueErroranymore.

Perhaps df.iat can also be used a workaround for df.iloc, but I have not tested this.

Unfortunately, sktime-dl restricts me to using this old pandas version since I need Python 3.6.

0reactions
jmpalumbocommented, Sep 8, 2022

df.at worked like a charm. This is the only board online where I saw df.at as a solution to this ValueError. Thank you @cedricdonie

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python .iloc error trying to input values into dataframe
pandas - Python . iloc error trying to input values into dataframe - Stack Overflow. Stack Overflow for Teams – Start collaborating and...
Read more >
How to use loc and iloc for selecting data in Pandas | by B. Chen
We get the error because iloc cannot accept a boolean Series. It only accepts a boolean list. We can use the list() function...
Read more >
pandas.Series.loc — pandas 1.5.2 documentation
Access a group of rows and columns by label(s) or a boolean array. .loc[] is primarily label based, but may also be used...
Read more >
Pandas iloc and loc – quickly select data in DataFrames
Note that .iloc returns a Pandas Series when one row is selected, and a ... the DataFrame is not integer based. ix will...
Read more >
SettingWithCopyWarning in Pandas: Views vs Copies
mask is an instance of a Pandas Series with Boolean data and the indices from ... The assignment fails because df.loc[mask] returns a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found