Skip to content
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

BUG: date_range inclusive parameter behavior doesn't match interval notation when start == end #55293

Open
3 tasks done
mixilchenko opened this issue Sep 26, 2023 · 7 comments
Assignees
Labels
Bug Datetime Datetime data dtype Needs Triage Issue that has not been reviewed by a pandas team member Regression Functionality that used to work in a prior pandas version

Comments

@mixilchenko
Copy link

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

pd.date_range('20230926', '20230926', inclusive='right')
# Result: DatetimeIndex(['2023-09-26'], dtype='datetime64[ns]', freq='D')
# Expected: DatetimeIndex([], dtype='datetime64[ns]', freq='D')

Issue Description

Date ranges of (date, date] and [date, date) equal to [date, date] but not (date, date) which is unintuitive.
Issue #46331 looks related to this

Expected Behavior

import pandas as pd

date = pd.Timestamp('20230926')
r = pd.date_range(date, date, inclusive='right')
l = pd.date_range(date, date, inclusive='left')
n = pd.date_range(date, date, inclusive='neither')
pd.testing.assert_index_equal(r, n)
pd.testing.assert_index_equal(l, n)

Installed Versions

INSTALLED VERSIONS

commit : 89bd569
python : 3.9.10.final.0
python-bits : 64
OS : Darwin
OS-release : 22.2.0
Version : Darwin Kernel Version 22.2.0: Fri Nov 11 02:03:51 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : ru_RU.UTF-8
LOCALE : ru_RU.UTF-8

pandas : 2.2.0dev0+283.g89bd5695df
numpy : 1.26.0
pytz : 2021.3
dateutil : 2.8.2
setuptools : 58.1.0
pip : 21.2.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.1.1
pandas_datareader : None
bs4 : None
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.0
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@mixilchenko mixilchenko added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 26, 2023
@mixilchenko
Copy link
Author

#40245 is also related

@PiotrekB416
Copy link

take

@DavidToneian
Copy link
Contributor

Date ranges of (date, date] and [date, date) equal to [date, date] but not (date, date) which is unintuitive.
Issue https://github.com//issues/46331 looks related to this

I'm not sure whether parts of the pandas API would lead one to believe this, but from a mathematical point of view, I find this very intuitive:
(date, date], [date, date), and [date, date] are all intervals containing exactly one value: date.
(date, date), however, is the empty set (interval of all values that are larger than date, but smaller than date).

@PiotrekB416
Copy link

From a purely mathematical point of view (date, date] means that the interval contains every x that matches the inequality $date < x \leq date$
and since no x can match that condition the interval should be an empty set

@DavidToneian
Copy link
Contributor

Thanks for the correction, you're right of course. :)

@mickjermsurawong-stripe
Copy link

mickjermsurawong-stripe commented Jan 12, 2024

Contractually, this is a regression from earlier version
This is a regression from earlier version of pandas at least in version 1.2.4

import pandas as pd
print(pd.__version__)
date = pd.Timestamp('20230926')
r = pd.date_range(date, date, closed='left')
print(r)
# 1.2.4
# DatetimeIndex([], dtype='datetime64[ns]', freq='D')

Same problem described above in version at least in 1.5.3

import pandas as pd
print(pd.__version__)
date = pd.Timestamp('20230926')
r = pd.date_range(date, date, closed='left')
print(r)
# 1.5.3
# DatetimeIndex(['2023-09-26'], dtype='datetime64[ns]', freq='D')

From the new version, code-wise it is surprising that we are only handling exclusivity for the two ends:

if start == end:
if not left_inclusive and not right_inclusive:
i8values = i8values[1:-1]

The i8values will return 1 element since it satisfies the earlier function of both two-ended inclusivity
* This method is faster for generating weekdays than dateutil.rrule
* At least two of (start, end, periods) must be specified.
* If both start and end are specified, the returned dates will
satisfy start <= date <= end.

I can't think of the scenario when the timestamp is equal and we will have more than one element.. I think we should be handling exclusivity independently with or instead?

 if start == end: 
     if not left_inclusive or not right_inclusive:
         i8values = []

@simonjayhawkins simonjayhawkins added Datetime Datetime data dtype Regression Functionality that used to work in a prior pandas version labels Feb 8, 2024
@tscheburaschka
Copy link

All this started with the poorly informed change requested in #43394
Whether this has any relation to the renaming of kwarg closed to inclusive is beyond my insights.
I would strongly support an API coherent with the mathematical definition of intervals.
Quote from https://en.wikipedia.org/wiki/Interval_(mathematics)#Including_or_excluding_endpoints:

Each interval (a, a), [a, a), and (a, a] represents the empty set, whereas [a, a] denotes the singleton set {a}.

Additionally, the exact significance of the periods parameter when combined with inclusive should be clarified. From reading #46331 it seems to me, that the length of the result depends on the value of inclusive which is very counterintuitive to me.

How all this could be done without breaking a lot of peoples code (again) in non-obvious ways is not clear to me. Perhaps it requires a resuscitation of the closed kwarg.
I could help with discussing the pros and cons of various approaches but I don't have time for actual implementations (especially since I have not yet contributed to pandas and there seems to be a substantial initial investment required).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Needs Triage Issue that has not been reviewed by a pandas team member Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants