Repository navigation
Conversation
|
Ah, so in this case the optimization doesn't trigger because the layer just before the drop isn't a blockwise operation. Is that right? |
|
Hrm, as an alternative, we might try the following:
We can also look at the shuffle code, and see if the layers of the join just preceding the Thoughts on that plan? |
|
cc @eriknw. |
|
I think this may still trigger a copy. I believe that triggers fancy indexing, which always copies? In [69]: a = np.random.random((100, 4))
In [70]: b = np.random.random((100, 4)).astype(int)
In [71]: df = pd.concat([pd.DataFrame(a), pd.DataFrame(b)], axis=1, ignore_index=True, copy=False)
In [72]: df._data.blocks[0].values.base is a
Out[72]: True
In [73]: np.may_share_memory(df[[0, 1, 2, 3, 4, 5]]._data.blocks[0].values, a)
Out[73]: False |
This is where my understanding of |
|
Right, that means that the drop call is at the very beginning of the Blockwise set of calls. It directly affects the input. In this case we can't safely add the inplace keyword in general. |
|
@TomAugspurger is there a way in pandas to return a new dataframe with a column missing that doesn't copy the underlying data? |
I believe that @VibhuJawa used this approach to get a speedup, see his comment. Is there any reason this would improve performance if it wasn't avoiding a copy? |
|
(we probably should have asked you about this a while ago :) ) |
Perhaps cudf and pandas have different behaviors here? |
Today, something like I believe that whether a copy needs to be made depends on whether the column is part of a larger Block. IIRC, the motivating case here was dropping a column It's quite likely that cudf doesn't have the same limitation. |
|
Well, it would be nice if this improved performance on the Pandas side as well. cuDF is a bit more flexible than Pandas, so I'd prefer to find a solution that works well with Pandas and then ensure that that also works with cudf. Perhaps what I want is a df = df.copy(shallow=True)
del df["_partitions"]This doesn't appear to exist though. I would hope that even if we did need to copy the block that we would only need to copy that one block and not all of the others. Also, my hope is that the |
Is there good reason to avoid using return df3[[c for c in df.columns if c != "_partitions"]]ranther than del df3["_partitions"]
return df3just because it improves cudf-based performance and not pandas-based performance? I understand that we want to improve both cases in the long run, but I am not understanding if there is a good reason to avoid this change for now. |
|
I'm fine with that personally. It isn't significantly more complex and doesn't degrade performance elsewhere. Want to throw up a PR? |
Sounds good to me, but I most likely won't get to it until later tonight. |
In [56]: df = pd.DataFrame(randn(10,3), columns=list('ABC'))
In [57]: df
Out[57]:
A B C
0 0.114722 1.558930 -2.126745
1 0.766587 0.046552 1.090657
2 0.702984 0.344245 -0.236198
3 0.682041 -0.047099 0.091260
4 0.347412 -0.679305 0.915347
5 0.127372 0.339934 -0.590698
6 -1.008189 -0.037308 -0.158385
7 0.237671 0.589957 -1.010068
8 -1.426825 -1.557856 0.389637
9 0.285834 -1.140209 -0.350228
In [58]: df2 = df.copy(deep=False)
In [59]: del df2['B']
In [60]: df2
Out[60]:
A C
0 0.114722 -2.126745
1 0.766587 1.090657
2 0.702984 -0.236198
3 0.682041 0.091260
4 0.347412 0.915347
5 0.127372 -0.590698
6 -1.008189 -0.158385
7 0.237671 -1.010068
8 -1.426825 0.389637
9 0.285834 -0.350228
In [61]: df
Out[61]:
A B C
0 0.114722 1.558930 -2.126745
1 0.766587 0.046552 1.090657
2 0.702984 0.344245 -0.236198
3 0.682041 -0.047099 0.091260
4 0.347412 -0.679305 0.915347
5 0.127372 0.339934 -0.590698
6 -1.008189 -0.037308 -0.158385
7 0.237671 0.589957 -1.010068
8 -1.426825 -1.557856 0.389637
9 0.285834 -1.140209 -0.350228 |
|
Indeed ... In [8]: df = pd.DataFrame({"x": np.arange(10000000)})
In [9]: %timeit df.copy()
18.4 ms ± 278 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [10]: %timeit df.copy(deep=False)
34.3 µs ± 1.44 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each) |
|
So it seems like we should maybe change the definition of |
|
I'm thinking we can close this in favor of #5675? |
|
This is still an interesting approach that I suspect will be useful in the future. @eriknw , I suspect that you in particular might be interested in this. If so, is there a way in which you would like this to be preserved? |
|
Thanks for the ping! This would be a good optimization to perform (when applicable). |
Closes #5664
Adds
optimize_dropfunction to addinplace=Trueto tasks withM.drop. Theinplacechange is not performed in the case that the corresponding tasks has no dependencies within theBlockwisesubgraph (to avoid modifying input data).Note: The requirement to avoid modifying tasks that depend on subgraph inputs is currently skipping the optimization for merge operations (the original motivation for the in-place drop). For this reason, I added a
forceargument to effectively ignore this criteria. For example, the optimization can be used by adding these lines to the dask-cuda merge benchmark (just before theddf_join.persist()call):Before Optimization:
After Optimization:
This performance difference is a clear motivation to find a way to capture the "
merge" situation (using somethin better/safer than theforce=Truehack/workaround).black dask/flake8 dask