Closed
Description
Hi,
I'm not sure this is a bug but the behaviour is far from intuitive. I'm trying to assign the values of a slice of a DataFrame
into another slice (of same size) of the same DataFrame
.
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: a = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [6, 7, 8, 9, 10]})
In [4]: a
Out[4]:
a b
0 1 6
1 2 7
2 3 8
3 4 9
4 5 10
In [5]: a.iloc[0:2,:] = a.iloc[3:5, :]
In [6]: a
Out[6]:
a b
0 NaN NaN
1 NaN NaN
2 3.0 8.0
3 4.0 9.0
4 5.0 10.0
Specifically, in the current implementation the data type is not preserved (int
to float
) and the value copied are np.NaN
instead of the correct values.
The expected output can be obtained using this counter-intuitive command:
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: a = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [6, 7, 8, 9, 10]})
In [4]: a.iloc[0:2,:] = (np.array(a.iloc[3:5, :])).astype(int)
In [5]: a
Out[5]:
a b
0 4 9
1 5 10
2 3 8
3 4 9
4 5 10
This is the environment I'm working on:
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-31-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 24.0.2
Cython: None
numpy: 1.11.1
scipy: 0.17.1
statsmodels: None
xarray: None
IPython: 5.0.0
sphinx: 1.4.5
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.1.0b2
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None