Skip to content

Data type of new assigned DataFrame column #23326

Closed
@gitwhistle

Description

@gitwhistle
df = pd.DataFrame([list('abc')])
print(df)

df.loc[:, 'col1'] = int(5)
df['col2']        = int(5)
df                = df.assign(col3=int(5))

print(df.dtypes)
print(df)


  0  1  2
0  a  b  c
0        object
1        object
2        object
col1    float64
col2      int64
col3      int64
dtype: object
   0  1  2  col1  col2  col3
0  a  b  c   5.0     5     5

Problem description

The fact that df.loc[:,'col1] = int(5) yields a new column with dtype float64 seems strange.

Expected Output

All three methods should yield a new column with dtype int64

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 2.7.15.final.0 python-bits: 64 OS: Linux OS-release: 4.15.0-1023-aws machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: C.UTF-8 LOCALE: None.None

pandas: 0.23.4
pytest: 3.8.2
pip: 18.1
setuptools: 40.4.3
Cython: None
numpy: 1.15.0
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.8.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions