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:
- Created 3 years ago
- Comments:12 (8 by maintainers)
Workaround for older pandas versions: use
df.at
instead ofdf.loc
. I tested this with pandas 1.1.5 and don’t get theValueError
anymore.Perhaps
df.iat
can also be used a workaround fordf.iloc
, but I have not tested this.Unfortunately, sktime-dl restricts me to using this old pandas version since I need Python 3.6.
df.at
worked like a charm. This is the only board online where I sawdf.at
as a solution to thisValueError
. Thank you @cedricdonie