Skip to content

merge of int and float column results in column of dtype object #16572

Closed
@gnebehay

Description

@gnebehay

Code Sample, a copy-pastable example if possible

import pandas as pd

A = pd.DataFrame({'X': [1, 2, 3]})
B = pd.DataFrame({'Y': [1.0, 3.0]})

B = B.merge(A, left_on='Y', right_on='X')

print(B.dtypes)

Problem description

The output is

Y    object
X     int64
dtype: object

meaning that Y was incorrectly upcast to object.

In pandas 0.19, the dtype of B.Y after merging is float64.
In pandas 0.20, the dtype of B.Y after merging is object.

The problem only occurs if some keys in B.Y are missing (try adding 2.0)

Expected Output

Y    float64
X      int64
dtype: object

Output of pd.show_versions()


commit: None python: 3.5.3.final.0 python-bits: 64 OS: Linux OS-release: 4.9.0-2-amd64 machine: x86_64 processor: byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.20.1
pytest: None
pip: 9.0.1
setuptools: 35.0.2
Cython: None
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 6.0.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDtype ConversionsUnexpected or buggy dtype conversionsReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions