Repository navigation
Modify dd.DataFrame.drop to use shallow copy - #5675
Conversation
| df = pd.DataFrame(np.random.randn(10, 3), columns=list("ABC")) | ||
| df2 = drop_by_shallow_copy(df, cols) | ||
|
|
||
| assert_eq(df2, df.drop(columns=cols)) |
There was a problem hiding this comment.
I think that we probably don't need to test this directly. The behavior of drop should already be tested at the public API level. I don't think that it's necessary to test this internal operation.
What might be more important to test is that we don't perform a deep copy, so maybe checking that df2.C._values is df.C._values?
There was a problem hiding this comment.
Looks like we have a problem here... I was revising the PR and tweaked the test to check that the copy is actually shallow, but the test fails. Seems that you can successfully shallow copy a series in pandas, but the dataframe shallow copy does not work...
There was a problem hiding this comment.
Hrm, indeed, but it does still seem to be fast.
In [1]: import pandas as pd, numpy as np
In [2]: df = pd.DataFrame({"x": np.arange(10000000)})
In [3]: %timeit df.copy()
20.3 ms ± 1.22 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [4]: %timeit df.copy(deep=False)
39.5 µs ± 2.38 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [6]: df2 = df.copy(deep=False)
In [7]: df2.x._values is df.x._values
Out[7]: FalseI suspect that the fact that the output of the last line is False has more to do with my not understanding how the ._values accessor works than with copying.
There was a problem hiding this comment.
In [31]: df2.x._values.base is df.x.values.base
Out[31]: TrueI'd recommend np.may_share_memory
In [32]: np.may_share_memory(df2.x.values, df.x.values)
Out[32]: TrueIf you're curious, I think that both df.x.values and df2.x.values are views on the original ndarray.
There was a problem hiding this comment.
I suspect that the fact that the output of the last line is False has more to do with my not understanding how the ._values accessor works than with copying.
@TomAugspurger - Do you know the correct way to test this? The pandas docs show an example for series (which works fine for me), but not for dataframes.
There was a problem hiding this comment.
Hopefully answered in #5675 (comment), but LMK if not.
There was a problem hiding this comment.
Regardless, I'm also okay with removing the test if we know the change isn't slowing pandas-based code down, I know it is significantly boosting dask_cudf performance :)
There was a problem hiding this comment.
Thanks for your help/advice @TomAugspurger!
I have still been struggling with this for longer than I'd like to admit :/ . It seems that the drop operation is creating a copy in pandas (in certain cases):
In [1]: import pandas as pd; import numpy as np
...: df = pd.DataFrame(np.random.randn(10, 3), columns=list("ABC"))
...: df2 = df.copy(deep=False)
In [2]: np.may_share_memory(df2.C.values.base, df.C.values)
Out[2]: True
In [3]: df = pd.DataFrame(np.random.randn(10, 3), columns=list("ABC"))
...: df2 = df.copy(deep=False)
...: del df2["A"]
...: np.may_share_memory(df2.C.values.base, df.C.values)
Out[3]: FalseThe really wierd part is that there is NOT a copy made if you explicitly check that the memory is shared before the drop operation:
In [4]: df = pd.DataFrame(np.random.randn(10, 3), columns=list("ABC"))
...: df2 = df.copy(deep=False)
...: np.may_share_memory(df2.C.values.base, df.C.values)
Out[4]: True
In [5]: del df2["A"]
...: np.may_share_memory(df2.C.values.base, df.C.values)
Out[5]: TrueDoes this behavior make any sense to you?
There was a problem hiding this comment.
No, that doesn't make sense to me, sorry :)
FWIW, I think at this point we can give up on this test assertion, if it's proving to be too much of a hassle.
|
Sorry, I left out an important negative. Please don't handle them tonight. My bad ... |
|
Doh, I didn't see this PR until now. |
|
I'm confused by the recent change. I don't think that users should see a Instead I think that everything was good before, but that you just needed to pass through the errors= keyword. |
I agree - This was a possible change to fix the failing test ( |
|
The intention of that test is to ensure that the |
|
Thanks for yout help here @mrocklin and @TomAugspurger - Are there any remaining concerns here? |
|
Awesome. This looks good to me. Merging this in. Thanks @rjzamora for your patience here. |
|
Great work @rjzamora, not every day we get a 2x speedup :) |
|
I don't know. It seems to be happening more regularly recently :)
…On Fri, Dec 6, 2019 at 12:49 AM Mads R. B. Kristensen < ***@***.***> wrote:
Great work @rjzamora <https://github.com/rjzamora>, not every day we get
a 2x speedup :)
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#5675?email_source=notifications&email_token=AACKZTBSR74AOJDDMBMS4ODQXIGYFA5CNFSM4JVUEYOKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEGDN4TI#issuecomment-562486861>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACKZTFEB237PPZNLIPNT73QXIGYFANCNFSM4JVUEYOA>
.
|
Closes #5664
Following discussion in #5669, this PR modifies dd.DataFrame.drop to effectively perform the drop operation "in place". This change gives the same performance boost as thoses quoted in the referenced PR, but does not suffer from the same graph-dependency problem.
black dask/flake8 dask