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

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

Closed
gnebehay opened this Issue Jun 1, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@gnebehay

gnebehay commented Jun 1, 2017

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

@jreback

This comment has been minimized.

Contributor

jreback commented Jun 1, 2017

this does looks like a bug. I don't think we have this as a test. This is a fairly narrow case though, the floats have to be exactly equal to the integers. welcome to have a PR. I can point you to where the issue is.

@jreback jreback added this to the Next Major Release milestone Jun 1, 2017

@gnebehay

This comment has been minimized.

gnebehay commented Jun 2, 2017

I encountered this issue while getting data via pd.read_sql(). There, primary keys are returned as ints (since there are no missing values), but nullable foreign keys are upcast to float (so that nulls can be represented as nan). If you then do a merge on these columns, you get what is described above. If you point me to a code location, I will see what I can do.

@jreback

This comment has been minimized.

Contributor

jreback commented Jun 2, 2017

Its right about here: https://github.com/pandas-dev/pandas/blob/master/pandas/core/reshape/merge.py#L911

We allow differing int types, e.g. int64 and int8 are ok. but int64 and float will fall thru. you can add a check like

In [8]: lk = np.array([1.0, 2, 3])

In [9]: rk = np.array([2, 3, 4])

In [11]: if is_float_dtype(lk) and is_integer_dtype(rk):
    ...:     if (lk == lk.astype('int64')).all():
    ...:         continue
             # check opposite way as well
    

@jreback jreback modified the milestones: Next Major Release, 0.22.0 Nov 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment