You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DataFrame.loc errors when row indexing with list, even with set number of partitions #8513
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
importdask.dataframeasddimportpandasaspdprimary=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 exampleprimary.loc[test_clust['label'].index.values, :]
The final line throws the following error: KeyError: 'Cannot index with list against unknown division'
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.
@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)?
Huh ok I made a reproducible example and I think what you need is to add sorted=True to the set_index call:
importdaskimportdask.dataframeasdd# write a single csv to a filedask.datasets.timeseries(freq="1H").compute().to_csv("timeseries.csv")
# read in from the file and repartitionddf=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).
Activity
jsignell commentedon Dec 22, 2021
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.jlehrer1 commentedon Dec 22, 2021
@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 likedf.set_index(df.index)?jlehrer1 commentedon Dec 22, 2021
Actually, that does not fix the problem. When I add
The error persists.
jsignell commentedon Dec 23, 2021
Huh ok I made a reproducible example and I think what you need is to add
sorted=Trueto theset_indexcall: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).
jlehrer1 commentedon Dec 23, 2021
Thank you! This is clear