Replacing multiple columns (or just one) with iloc does not work
See original GitHub issueCode Sample, a copy-pastable example if possible
import pandas
columns = pandas.DataFrame({'a2': [11, 12, 13], 'b2': [14, 15, 16]})
inputs = pandas.DataFrame({'a1': [1, 2, 3], 'b1': [4, 5, 6], 'c1': [7, 8, 9]})
inputs.iloc[:, [1]] = columns.iloc[:, [0]]
print(inputs)
Problem description
I have a code which is replacing a set of columns with another set of columns, based on column indices. To make things done without a special case, I assumes I could just use iloc
to both select and set columns in a DataFrame. But it seems that this not work and fails in strange ways.
Expected Output
a1 b1 c1
0 1 11 7
1 2 12 8
2 3 13 9
But in reality, you get:
a1 b1 c1
0 1.0 NaN 7.0
1 2.0 NaN 8.0
2 3.0 NaN 9.0
See how values converted to float and how column is NaN
s?
But, if I do the following I get expected results:
inputs.iloc[:, [1]] = [[11], [12], [13]]
This also works:
inputs.iloc[:, [1]] = columns.iloc[:, [0]].values
So if it works with lists and ndarrays, one would assume it would also work with DataFrames themselves. But it does not.
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None python: 3.6.3.final.0 python-bits: 64 OS: Linux OS-release: 4.13.0-46-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8
pandas: 0.23.3 pytest: None pip: 18.0 setuptools: 40.0.0 Cython: None numpy: 1.15.0 scipy: None pyarrow: None xarray: None IPython: None sphinx: None patsy: None dateutil: 2.7.3 pytz: 2018.5 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: None openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: None sqlalchemy: None pymysql: None psycopg2: None jinja2: None s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
Yes, in principle
iloc
should not care about column names, so this certainly seems a bug to me.Thanks.