Skip to content

BUG: fixes indexing with monotonic decreasing DTI (#19362) #20677

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 3 commits into from
Apr 20, 2018

Conversation

mapehe
Copy link
Contributor

@mapehe mapehe commented Apr 13, 2018

Seems that #19362 is caused by the fact that np.any([0]) is false. Here's a suggested solution. Also added a test.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

pls add a whatsnew note (indexing bug fix section)

@@ -342,7 +342,7 @@ def _format_with_header(self, header, **kwargs):
def __contains__(self, key):
try:
res = self.get_loc(key)
return is_scalar(res) or type(res) == slice or np.any(res)
return is_scalar(res) or type(res) == slice or np.any(res + 1)
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 change type(res) == slice to isinstance(res, slice).

I think condition could be (for the last clause)
is_list_like(res) and len(res). just need to account for an indexer, not the character of it

df = pd.DataFrame({'A': [1, 2, 3]},
index=pd.date_range('20170101',
periods=3)[::-1])
df.loc['2017-01-03']
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 assert on the result of this

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves Datetime Datetime data dtype labels Apr 14, 2018
@@ -252,3 +252,11 @@ def test_series_partial_set_period(self):
check_stacklevel=False):
result = ser.loc[keys]
tm.assert_series_equal(result, exp)

def test_monotone_DTI_indexing_bug(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

this test belongs in one of:

pandas/tests/indexes/datetimes/test_datetime.py
pandas/tests/indexes/datetimes/test_partial_slicing.py

@codecov
Copy link

codecov bot commented Apr 14, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@8756f55). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #20677   +/-   ##
=========================================
  Coverage          ?   91.81%           
=========================================
  Files             ?      153           
  Lines             ?    49279           
  Branches          ?        0           
=========================================
  Hits              ?    45247           
  Misses            ?     4032           
  Partials          ?        0
Flag Coverage Δ
#multiple 90.2% <100%> (?)
#single 41.9% <100%> (?)
Impacted Files Coverage Δ
pandas/core/indexes/datetimelike.py 96.72% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8756f55...ecff6e4. Read the comment docs.

@mapehe
Copy link
Contributor Author

mapehe commented Apr 14, 2018

Thanks for the helpful comments @jreback, I think the updated commit should address the points.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm. some small comments. ping on green.

@@ -368,3 +368,14 @@ def test_factorize_dst(self):
def test_unique(self, arr, expected):
result = arr.unique()
tm.assert_index_equal(result, expected)

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 move near the other tests?

also can you add all of the examples from the original issue (e.g. the working ones). so this is a complete example.

can you add a comment on what is being tested here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can you move near the other tests?

Sorry, didn't quite get this one, but tried to move this closer to other tests that seem a little similar.

also can you add all of the examples from the original issue (e.g. the working ones). so this is a complete example.

There wasn't a lot of stuff in the original issue, but I added what the person who posted that issue was attempting to do.

can you add a comment on what is being tested here.

Added a short comment.

@jreback jreback added this to the 0.23.0 milestone Apr 14, 2018
Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

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

Small style comment. @mapehe can you update fixing that & @jreback's suggestions (and fix the merge conflict). Doing a release this week.

@@ -342,7 +342,8 @@ def _format_with_header(self, header, **kwargs):
def __contains__(self, key):
try:
res = self.get_loc(key)
return is_scalar(res) or type(res) == slice or np.any(res)
return is_scalar(res) or isinstance(res, slice) or \
Copy link
Contributor

Choose a reason for hiding this comment

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

Typically we use parenthesis to break long lines, not \.

Copy link
Contributor Author

@mapehe mapehe Apr 16, 2018

Choose a reason for hiding this comment

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

Changed that, also fixed the conflict.

@mapehe
Copy link
Contributor Author

mapehe commented Apr 16, 2018

3c4f38b was all green @jreback

@jreback jreback merged commit 3a2e9e6 into pandas-dev:master Apr 20, 2018
@jreback
Copy link
Contributor

jreback commented Apr 20, 2018

thanks @mapehe!

@mapehe
Copy link
Contributor Author

mapehe commented Apr 20, 2018

Np!

@mapehe mapehe deleted the datetime_bug branch April 20, 2018 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: indexing with monotonic decreasing DTI
3 participants