Skip to content

set_index with sorted=True can drop rows in dataframes with empty partitions #8735

Closed

Description

@albarji

What happened: when setting a new index on a Dask DataFrame through the set_index method, in some cases of DataFrame with empty partitions, some rows are dropped in the process. This only seems to happen when using the argument sorted=True.

What you expected to happen: set_index should not drop rows.

Minimal Complete Verifiable Example:

import pandas as pd
import dask.dataframe as dd

# Create dataframe with empty partitions
data1 = dd.from_pandas(
    pd.DataFrame(
        index=[0, 1, 2],
        data={
            "datA": ["A1", "A2", "A3"],
            "datB": ["B1", "B2", "B3"],
            "datC": ["C1", "C2", "C3"],
            "new_index": [1, 2, 3]
        }
    ),
    npartitions=1
)
data_dummy = dd.from_pandas(  # These data will be removed to create empty partitions
    pd.DataFrame(
        index=[9999],
        data={
            "datA": ["xxx"],
            "datB": ["xxx"],
            "datC": ["xxx"],
            "new_index": [9999]
        }
    ),
    npartitions=1
)
data2 = dd.from_pandas(
    pd.DataFrame(
        index=[3],
        data={
            "datA": ["A4"],
            "datB": ["B4"],
            "datC": ["C4"],
            "new_index": [4]
        }
    ),
    npartitions=1
)
ddf = dd.concat([data1, data_dummy, data2, data_dummy])
ddf = ddf[ddf["datA"] != "xxx"]

# Set index, the last row gets dropped in the result
print(ddf.set_index("new_index", sorted=True).compute())

The test Dataframe ddf looks like

  datA datB datC  new_index
0   A1   B1   C1          1
1   A2   B2   C2          2
2   A3   B3   C3          3
4   A4   B4   C4          4

with partitions:

  • Partition 0: indices 0, 1 and 2
  • Partition 1: empty
  • Partition 2: index 3
  • Partition 3: empty

After set_index, the resultant DataFrame is

          datA datB datC
new_index               
1           A1   B1   C1
2           A2   B2   C2
3           A3   B3   C3

where the last row has been dropped.

This does not happen if sorted=True is not used:

print(ddf.set_index("new_index").compute())
          datA datB datC
new_index               
1           A1   B1   C1
2           A2   B2   C2
3           A3   B3   C3
4           A4   B4   C4

Environment:

  • Dask version: 2022.01.0
  • Python version: 3.9.7
  • Operating System: Ubuntu 16.04.6 LTS
  • Install method (conda, pip, source): pip

Activity

  1. jsignell commented on Feb 18, 2022

    @jsignell
    Member

    Thanks for writing this up @albarji. I have been struggling with similar issues in #8517 I think the logic isn't quite right in:

    def compute_and_set_divisions(df, **kwargs):
    mins = df.index.map_partitions(M.min, meta=df.index)
    maxes = df.index.map_partitions(M.max, meta=df.index)
    mins, maxes = compute(mins, maxes, **kwargs)
    mins = remove_nans(mins)
    maxes = remove_nans(maxes)
    if (
    sorted(mins) != list(mins)
    or sorted(maxes) != list(maxes)
    or any(a > b for a, b in zip(mins, maxes))
    ):
    raise ValueError(
    "Partitions must be sorted ascending with the index", mins, maxes
    )
    df.divisions = tuple(mins) + (list(maxes)[-1],)
    overlap = [i for i in range(1, len(mins)) if mins[i] >= maxes[i - 1]]
    return fix_overlap(df, overlap) if overlap else df

    I'm taking a look now

  2. jsignell commented on Feb 23, 2022

    @jsignell
    Member

    Ok I think the issue has to do with how fix_overlap works. Basically when there are empty partitions and overlap, the function can drop rows. I am including the fix for this on #8517.

  3. albarji commented on Feb 24, 2022

    @albarji
    Author

    Thanks a lot for looking into this! 👍

  4. vdytyniak-exos commented on Jul 15, 2022

    @vdytyniak-exos

    Hi. I see the same issue, but I don't have empty partitions. Can someone explain why I lost records after set_index?

    df = pd.DataFrame({
        'ts': [1, 1, 2, 2, 3, 3, 3, 3],
        'value': 'abc'
    })
    ddf = dd.from_pandas(df, npartitions=3)
    ddf = ddf.set_index('ts', sorted=True)
    print(ddf.compute())
    
       value
    ts      
    1    abc
    1    abc
    2    abc
    2    abc
    3    abc
    3    abc
    

    Dask: 2022.5.0
    Python: 3.10.4
    OS: macOS Monterey 12.1
    Install method: pip

  5. jsignell commented on Aug 2, 2022

    @jsignell
    Member

    Oh that is a nice clear example of bad behavior. I promoted it to a new issue.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions