Skip to content

dataframe merge returns incorrect result #2237

Closed

Description

@Louis-Tian

Dask's dd.merge operation returns incorrect results under certain unkown condition. Seems to be related to the size of the dataframe.

I have created a repo for this issue.
https://gitlab.com/LouisTian/dask_merge_issue

import dask.dataframe as dd
import pandas as pd
dask_df = dd.read_csv('./fastText_extract.csv', encoding='utf-8', usecols=list(range(1, 302)))
pd_df = pd.read_csv('./fastText_extract.csv', encoding='utf-8', usecols=list(range(1, 302)))
features = pd.read_csv('./features.csv', encoding='utf-8', usecols=[1])
pd_df[pd_df['word'] == 'leonid']
word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
15810 leonid -0.11284 0.27493 -0.11428 0.27465 -0.1484 -0.16222 0.54122 -0.35387 -0.44765 ... -0.35554 -0.21266 0.048219 -0.023976 0.23689 -0.11721 0.012078 -0.15143 -0.19005 0.043254

1 rows × 301 columns

features[features['word'] == 'leonid']
word
614 leonid

Different merge result using pandas and dask

result = pd_df.merge(features, on='word', how='right')
result[result['word'] == 'leonid']
word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
15810 leonid -0.11284 0.27493 -0.11428 0.27465 -0.1484 -0.16222 0.54122 -0.35387 -0.44765 ... -0.35554 -0.21266 0.048219 -0.023976 0.23689 -0.11721 0.012078 -0.15143 -0.19005 0.043254

1 rows × 301 columns

result = dask_df.merge(features, on='word', how='right').compute()
result[result['word'] == 'leonid']
word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
9748 leonid NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

1 rows × 301 columns

Use smaller subset of data, same result

dask_df = dd.read_csv('./fastText_sample.csv', encoding='utf-8', usecols=list(range(1, 302)))
pd_df = pd.read_csv('./fastText_sample.csv', encoding='utf-8', usecols=list(range(1, 302)))
features = pd.read_csv('./features.csv', encoding='utf-8', usecols=[1])
result = pd_df.merge(features, on='word', how='right')
result[result['word'] == 'leonid']
word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
810 leonid -0.11284 0.27493 -0.11428 0.27465 -0.1484 -0.16222 0.54122 -0.35387 -0.44765 ... -0.35554 -0.21266 0.048219 -0.023976 0.23689 -0.11721 0.012078 -0.15143 -0.19005 0.043254

1 rows × 301 columns

result = dask_df.merge(features, on='word', how='right').compute()
result[result['word'] == 'leonid']
word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
810 leonid -0.11284 0.27493 -0.11428 0.27465 -0.1484 -0.16222 0.54122 -0.35387 -0.44765 ... -0.35554 -0.21266 0.048219 -0.023976 0.23689 -0.11721 0.012078 -0.15143 -0.19005 0.043254

1 rows × 301 columns

Activity

  1. jcrist commented on Apr 19, 2017

    @jcrist
    Member

    What version of dask are you using? I looked at that repo (and this one: https://github.com/Louis-Tian/dask_merge_issue) and didn't see the data. If you can provide a reproducible example and versions used (dask, pandas, partd, and numpy) that would help in debugging.

  2. Louis-Tian commented on Apr 19, 2017

    @Louis-Tian
    Author

    @jcrist my apology. didn't make the repo public. Data are too big to be pushed to github, so please checkout the gitlab repo instead.

    I am running

    • python 3.6
    • dask 0.14.1
    • pandas 0.19.2
    • partd 0.3.7
    • numpy 1.12.1

    The requirements.txt in the repo has the complete environment.

  3. Louis-Tian commented on Apr 19, 2017

    @Louis-Tian
    Author

    Run the same code on a Windows machine with python 2.7 and dask started throwing UnicodeEncodeError while pandas still runs okay.


    import dask.dataframe as dd
    import pandas as pd
    dask_df = dd.read_csv('./fastText_extract.csv', encoding='utf-7', usecols=list(range(1, 302)))
    pd_df = pd.read_csv('./fastText_extract.csv', encoding='utf-8', usecols=list(range(1, 302)))
    features = pd.read_csv('./features.csv', encoding='utf-8', usecols=[1])
    pd_df[pd_df['word'] == 'leonid']
    word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
    15810 leonid -0.11284 0.27493 -0.11428 0.27465 -0.1484 -0.16222 0.54122 -0.35387 -0.44765 ... -0.35554 -0.21266 0.048219 -0.023976 0.23689 -0.11721 0.012078 -0.15143 -0.19005 0.043254

    1 rows × 301 columns

    features[features['word'] == 'leonid']
    word
    614 leonid

    Different merge result using pandas and dask

    result = pd_df.merge(features, on='word', how='right')
    result[result[u'word'] == u'leonid']
    word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
    15810 leonid -0.11284 0.27493 -0.11428 0.27465 -0.1484 -0.16222 0.54122 -0.35387 -0.44765 ... -0.35554 -0.21266 0.048219 -0.023976 0.23689 -0.11721 0.012078 -0.15143 -0.19005 0.043254

    1 rows × 301 columns

    result = dask_df.merge(features, on='word', how='right').compute()
    result[result[u'word'] == u'leonid']
    ---------------------------------------------------------------------------
    
    UnicodeEncodeError                        Traceback (most recent call last)
    
    <ipython-input-8-a8196fb6e4ca> in <module>()
    ----> 1 result = dask_df.merge(features, on='word', how='right').compute()
          2 result[result[u'word'] == u'leonid']
    
    
    C:\Anaconda2\lib\site-packages\dask\dataframe\core.pyc in merge(self, right, how, on, left_on, right_on, left_index, right_index, suffixes, indicator, npartitions, shuffle)
       2378                      right_index=right_index, suffixes=suffixes,
       2379                      npartitions=npartitions, indicator=indicator,
    -> 2380                      shuffle=shuffle)
       2381 
       2382     @derived_from(pd.DataFrame)
    
    
    C:\Anaconda2\lib\site-packages\dask\dataframe\multi.pyc in merge(left, right, how, on, left_on, right_on, left_index, right_index, suffixes, indicator, npartitions, shuffle, max_branch)
        394             right_on = False
        395             right_index = True
    --> 396         right = from_pandas(right, npartitions=1)  # turn into DataFrame
        397 
        398     # Both sides are now dd.DataFrame or dd.Series objects
    
    
    C:\Anaconda2\lib\site-packages\dask\dataframe\io\io.pyc in from_pandas(data, npartitions, chunksize, sort, name)
        180         npartitions = int(ceil(nrows / chunksize))
        181 
    --> 182     name = name or ('from_pandas-' + tokenize(data, chunksize))
        183 
        184     if not nrows:
    
    
    C:\Anaconda2\lib\site-packages\dask\base.pyc in tokenize(*args, **kwargs)
        366     if kwargs:
        367         args = args + (kwargs,)
    --> 368     return md5(str(tuple(map(normalize_token, args))).encode()).hexdigest()
    
    
    C:\Anaconda2\lib\site-packages\dask\utils.pyc in __call__(self, arg)
        547             pass
        548         else:
    --> 549             return impl(arg)
        550         # Is a lazy registration function present?
        551         toplevel, _, _ = typ.__module__.partition('.')
    
    
    C:\Anaconda2\lib\site-packages\dask\base.pyc in normalize_dataframe(df)
        320         data = [block.values for block in df._data.blocks]
        321         data += [df.columns, df.index]
    --> 322         return list(map(normalize_token, data))
        323 
        324 
    
    
    C:\Anaconda2\lib\site-packages\dask\utils.pyc in __call__(self, arg)
        547             pass
        548         else:
    --> 549             return impl(arg)
        550         # Is a lazy registration function present?
        551         toplevel, _, _ = typ.__module__.partition('.')
    
    
    C:\Anaconda2\lib\site-packages\dask\base.pyc in normalize_array(x)
        343                 data = md5('-'.join(x.flat).encode('utf-8')).hexdigest()
        344             except TypeError:
    --> 345                 data = md5(b'-'.join([str(item).encode() for item in x.flat])).hexdigest()
        346         else:
        347             try:
    
    
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)
    

    Use smaller subset of data, same result

    dask_df = dd.read_csv('./fastText_sample.csv', encoding='utf-8', usecols=list(range(1, 302)))
    pd_df = pd.read_csv('./fastText_sample.csv', encoding='utf-8', usecols=list(range(1, 302)))
    features = pd.read_csv('./features.csv', encoding='utf-8', usecols=[1])
    result = pd_df.merge(features, on='word', how='right')
    result[result['word'] == 'leonid']
    word 0 1 2 3 4 5 6 7 8 ... 290 291 292 293 294 295 296 297 298 299
    810 leonid -0.11284 0.27493 -0.11428 0.27465 -0.1484 -0.16222 0.54122 -0.35387 -0.44765 ... -0.35554 -0.21266 0.048219 -0.023976 0.23689 -0.11721 0.012078 -0.15143 -0.19005 0.043254

    1 rows × 301 columns

    result = dask_df.merge(features, on='word', how='right').compute()
    result[result['word'] == 'leonid']
    ---------------------------------------------------------------------------
    
    UnicodeEncodeError                        Traceback (most recent call last)
    
    <ipython-input-25-74d8e0271756> in <module>()
    ----> 1 result = dask_df.merge(features, on='word', how='right').compute()
          2 result[result['word'] == 'leonid']
    
    
    C:\Anaconda2\lib\site-packages\dask\dataframe\core.pyc in merge(self, right, how, on, left_on, right_on, left_index, right_index, suffixes, indicator, npartitions, shuffle)
       2378                      right_index=right_index, suffixes=suffixes,
       2379                      npartitions=npartitions, indicator=indicator,
    -> 2380                      shuffle=shuffle)
       2381 
       2382     @derived_from(pd.DataFrame)
    
    
    C:\Anaconda2\lib\site-packages\dask\dataframe\multi.pyc in merge(left, right, how, on, left_on, right_on, left_index, right_index, suffixes, indicator, npartitions, shuffle, max_branch)
        394             right_on = False
        395             right_index = True
    --> 396         right = from_pandas(right, npartitions=1)  # turn into DataFrame
        397 
        398     # Both sides are now dd.DataFrame or dd.Series objects
    
    
    C:\Anaconda2\lib\site-packages\dask\dataframe\io\io.pyc in from_pandas(data, npartitions, chunksize, sort, name)
        180         npartitions = int(ceil(nrows / chunksize))
        181 
    --> 182     name = name or ('from_pandas-' + tokenize(data, chunksize))
        183 
        184     if not nrows:
    
    
    C:\Anaconda2\lib\site-packages\dask\base.pyc in tokenize(*args, **kwargs)
        366     if kwargs:
        367         args = args + (kwargs,)
    --> 368     return md5(str(tuple(map(normalize_token, args))).encode()).hexdigest()
    
    
    C:\Anaconda2\lib\site-packages\dask\utils.pyc in __call__(self, arg)
        547             pass
        548         else:
    --> 549             return impl(arg)
        550         # Is a lazy registration function present?
        551         toplevel, _, _ = typ.__module__.partition('.')
    
    
    C:\Anaconda2\lib\site-packages\dask\base.pyc in normalize_dataframe(df)
        320         data = [block.values for block in df._data.blocks]
        321         data += [df.columns, df.index]
    --> 322         return list(map(normalize_token, data))
        323 
        324 
    
    
    C:\Anaconda2\lib\site-packages\dask\utils.pyc in __call__(self, arg)
        547             pass
        548         else:
    --> 549             return impl(arg)
        550         # Is a lazy registration function present?
        551         toplevel, _, _ = typ.__module__.partition('.')
    
    
    C:\Anaconda2\lib\site-packages\dask\base.pyc in normalize_array(x)
        343                 data = md5('-'.join(x.flat).encode('utf-8')).hexdigest()
        344             except TypeError:
    --> 345                 data = md5(b'-'.join([str(item).encode() for item in x.flat])).hexdigest()
        346         else:
        347             try:
    
    
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 1: ordinal not in range(128)
    
  4. jcrist commented on Apr 21, 2017

    @jcrist
    Member

    Thanks for the bug report, I've tracked this down to a bug in how we hash pandas object arrays (specifically object arrays that have missing values). Fix coming soon.

  5. jcrist commented on Apr 21, 2017

    @jcrist
    Member

    Fixed in #2243. Using your code, this now passes:

    import dask.dataframe as dd
    import pandas as pd
    dask_df = dd.read_csv('./fastText_extract.csv', encoding='utf-8', usecols=list(range(1, 302)))
    pd_df = pd.read_csv('./fastText_extract.csv', encoding='utf-8', usecols=list(range(1, 302)))
    features = pd.read_csv('./features.csv', encoding='utf-8', usecols=[1])
    
    presult = pd_df.merge(features, on='word', how='right').set_index('word').sort_index()
    dresult = dask_df.merge(features, on='word', how='right').compute().set_index('word').sort_index()
    assert presult.equals(dresult)
  6. added this to the 0.14.2 milestone on May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions