Description
df = pd.DataFrame({'A': ['a', 'b'], 'B': ['1', '2'], 'C': ['3', '4']})
df.loc[:, ['B', 'C']] = df.loc[:, ['B', 'C']].astype('int')
df.dtypes
A object
B object
C object
dtype: object
When I try to update multiple object columns with loc/iloc, the values in the columns change but object dtype is preserved. This is not the case for numeric dtypes.
df = pd.DataFrame({'A': ['a', 'b'], 'B': [1, 2], 'C': [3, 4]})
df.loc[:, ['B', 'C']] = df.loc[:, ['B', 'C']].astype('float')
df.dtypes
A object
B float64
C float64
dtype: object
Shouldn't the columns in the first example have integer dtypes? I found this issue but it seems it is specific to extension arrays. Also, if I try it with a single column like the one in the linked issue, the dtype changes:
df = pd.DataFrame({'A': ['a', 'b'], 'B': ['1', '2'], 'C': ['3', '4']})
df.loc[:, 'B'] = df.loc[:, 'B'].astype('int')
df.dtypes
A object
B int64
C object
dtype: object
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Linux
OS-release: 4.10.0-42-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.22.0
pytest: 3.4.1
pip: 9.0.2
setuptools: 38.5.1
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.7.1
patsy: 0.5.0
dateutil: 2.7.0
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.5.0
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.4
pymysql: None
psycopg2: 2.7.4 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None