BUG: Stable sort on DatetimeIndex is not stable · Issue #57151 · pandas-dev/pandas (original) (raw)
Pandas version checks
- I have checked that this issue has not already been reported.
- 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
import pandas as pd
df = pd.DataFrame( data=[ (pd.Timestamp("2024-01-30 13:00:00"), 13.0), (pd.Timestamp("2024-01-30 13:00:00"), 13.1), (pd.Timestamp("2024-01-30 12:00:00"), 12.0), (pd.Timestamp("2024-01-30 12:00:00"), 12.1), ], columns=["dt", "value"], ).set_index(["dt"])
df.sort_index(level="dt", kind="stable")
value
dt
2024-01-30 12:00:00 12.1
2024-01-30 12:00:00 12.0
2024-01-30 13:00:00 13.1
2024-01-30 13:00:00 13.0
Issue Description
Sorting on a timestamp index is not stable, even when using kind="stable"
.
Expected Behavior
Sorting to be stable, as with pandas=2.1.4
:
import pandas as pd
df = pd.DataFrame( data=[ (pd.Timestamp("2024-01-30 13:00:00"), 13.0), (pd.Timestamp("2024-01-30 13:00:00"), 13.1), (pd.Timestamp("2024-01-30 12:00:00"), 12.0), (pd.Timestamp("2024-01-30 12:00:00"), 12.1), ], columns=["dt", "value"], ).set_index(["dt"])
df.sort_index(level="dt", kind="stable")
value
dt
2024-01-30 12:00:00 12.0
2024-01-30 12:00:00 12.1
2024-01-30 13:00:00 13.0
2024-01-30 13:00:00 13.1
Installed Versions
INSTALLED VERSIONS
commit : f538741
python : 3.11.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.133.1-microsoft-standard-WSL2
Version : #1 SMP Thu Oct 5 21:02:42 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.4
dateutil : 2.8.2
setuptools : 69.0.3
pip : 23.3.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 14.0.2
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None