Skip to content

Support keyword API for Dataset.drop #3128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 18, 2019

Conversation

gwgundersen
Copy link
Contributor

  • Closes Keyword argument support for drop() #2910.
  • In tests/test_dataset, added test_drop_labels_by_keyword
  • Added a utility function is_list_like.
  • Documented in whats-new.rst and confirmed API docs built correctly.

@codecov
Copy link

codecov bot commented Jul 14, 2019

Codecov Report

Merging #3128 into master will increase coverage by 0.31%.
The diff coverage is 92.85%.

@@            Coverage Diff             @@
##           master    #3128      +/-   ##
==========================================
+ Coverage   95.66%   95.98%   +0.31%     
==========================================
  Files          64       63       -1     
  Lines       13117    12788     -329     
==========================================
- Hits        12548    12274     -274     
+ Misses        569      514      -55

@shoyer
Copy link
Member

shoyer commented Jul 14, 2019

Awesome, thanks for working on this @gwgundersen !

In the long term, we might want to deprecate the current function signature, but I'm happy to leave that for later.

@shoyer shoyer mentioned this pull request Aug 4, 2019
@max-sixty
Copy link
Collaborator

@gwgundersen I see you trying to merge re the recent black changes. Here are the instructions for making this (relatively) seamless: https://github.com/pydata/xarray/blob/d3f1ced1aa84cb0a0f3e47e7ee67492e203fcce5/doc/contributing.rst#code-formatting

@gwgundersen
Copy link
Contributor Author

@max-sixty, thanks! It's useful to be able to do in advance.

After merging from master, I get this error:

flake8
./xarray/core/dataset.py:3466:5: F811 redefinition of unused 'drop' from line 3460

I assume this is due to #3177 cc @crusaderky. Is there a way to avoid this?

@max-sixty
Copy link
Collaborator

@gwgundersen Could you force-push your latest code?

If your current latest code is pushed, you may have to git reset --hard and start again - unfortunately it looks like your PR is proposing +24,439 −17,647 changes

@gwgundersen gwgundersen force-pushed the drop-keyword-support branch from 97271bd to 1de442e Compare August 16, 2019 06:46
@pep8speaks
Copy link

pep8speaks commented Aug 16, 2019

Hello @gwgundersen! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-08-18 07:25:58 UTC

@gwgundersen
Copy link
Contributor Author

Sorry about that. I'm not sure what happened. I've reverted branch and cherry picked my changes. The issue with drop being a duplicated name still stands.

Copy link
Collaborator

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

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

Added a noqa suggestion so flake doesn't complain

@max-sixty
Copy link
Collaborator

Thanks @gwgundersen - good idea re the cherry pick for more discrete changes.

Still getting a test failure though, check it out and lmk if you need any help

@gwgundersen
Copy link
Contributor Author

Now the docs don't build. When I try to build locally, I get a vague error:

  File "/Users/gwg/miniconda3/envs/xarray-docs/lib/python3.7/site-packages/IPython/sphinxext/ipython_directive.py", line 583, in process_input
    raise RuntimeError('Non Expected warning in `{}` line {}'.format(filename, lineno))
RuntimeError: Non Expected warning in `/Users/gwg/projects/xarray/doc/indexing.rst` line 241

Do you see anything wrong with this?

The :py:meth:`~xarray.Dataset.drop` method returns a new object with the listed
index labels along a dimension dropped:

.. ipython:: python

    ds.drop(['IN', 'IL'], dim='space')
<-- line 241
``drop`` is both a ``Dataset`` and ``DataArray`` method.

Use :py:meth:`~xarray.Dataset.drop_dims` to drop a full dimension from a Dataset.
Any variables with these dimensions are also dropped:

@dcherian
Copy link
Contributor

It looks like you've added a warming for this case. So you'll need to specify :okwarning: .

@max-sixty
Copy link
Collaborator

I see the docs timing out after 60 min. I also see that message, but is that causing the failure?

I restarted the docs build to see whether that might help

@gwgundersen
Copy link
Contributor Author

Sorry for the back-and-forth. CI tests and checks are all passing, and this is ready for another review.

Copy link
Collaborator

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

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

Super! Will merge tomorrow unless anyone has comments?

@max-sixty
Copy link
Collaborator

@gwgundersen could we add a whatsnew too?

@gwgundersen
Copy link
Contributor Author

@max-sixty, done.

@max-sixty max-sixty merged commit d578bfe into pydata:master Aug 18, 2019
@max-sixty
Copy link
Collaborator

Great @gwgundersen !

@gwgundersen gwgundersen deleted the drop-keyword-support branch August 18, 2019 17:51
Copy link
Member

@shoyer shoyer left a comment

Choose a reason for hiding this comment

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

@gwgundersen thanks for this PR!

I have a few minor points that would be good to address before this goes out in an official release.

"""
if errors not in ["raise", "ignore"]:
raise ValueError('errors must be either "raise" or "ignore"')

if dim is None:
labels_are_coords = isinstance(labels, DataArrayCoordinates)
if labels_kwargs or (utils.is_dict_like(labels) and not labels_are_coords):
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it would be more predictable to special case the dict type rather than not labels_are_coords, which is just one of many internal xarray mapping types, e.g.,

Suggested change
if labels_kwargs or (utils.is_dict_like(labels) and not labels_are_coords):
if labels_kwargs or isinstance(labels, dict):

More generally, I get that writing array.drop(array.coords) is convenient, but in the long term it seems like a bad idea to treat different mapping types differently. This suggests that such uses should be deprecated. Either array.drop_vars(array.coords) or array.drop(list(array.coords)) would be less ambiguous.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I thought needing the check for labels_are_coords was odd, but I didn't expect passing in coordinates to take a separate code path. Special-casing the dict type makes sense. And the API that is being deprecated is treating DataArrayCoordinates as non-dict-like, right?

Copy link
Member

Choose a reason for hiding this comment

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

And the API that is being deprecated is treating DataArrayCoordinates as non-dict-like, right?

That's right, we would rather always use this new rule for dict-like inputs.

@@ -236,6 +236,7 @@ The :py:meth:`~xarray.Dataset.drop` method returns a new object with the listed
index labels along a dimension dropped:

.. ipython:: python
:okwarning:

ds.drop(['IN', 'IL'], dim='space')
Copy link
Member

Choose a reason for hiding this comment

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

Since this is deprecated, we should fix the docs to use ds.drop(space=['IN', 'IL'] as an example of the preferred syntax going forward.

@max-sixty
Copy link
Collaborator

@shoyer thanks for the follow-up comments. I probably should have spotted those myself.
But given there'll be points I miss, lmk if I should be waiting longer / raising specific points before hitting the big green button, or these post-merge reviews are OK. I can ensure they're complete before a release.

@shoyer
Copy link
Member

shoyer commented Aug 18, 2019

Post merge reviews are totally fine. It is not a big deal to revert a merge if needed.

@gwgundersen
Copy link
Contributor Author

Happy to make these changes, but what's the protocol here? Seems like reverting the merge and letting me amend is cleaner than me creating a new PR.

shoyer added a commit that referenced this pull request Aug 18, 2019
@shoyer
Copy link
Member

shoyer commented Aug 18, 2019

My (limited) understanding of how github handles reverting is that you'll still need to make a new pull request to apply your edits either way. There is no more going to back to amend after PR is merges. The only difference is what the state of "master" looks like when you make your new pull request (whether it includes your partial changes or not).

So I don't think it particularly matters if you revert first (as long as the forward fix happens relatively soon), but if you'd prefer to do it that way that's totally fine -- let me know and I will do it.

@max-sixty
Copy link
Collaborator

I think the easiest way forward is to do a follow-up PR, no need to revert.

Reverting would be helpful if we wanted to undo these changes - in this case we have some v marginal additional changes.

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.

Keyword argument support for drop()
5 participants