Skip to content

BUG: DataFrame.drop raising TypeError when passing empty DatetimeIndex/list #28114

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

Closed

Conversation

katsuya-horiuchi
Copy link

@jbrockmendel
Copy link
Member

small comments, otherwise looks good

* Make separate change
* Fix whatsnew
* Use is_list_like
@katsuya-horiuchi
Copy link
Author

Pushed a new commit fixing the above.

@simonjayhawkins simonjayhawkins added Bug DataFrame DataFrame data structure labels Aug 25, 2019
@TomAugspurger
Copy link
Contributor

Is this fixing a regression in 0.25? If not, can you move the release note to 1.0.0.rst?

@katsuya-horiuchi
Copy link
Author

The release note has been moved.

@katsuya-horiuchi
Copy link
Author

@TomAugspurger Can you check if PR can be merged?

* Handle str passed to DataFrame.drop() when dataframe has DatetimeIndex
* Add comments
* Fix whatsnew
@@ -3915,6 +3915,9 @@ def drop(

for axis, labels in axes.items():
if labels is not None:
# Check for empty Index, GH 27994
if is_list_like(labels) and not len(labels):
Copy link
Contributor

Choose a reason for hiding this comment

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

rather than do this, you can just handle the empty case in _drop_axis

Copy link
Author

Choose a reason for hiding this comment

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

Are you sure @jreback? I tried a little, but I couldn't get it work while making sure to raise KeyError when passing str/List[str] to DataFrame.drop() that has DatetimeIndex.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the suggestion is to include the not len(labels) check in NDFrame._drop_axis. And when labels is empty we just return self from _drop_axis.

Copy link
Contributor

Choose a reason for hiding this comment

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

can you try this

@@ -4720,6 +4720,10 @@ def get_indexer_non_unique(self, target):
return pself.get_indexer_non_unique(ptarget)

if self.is_all_dates:
# If `target` doesn't consist of dates, `target.asi8` will return
# None, which will raise TypeError. GH 27994
if not target.is_all_dates:
Copy link
Contributor

Choose a reason for hiding this comment

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

this likely is not necessary if you implemented as per my comment above

Copy link
Author

Choose a reason for hiding this comment

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

When passing str, for example, to dataframe with DatetimeIndex, type(self) == type(target) would return False, in which case I believe KeyError should be raised. But underlying arrays aren't empty.
If not here, I feel like Cython code needs changing though I'm not confident.

Copy link
Contributor

Choose a reason for hiding this comment

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

What exception do you get without this? Index.is_all_dates can be surprisingly expensive.

Katsuya Horiuchi added 2 commits September 4, 2019 01:28
* Move whatsnew
* Change variable names & keep consistency in tests
@WillAyd
Copy link
Member

WillAyd commented Oct 11, 2019

@katsuya-horiuchi can you merge master and address latest comments?

@@ -3915,6 +3915,9 @@ def drop(

for axis, labels in axes.items():
if labels is not None:
# Check for empty Index, GH 27994
if is_list_like(labels) and not len(labels):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you try this

@jreback
Copy link
Contributor

jreback commented Oct 16, 2019

pls merge master

@katsuya-horiuchi
Copy link
Author

Sorry guys I have been extremely busy recently. I will take another look as soon as possible.

@jreback
Copy link
Contributor

jreback commented Nov 13, 2019

can you merge master and update

@WillAyd
Copy link
Member

WillAyd commented Dec 17, 2019

Closing as I think this is stale but @katsuya-horiuchi ping if you'd like to continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug DataFrame DataFrame data structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataFrame.drop with empty DatetimeIndex behaves differently than passing empty Index
7 participants