Skip to content

DataFrame.loc errors when row indexing with list, even with set number of partitions #8513

Closed

Description

@jlehrer1

What happened:
Recieved KeyError: 'Cannot index with list against unknown division' when doing row-wise indexing on a DataFrame wit ha set number of partitions

What you expected to happen:
Return a new DataFramee with the indexed rows

import dask.dataframe as dd
import pandas as pd 

primary = dd.read_csv(path, blocksize=25e6, assume_missing=True).repartition(npartitions=10)
labels = pd.read_csv(other_path)

test_clust = labels.loc[labels['label']==1.0, :] # for example

primary.loc[test_clust['label'].index.values, :]

The final line throws the following error: KeyError: 'Cannot index with list against unknown division'

Environment:
Jupyterm, Python 3.9

  • Dask version: 2021.10.0
  • Python version: 3.9
  • Operating System: MacOS Monterey
  • Install method (conda, pip, source): pip

Activity

  1. jsignell commented on Dec 22, 2021

    @jsignell
    Member

    Yeah so that error is basically saying that primary.known_divisions == False. Even thought you have a set number of partitions, that doesn't mean that Dask knows the bounds of the index for each partition. The way to get known divisions is by setting the index.

  2. jlehrer1 commented on Dec 22, 2021

    @jlehrer1
    Author

    @jsignell Can you elaborate further? I didn't see this specified in the documentation anywhere. The index is, by default, a RangeIndex. Setting the index tends to be very slow, so can I just do something self referential like df.set_index(df.index)?

  3. jlehrer1 commented on Dec 22, 2021

    @jlehrer1
    Author

    Actually, that does not fix the problem. When I add

    df = df.set_index(df.index, divisions=list(range(0, 10)))

    The error persists.

  4. jsignell commented on Dec 23, 2021

    @jsignell
    Member

    Huh ok I made a reproducible example and I think what you need is to add sorted=True to the set_index call:

    import dask
    import dask.dataframe as dd
    
    # write a single csv to a file
    dask.datasets.timeseries(freq="1H").compute().to_csv("timeseries.csv")
    
    # read in from the file and repartition
    ddf = dd.read_csv("timeseries.csv").repartition(npartitions=10)
    ddf.divisions
    # (None, None, None, None, None, None, None, None, None, None, None)
    
    ddf = ddf.set_index(ddf.index, sorted=True)
    ddf.divisions
    # (0, 72, 144, 216, 288, 360, 432, 504, 576, 648, 719)

    That should work for your case, but I do think there is a codepath that doesn't work quite as expected if the index is the same as the original ddf. FWIW this whole pattern is being reconsidered and we are currently thinking of ways to make this smoother, faster, and easier to understand (see #8435 for example).

  5. jlehrer1 commented on Dec 23, 2021

    @jlehrer1
    Author

    Thank you! This is clear

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