Skip to content

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

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
2 of 3 tasks
gozwei opened this issue Sep 6, 2024 · 3 comments
Closed
2 of 3 tasks

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

gozwei opened this issue Sep 6, 2024 · 3 comments
Labels
Bug Closing Candidate May be closeable, needs more eyeballs

Comments

@gozwei
Copy link

gozwei commented Sep 6, 2024

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

@gozwei gozwei added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 6, 2024
@gdowdy3
Copy link

gdowdy3 commented Sep 6, 2024

From my perspective, the most unintuitive thing about this example is that min(dfA['date']) does not return the same result as min(dfB['date']).

@mroeschke
Copy link
Member

Thanks for the report but I think this is the expected behavior.

NaT is like nan where all comparison operators always evaluate to False, and since min just iterates over each value and performs a comparison it's equivalent to this

In [3]: min([float("nan"), 1.0, 2.0])
Out[3]: nan

In [4]: min([1.0, 2.0, float("nan")])
Out[4]: 1.0

Additionally Series.min has a skipna=True default value so this method ignores NaT by default

@mroeschke mroeschke added Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 6, 2024
@gozwei
Copy link
Author

gozwei commented Sep 6, 2024

Ok, I see your argument. This is an example without pandas:


a = [1,2,3,np.nan]
b = [np.nan,1,2,3]
print(min(a))
print(min(b))

While this does not seem like pandas issue, I find it counterintuitive.

I'm ok to close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs
Projects
None yet
Development

No branches or pull requests

3 participants