Skip to content

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

Closed
@alejsm

Description

@alejsm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions