Skip to content

Modify dd.DataFrame.drop to use shallow copy - #5675

Merged
mrocklin merged 9 commits into
dask:masterfrom
rjzamora:drop-in-place
Dec 5, 2019
Merged

mrocklin merged 9 commits into
dask:masterfrom
rjzamora:drop-in-place

Conversation

@rjzamora

@rjzamora rjzamora commented Dec 5, 2019

Copy link
Copy Markdown
Member

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.

  • Tests added / passed
  • Passes black dask / flake8 dask

@mrocklin mrocklin left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rjzamora !

I've added some comments below (although please don't handle them tonight).

Comment thread dask/dataframe/core.py Resolved
Comment thread dask/dataframe/shuffle.py Resolved
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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@rjzamora rjzamora Dec 5, 2019 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]: False

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 TomAugspurger Dec 5, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In [31]: df2.x._values.base is df.x.values.base
Out[31]: True

I'd recommend np.may_share_memory

In [32]: np.may_share_memory(df2.x.values, df.x.values)
Out[32]: True

If you're curious, I think that both df.x.values and df2.x.values are views on the original ndarray.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully answered in #5675 (comment), but LMK if not.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]: False

The 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]: True

Does this behavior make any sense to you?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread dask/dataframe/utils.py Resolved
@mrocklin

mrocklin commented Dec 5, 2019

Copy link
Copy Markdown
Member

Sorry, I left out an important negative.

Please don't handle them tonight.

My bad ...

@madsbk

madsbk commented Dec 5, 2019

Copy link
Copy Markdown
Contributor

Doh, I didn't see this PR until now.
I just implemented something similar: #5678

Comment thread dask/dataframe/utils.py Resolved
Comment thread dask/dataframe/core.py Resolved
@mrocklin mrocklin mentioned this pull request Dec 5, 2019
Comment thread dask/dataframe/core.py Resolved
@mrocklin

mrocklin commented Dec 5, 2019

Copy link
Copy Markdown
Member

I'm confused by the recent change. I don't think that users should see a deep= keyword. This isn't part of the Pandas API and isn't something that I think is necessary.

Instead I think that everything was good before, but that you just needed to pass through the errors= keyword.

@rjzamora

rjzamora commented Dec 5, 2019

Copy link
Copy Markdown
Member Author

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 (test_simple_map_partitions), where we are explicitly looking for M.drop in the task graph. Should I remove this test?

@mrocklin

mrocklin commented Dec 5, 2019

Copy link
Copy Markdown
Member

The intention of that test is to ensure that the enforce_metadata= keyword results in simple graphs. We used drop as an example, but if drop is getting more complex then maybe we should redirect this test to some other function that is supposed to be simple. Perhaps .clip?

@rjzamora

rjzamora commented Dec 5, 2019

Copy link
Copy Markdown
Member Author

Thanks for yout help here @mrocklin and @TomAugspurger - Are there any remaining concerns here?

@mrocklin

mrocklin commented Dec 5, 2019

Copy link
Copy Markdown
Member

Awesome. This looks good to me. Merging this in. Thanks @rjzamora for your patience here.

@mrocklin
mrocklin merged commit f8197cd into dask:master Dec 5, 2019
@madsbk

madsbk commented Dec 6, 2019

Copy link
Copy Markdown
Contributor

Great work @rjzamora, not every day we get a 2x speedup :)

@mrocklin

mrocklin commented Dec 6, 2019 via email

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEA] In-place DataFrame.drops()

4 participants