Skip to content

DOC: DataFrame.last() needs the index to be sorted to deliver the expected results #38000

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
alejsm opened this issue Nov 22, 2020 · 5 comments · Fixed by #38536
Closed

DOC: DataFrame.last() needs the index to be sorted to deliver the expected results #38000

alejsm opened this issue Nov 22, 2020 · 5 comments · Fixed by #38536
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@alejsm
Copy link

alejsm commented Nov 22, 2020

Location of the documentation

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.last.html

Documentation problem

For the method to work it is needed that the DatetimeIndex of the dataframe is sorted, otherwise it will notwork.
Example:

import pandas as pd

df = pd.DataFrame({
    'Date': ['2020-01-03', '2020-01-02', '2020-01-05', '2020-01-01', '2020-01-04'],
    'Value': [1, 2, 3, 4, 5]
    })

df['Date'] = pd.to_datetime(df.Date)
df.set_index('Date', inplace=True)
#df.sort_index(inplace=True) This line is needed to make it work
df.last('3D')

returns all the rows in the DataFrame

                   Value
Date	
2020-01-03	1
2020-01-02	2
2020-01-05	3
2020-01-01	4
2020-01-04	5

Suggested fix for documentation

It should be mentioned that for the last() method to work, the DatetimeIndex must be sorted, or the user may think that there is a bug in the function.

@alejsm alejsm added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 22, 2020
@phofl
Copy link
Member

phofl commented Nov 22, 2020

Hi thanks for your report. You are right, the documentation should be better here. Additionally I am wondering if we should raise when the Index is unsorted.

PS: Could you adjust your title to reflect the problem a bit better?

@alejsm alejsm changed the title DOC: DOC: DataFrame.last() needs the index to be sorted to deliver the expected results Nov 22, 2020
@aflah02
Copy link
Contributor

aflah02 commented Dec 16, 2020

Can I take up this issue and add this to the documentation? @phofl

@alejsm
Copy link
Author

alejsm commented Dec 16, 2020

Yes, sure!

@aflah02
Copy link
Contributor

aflah02 commented Dec 16, 2020

Oh thanks @alejsm

@aflah02
Copy link
Contributor

aflah02 commented Dec 17, 2020

Do I need to just add the part in the documentation or also add a error when the index is not sorted, if yes, what should be the type of the error? @phofl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
3 participants