Skip to content

BUG: Inconsistent behavior of min() when NaT is present #59740

@gozwei

Description

@gozwei

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
print(f"{pd. __version__=}")

L = []

L.append({"date": "2024-01-01"})
L.append({"date": "2025-01-01"})
L.append({"date": ""})

dfA = pd.DataFrame(L)
dfA["date"] = pd.to_datetime(dfA["date"])

print("dfA:")
print(dfA)
print(f"{min(dfA['date'])=}")
print(f"{dfA['date'].min()=}")

print()
print("dfB:")
dfB = dfA.iloc[::-1]
print(dfB)
print(f"{min(dfB['date'])=}")
print(f"{dfB['date'].min()=}")

Issue Description

The result of the Python min() function depends on the order of items in the DataFrame.

Expected Behavior

min(dfB["date"]) should behave the same as dfB["date"].min()

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.2.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-101-generic
Version : #111-Ubuntu SMP Tue Mar 5 20:16:58 UTC 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : None
pip : 24.1.2
Cython : None
pytest : 8.1.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.2.0
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.9
jinja2 : 3.1.3
IPython : 8.23.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : 2024.2.0
fsspec : 2024.2.0
gcsfs : None
matplotlib : 3.9.0
numba : 0.59.0
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 15.0.1
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : 2024.2.0
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions