Skip to content

Dataframe index is not sorted after .set_index() #3376

Closed

Description

@bnaul

From the docs+source it appears that the output of ddf.set_index() should be sorted by index, but this doesn't seem to be the case.

import dask, dask.dataframe as dd, distributed

In [1]: c = distributed.Client('localhost:8786')
In [2]: ddf = dd.read_csv('gs://<bucket>/2018-01-*.csv.gz', compression='gzip').set_index('uid').compute()
In [3]: ddf.index.is_monotonic
Out[3]: False
In [4]: ddf.head().index
Out[4]:
Index(['00004cd2-8a03-4dc7-85a9-f32ded88d6f6',
       '00004cd2-8a03-4dc7-85a9-f32ded88d6f6',
       '00009051-c962-460b-8090-c1c9d45df180',
       '00009051-c962-460b-8090-c1c9d45df180',
       '00009051-c962-460b-8090-c1c9d45df180'],
      dtype='object', name='uid')

The inputs are randomly ordered, and the output appears to be mostly-but-not-quite sorted; maybe sorted within each partition but not reassembled correctly?

Activity

  1. mrocklin commented on Apr 5, 2018

    @mrocklin
    Member
  2. bnaul commented on Apr 5, 2018

    @bnaul
    ContributorAuthor

    Interesting; do you think things like .groupby().apply() would be affected by this level of non-sortedness, or will everything behave OK as long as the partitions/divisions are consistent? The fact that the index isn't sorted within partitions might not be a problem per se, I was mostly just surprised since I saw several references to .set_index performing a sort.

  3. mrocklin commented on Apr 5, 2018

    @mrocklin
    Member
  4. TomAugspurger commented on Apr 5, 2018

    @TomAugspurger
    Member
  5. mrocklin commented on Apr 19, 2018

    @mrocklin
    Member

    @bnaul do you have any interest in fixing this? It seems like a well isolated contribution.

  6. added
    good first issue
    on Apr 19, 2018
  7. bnaul commented on Apr 20, 2018

    @bnaul
    ContributorAuthor

    I am interested; will try to take a look within the next few days

  8. bnaul commented on Apr 22, 2018

    @bnaul
    ContributorAuthor

    @mrocklin I poked around a bit more: I have only been able to reproduce this behavior w/ the distributed scheduler and lot of data, so unfortunately I don't have a simple test case to share (nor can I use the single-threaded scheduler for debugging).

    It seems like sorting is not the issue per se, but rather the shuffle/partition itself:

    ddf = dd.read_csv('gs://<bucket>/2018-01-*.csv.gz', compression='gzip').set_index('uid').persist()
    
    # found a partition where one order violation occurred
    In [90]: ddf.get_partition(255)
    Out[90]:
    Dask DataFrame Structure:
                                              lat      lng
    npartitions=1
    135c2fc8-8861-4dfd-9663-4b79438d311f  float64  float64
    137266de-0abd-473c-9915-1f1330d58d71      ...      ...
    Dask Name: get-partition, 3457 tasks
    
    In [91]: ddf.get_partition(255).tail()
    Out[91]:
                                                lat        lng
    uid
    f66de7bc-49c9-4a8d-8090-df3a0d61fac6  ... ...
    f66de7bc-49c9-4a8d-8090-df3a0d61fac6  ... ...
    f66de7bc-49c9-4a8d-8090-df3a0d61fac6  ... ...  
    f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0  ... ...
    f66eb47d-b3a3-42bc-8e76-ac739ef8d7a0  ... ...
    
    In [92]: ddf.map_partitions(lambda df: df.index.is_monotonic).compute().all()
    Out[92]:
    True
    

    The divisions themselves are in sorted order, and each partition individually is sorted, but a few pieces (in this case 6 index values, out of a 4M row dataframe) just end up in the wrong place. Any ideas how this might come to be?

  9. mrocklin commented on Apr 22, 2018

    @mrocklin
    Member
  10. mrocklin commented on Apr 22, 2018

    @mrocklin
    Member
  11. bnaul commented on Apr 23, 2018

    @bnaul
    ContributorAuthor

    Yep this is 0c443fd49cd63ce23f803706c7f9762f0b8bb165. I think I could come up with a reproducible example if it would help, but like I said it seems like a rare problem and I can't reproduce it on anything smaller than millions of rows. I have managed to get the same behavior w/ a subset of columns: now I have a few thousand files that look like

    i,uid
    0,002862bc-67d5-43a4-bbb7-da659a987f0c
    1,002862bc-67d5-43a4-bbb7-da659a987f0c
    2,002862bc-67d5-43a4-bbb7-da659a987f0c
    3,002862bc-67d5-43a4-bbb7-da659a987f0c
    4,002862bc-67d5-43a4-bbb7-da659a987f0c
    5,03b076de-9038-48d3-9609-178690ca808e
    6,03b076de-9038-48d3-9609-178690ca808e
    7,03b076de-9038-48d3-9609-178690ca808e
    8,03b076de-9038-48d3-9609-178690ca808e
    9,03b076de-9038-48d3-9609-178690ca808e
    

    Loading these, setting the index, and computing gives the above behavior.

  12. mrocklin commented on Apr 23, 2018

    @mrocklin
    Member
  13. bnaul commented on Apr 23, 2018

    @bnaul
    ContributorAuthor

    The following runs in about 5 minutes on my local machine:

    import numpy as np, pandas as pd, dask.dataframe as dd, distributed, uuid
    
    c = distributed.Client('localhost:8786')
    
    ids = [uuid.uuid4() for i in range(100_000)]
    df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
                       'x': 0})
    dd.from_pandas(df, npartitions=4000).to_csv('/tmp/test_*.csv', index=False)
    del df
    
    ddf = dd.read_csv('/tmp/test_*.csv').set_index('uid').persist()
    print(ddf.index.compute().is_monotonic)  # False
    

    For me the very first partition was incorrect:

    In [35]: ddf.get_partition(0)
    Out[35]:
    Dask DataFrame Structure:
                                              x
    npartitions=1
    00001f4f-7f25-43d0-a939-cc485a359e7e  int64
    00143d26-016c-427a-84ff-8d1fca4bfa08    ...
    Dask Name: get-partition, 4001 tasks
    
    In [36]: ddf.get_partition(0).tail()
    Out[36]:
                                          x
    uid
    f5f3f922-b22c-4537-90fe-f8a311d26f14  0
    f5f3f922-b22c-4537-90fe-f8a311d26f14  0
    f5f3f922-b22c-4537-90fe-f8a311d26f14  0
    f5f3f922-b22c-4537-90fe-f8a311d26f14  0
    f5f3f922-b22c-4537-90fe-f8a311d26f14  0
    
  14. mrocklin commented on Apr 23, 2018

    @mrocklin
    Member
  15. bnaul commented on Apr 23, 2018

    @bnaul
    ContributorAuthor

    Same behavior w/o read_csv:

    c = distributed.Client('localhost:8786')
    ids = [uuid.uuid4() for i in range(100_000)]
    df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
                       'x': 0})
    ddf = dd.from_pandas(df, npartitions=4000).set_index('uid').persist()
    print(ddf.index.compute().is_monotonic)  # False
    

    But running w/ the default dask scheduler works fine:

    ids = [uuid.uuid4() for i in range(100_000)]
    df = pd.DataFrame({'uid': np.concatenate([np.repeat(uid, np.random.randint(1, 750)) for uid in ids]),
                       'x': 0})
    ddf = dd.from_pandas(df, npartitions=4000).set_index('uid')
    print(ddf.index.compute().is_monotonic)  # True
    
  16. mrocklin commented on Apr 23, 2018

    @mrocklin
    Member
  17. mrocklin commented on Apr 23, 2018

    @mrocklin
    Member
  18. bnaul commented on Apr 23, 2018

    @bnaul
    ContributorAuthor

    Yep, shuffle='tasks' gives the same incorrect behavior. I tried decreasing the size of the dataframe but with fewer partitions it seems to always end up sorted (4000 breaks, 2000 is OK). Not sure what to make of that 🙃

  19. bnaul commented on May 16, 2019

    @bnaul
    ContributorAuthor

    Not sure what changed but when I run the above example now the index is sorted. Guess I'll close...?

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

    good first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions