Skip to content

Data type of new assigned DataFrame column #23326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gitwhistle opened this issue Oct 25, 2018 · 3 comments · Fixed by #47264
Closed

Data type of new assigned DataFrame column #23326

gitwhistle opened this issue Oct 25, 2018 · 3 comments · Fixed by #47264
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@gitwhistle
Copy link

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

@WillAyd WillAyd added the Dtype Conversions Unexpected or buggy dtype conversions label Oct 25, 2018
@WillAyd
Copy link
Member

WillAyd commented Oct 25, 2018

Thanks for the report - investigation and PRs are certainly welcome!

@WillAyd WillAyd added the Bug label Oct 25, 2018
@charlesdong1991
Copy link
Member

i would like to give a try...

@mroeschke
Copy link
Member

This looks correct on master. Could use a test

In [22]: 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     int64
col2     int64
col3     int64
dtype: object
   0  1  2  col1  col2  col3
0  a  b  c     5     5     5

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Dtype Conversions Unexpected or buggy dtype conversions labels Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
4 participants