Open ChrisDarley opened 1 year ago
Hi, thanks for your report. I think this is expected. slicing on DatetimeIndexes is only supposed to work on monotonic indexes. cc @jbrockmendel
Do they need to be monotonic increasing or any monotonic? seems like ::-1
is pretty unambiguous as to what we'd expect
Sorry, should have been more clear: monotonic increasing. We could certainly make this work, but the whole conversion into an indexer assumes monotonic increasing indexes
If it doesn't/shouldn't work, then the operation should raise, e.g. a KeyError?
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
A common way to reverse dataframes is by using df.loc[::-1]. If the dataframe has a datetime index, and you create a reversed view of the dataframe using df.loc[::-1], if you reverse that view again (i.e. df.loc[::-1].loc[::-1]), it only returns the last row of the original dataframe. This issue does not happen if you use df.iloc[::-1].iloc[::-1], nor does it happen with basic indexing such as df[::-1][::-1]. I think that this issue is specific to datetime indexes, because it did not occur when doing df.loc[::-1].loc[::-1] when I had a string index. I'm not sure that df.loc[::-1] is the preferred way to reverse a dataframe, but if double reversal works on a dataframe with a string index it should theoretically work on a a dataframe with a datetime index.
Expected Behavior
The same operation works fine on a dataframe with a string index
import pandas as pd import numpy as np a = pd.DataFrame( data=np.array([[1,2,3,4],[10,20,30,40]]).transpose(), index = ['a', 'b', 'c', 'd']) print(a.loc[::-1].loc[::-1])
Installed Versions