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
BUG: Dask dataframe cannot handle string index #3269
changed the title [-]How to call DataFrame.loc() with row labels in a delayed array?[/-][+]How to call Series.isin() or DataFrame.loc() with multiple row labels in a delayed Series?[/+]on Mar 11, 2018
I think the problem is related to strings, I believe it's a bug in dask handling of string indices.
import dask.dataframe as dd
import pandas as pd
a = dd.from_pandas(pd.DataFrame({'x':[0.1,0.2,0.3,0.4,0.5]}, index=["a","b","c","bc","abc"]), npartitions=3)
b = dd.from_pandas(pd.DataFrame({'word':["bc","abc"]}), npartitions=3)
a.loc[b.word]
TypeError: '<' not supported between instances of 'int' and 'str'
Another issue happens even with standard pd.Series
import dask.dataframe as dd
import pandas as pd
a = dd.from_pandas(pd.DataFrame({'x':[0.1,0.2,0.3,0.4,0.5]}, index=["a","b","c","bc","abc"]), npartitions=3)
b = pd.DataFrame({'word':["bc","abc"]})
a.loc[b.word]
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
changed the title [-]How to call Series.isin() or DataFrame.loc() with multiple row labels in a delayed Series?[/-][+]How to call Series.loc() or DataFrame.loc() with multiple row labels in a delayed Series?[/+]on Mar 11, 2018
I agree that this looks like a bug in dask dataframe. When we call loc on a dask.dataframe.Series we are currently trying to align the two dataframe/series objects by divisions, which is where the exception is occurring. Instead we actually need to do a more complex operation where every partition of the indexer talks to every partition of the indexed.
This should be fixed if anyone has interest.
It's worth pointing out though that this will never be nearly as fast as in the Pandas case.
I've been experimenting with this issue on my local copy. I would say the main issue is that DataFrame.map_partitions does not actually just map a functions to all partitions in the dataframe, but it also performs some alignment under the hood. That's because it dispatches to core.map_partitions which perform alignment automatically (a more specific name would be map_aligned_partitions)
I feel the best way to do this is to actually implement different versions of map_partitions:
Something on these lines:
"align" version, will take the _Frame arguments and align their index
"forward" version, which will forward the arguments unchanged (warning: this could blow up)
"broadcast" version, which will do the complex operation you were referring to. So the function will be applied to "paired partitions"l
With regard to the present issue, if the Series is of type bool, you need to align partitions, otherwise, you need to broadcast and concat the results
Sorry for the necro, just wanted to put my 2 cents in.
I ran into this problem when I wanted to select the groupwise minima over a certain column like this:
I implemented the suggested fix of having a broadcast_map_partitions() function which maps the given function over all combinations of partitions. I implemented this naively by copying map_partitions, removing the alignment that happens, and changing partitionwise_graph to create a partition-to-partition graph. This worked great, but memory usage was insane. My adaptive cluster peaked at 11 terabyte of memory usage on a calculation that previously spiked at ~100 gigs. I guess each partition is copied to the worker where each other partition lives? Might be an oversight in my implementation too, but at least it worked.
Anyway, I found that just relying on Dask's divisions and alignment works way better.
On my big dataset, this takes the same time but uses ~100x less memory. And in my specific case, I could even cut out the extra steps by combining idxmin and loc in a single pass using groupby()._aca_agg() directly.
@dask/maintenance can someone edit the title of this issue to better reflect the contents? I'd suggest something like "BUG: Dask dataframe cannot handle string index"
A minimal, reproducible example for this issue is given here: #3269 (comment)
changed the title [-]How to call Series.loc() or DataFrame.loc() with multiple row labels in a delayed Series?[/-][+]BUG: Dask dataframe cannot handle string index[/+]on Oct 12, 2021
Activity
mrocklin commentedon Mar 10, 2018
For faster feedback, I recommend constructing a minimal example.
ArcticLampyrid commentedon Mar 11, 2018
When I try to use
pWord.loc[wordInfoTable.word.str.slice(stop=1)]The code doesn't work.
gabrielelanaro commentedon Mar 11, 2018
pWordis indexed overwordcolumn. WhilewordInfoTableis indexed over some random integer index[-]How to call DataFrame.loc() with row labels in a delayed array?[/-][+]How to call Series.isin() or DataFrame.loc() with multiple row labels in a delayed Series?[/+]gabrielelanaro commentedon Mar 11, 2018
I think the problem is related to strings, I believe it's a bug in dask handling of string indices.
Another issue happens even with standard pd.Series
[-]How to call Series.isin() or DataFrame.loc() with multiple row labels in a delayed Series?[/-][+]How to call Series.loc() or DataFrame.loc() with multiple row labels in a delayed Series?[/+]ArcticLampyrid commentedon Mar 11, 2018
@gabrielelanaro
When both
aandbare the type from pandasIt can work well
mrocklin commentedon Mar 11, 2018
Thanks for the minimal example in #3269 (comment) @gabrielelanaro !
I agree that this looks like a bug in dask dataframe. When we call loc on a dask.dataframe.Series we are currently trying to align the two dataframe/series objects by divisions, which is where the exception is occurring. Instead we actually need to do a more complex operation where every partition of the indexer talks to every partition of the indexed.
This should be fixed if anyone has interest.
It's worth pointing out though that this will never be nearly as fast as in the Pandas case.
gabrielelanaro commentedon Mar 11, 2018
I've been experimenting with this issue on my local copy. I would say the main issue is that
DataFrame.map_partitionsdoes not actually just map a functions to all partitions in the dataframe, but it also performs some alignment under the hood. That's because it dispatches tocore.map_partitionswhich perform alignment automatically (a more specific name would bemap_aligned_partitions)I feel the best way to do this is to actually implement different versions of
map_partitions:Something on these lines:
With regard to the present issue, if the Series is of type bool, you need to align partitions, otherwise, you need to broadcast and concat the results
OliverHofkens commentedon Jan 15, 2020
Sorry for the necro, just wanted to put my 2 cents in.
I ran into this problem when I wanted to select the groupwise minima over a certain column like this:
I implemented the suggested fix of having a
broadcast_map_partitions()function which maps the given function over all combinations of partitions. I implemented this naively by copyingmap_partitions, removing the alignment that happens, and changingpartitionwise_graphto create a partition-to-partition graph. This worked great, but memory usage was insane. My adaptive cluster peaked at 11 terabyte of memory usage on a calculation that previously spiked at ~100 gigs. I guess each partition is copied to the worker where each other partition lives? Might be an oversight in my implementation too, but at least it worked.Anyway, I found that just relying on Dask's divisions and alignment works way better.
On my big dataset, this takes the same time but uses ~100x less memory. And in my specific case, I could even cut out the extra steps by combining
idxminandlocin a single pass usinggroupby()._aca_agg()directly.GenevieveBuckley commentedon Oct 12, 2021
@dask/maintenance can someone edit the title of this issue to better reflect the contents? I'd suggest something like "BUG: Dask dataframe cannot handle string index"
A minimal, reproducible example for this issue is given here: #3269 (comment)
[-]How to call Series.loc() or DataFrame.loc() with multiple row labels in a delayed Series?[/-][+]BUG: Dask dataframe cannot handle string index[/+]martindurant commentedon Oct 12, 2021
(title updated)
jsignell commentedon Oct 13, 2021
This is an issue that I think could be pretty decently handled with a good explanation in an error message. I am opening a PR with one option.