Skip to content

error merging dataframes with unicode and numpy.float64 column names #13353

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
corakingdon opened this issue Jun 3, 2016 · 2 comments · Fixed by #41431
Closed

error merging dataframes with unicode and numpy.float64 column names #13353

corakingdon opened this issue Jun 3, 2016 · 2 comments · Fixed by #41431
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@corakingdon
Copy link

corakingdon commented Jun 3, 2016

There is a strange error happening during pandas.merge when there is a unicode column name followed by a numpy.float64 column name. The error only happens for certain numpy.float64 values. The error is: "UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 5: invalid start byte"

The following code reproduces the error:

import pandas as pd
import numpy as np

def my_test(X):

    t=pd.DataFrame([[1, 2], [3, 4]])
    u=pd.DataFrame([[9, 10], [11, 12]])

    t.rename(columns={0:unicode('a'),1:np.float64(X)}, inplace=True)
    u.rename(columns={0:unicode('x'),1:unicode('y')}, inplace=True)

    pd.merge(u, t, how="inner", left_index=True, right_index=True)

#works fine for 113, but throws an error for 114
my_test(113)
my_test(114)

#print out the numbers up to 200 for which this error occurs:
problem_numbers=[]
for i in range(200):
    try:
        my_test(i)
    except UnicodeDecodeError:
        problem_numbers.append(i)

print(problem_numbers)
@jreback
Copy link
Contributor

jreback commented Jun 3, 2016

So this is breaking; though I can't get it to repro except with the test above. breaking here

In [18]: Index(np.array([u'a', 114.0], dtype=object)).is_monotonic
Out[18]: False

Its trying to hash the values and decoding is failing on the float. So I think simplest to catch all exceptions here (rather than just TypeError).

want to do a pull-request?

@jreback jreback added this to the 0.18.2 milestone Jun 3, 2016
@jorisvandenbossche jorisvandenbossche modified the milestones: Next Major Release, 0.19.0 Aug 18, 2016
@mroeschke
Copy link
Member

This looks like a Python 2 issue, as Python 3 strings are unicode. This works on master, so I suppose this could use a test

In [19]:
    ...:     t=pd.DataFrame([[1, 2], [3, 4]])
    ...:     u=pd.DataFrame([[9, 10], [11, 12]])
    ...:
    ...:     t.rename(columns={0:'a',1:np.float64(114)}, inplace=True)
    ...:     u.rename(columns={0:'x',1:'y'}, inplace=True)
    ...:
    ...:     pd.merge(u, t, how="inner", left_index=True, right_index=True)
Out[19]:
    x   y  a  114.0
0   9  10  1      2
1  11  12  3      4

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Unicode Unicode strings labels May 1, 2021
@jreback jreback modified the milestones: Contributions Welcome, 1.3 May 12, 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
Development

Successfully merging a pull request may close this issue.

5 participants