Skip to content

BUG: Fix first_last_valid_index, now preserves the frequency. #20569

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

Conversation

asdf8601
Copy link
Contributor

@asdf8601 asdf8601 commented Mar 31, 2018

Checklist

I'm not sure if I should add a test somewhere else. Notice that I've added a new function _find_first_valid.

@@ -8763,6 +8763,51 @@ def transform(self, func, *args, **kwargs):
scalar : type of index
"""

def _find_first_valid(self, reversed=False):
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 rename to _find_valid_index and make the param (required), and call it how='first'|'last'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool, looks better.

i = is_valid.idxmax()
if not is_valid[i]:
return None
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

don't need the elses (just return)

Copy link
Contributor Author

@asdf8601 asdf8601 Mar 31, 2018

Choose a reason for hiding this comment

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

hmmmm, I'm not sure, I was thinking about the case when there is no valid values (all nans), In that case this function returns the first index when should be None.

return None
else:
return i
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

don't need the else

if not is_valid.iat[len(self) - i - 1]:
return None
else:
return self.index[len(self) - i - 1]
Copy link
Contributor

Choose a reason for hiding this comment

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

same

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves Frequency DateOffsets labels Mar 31, 2018
@jreback
Copy link
Contributor

jreback commented Mar 31, 2018

can you add a whatsnew note as well

@asdf8601
Copy link
Contributor Author

asdf8601 commented Mar 31, 2018

@jreback of course, also I forgot asking you where I should add the whatsnew note.

@codecov
Copy link

codecov bot commented Mar 31, 2018

Codecov Report

Merging #20569 into master will decrease coverage by <.01%.
The diff coverage is 95.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #20569      +/-   ##
==========================================
- Coverage   91.84%   91.84%   -0.01%     
==========================================
  Files         152      152              
  Lines       49270    49262       -8     
==========================================
- Hits        45252    45243       -9     
- Misses       4018     4019       +1
Flag Coverage Δ
#multiple 90.22% <95.23%> (-0.01%) ⬇️
#single 41.9% <14.28%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/series.py 93.78% <ø> (-0.1%) ⬇️
pandas/core/frame.py 97.17% <ø> (-0.02%) ⬇️
pandas/core/generic.py 95.84% <95.23%> (-0.01%) ⬇️

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 d2ab407...48611fa. Read the comment docs.

@asdf8601
Copy link
Contributor Author

asdf8601 commented Apr 1, 2018

@jreback I added whatsnew note under "Bug fixes: Indexing" section.

@jreback jreback added this to the 0.23.0 milestone Apr 1, 2018
@jreback jreback merged commit 5d1f5ab into pandas-dev:master Apr 1, 2018
@jreback
Copy link
Contributor

jreback commented Apr 1, 2018

thanks @mmngreco

@asdf8601 asdf8601 deleted the feature/index branch April 18, 2018 07:20
kornilova203 pushed a commit to kornilova203/pandas that referenced this pull request Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Frequency DateOffsets Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected behaviour in pandas.DataFrame.first_valid_index (and last...)
2 participants