-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: consistency of input args for boundaries (pd.date_range) #43504
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
Conversation
pandas/core/arrays/datetimelike.py
Outdated
@@ -1823,6 +1823,37 @@ def validate_periods(periods): | |||
return periods | |||
|
|||
|
|||
def validate_inclusiveness(inclusive): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe just call this validate_inclusive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couple of things. I would like to move this new routine & validate_entpoint to pandas/core/indexers/utils.py i think.
Also need to slightly refactor .between_time
(which was just merged) to use the same.
Might be best to do these as a pre-cursor PR to this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might also look at other merged prs from underlying issue that may be able to use this validator,
pandas/core/arrays/datetimelike.py
Outdated
@@ -1823,6 +1823,37 @@ def validate_periods(periods): | |||
return periods | |||
|
|||
|
|||
def validate_inclusiveness(inclusive): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couple of things. I would like to move this new routine & validate_entpoint to pandas/core/indexers/utils.py i think.
Also need to slightly refactor .between_time
(which was just merged) to use the same.
Might be best to do these as a pre-cursor PR to this one.
closed = date_range(begin, end, closed=None, freq=freq) | ||
left = date_range(begin, end, closed="left", freq=freq) | ||
right = date_range(begin, end, closed="right", freq=freq) | ||
both = date_range(begin, end, inclusive="both", freq=freq) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you parameterize this test instead, using the inclusive_endpoints_fixture
fixtures (its in pandas/tests/frameso likely need to move to
pandas/conftest.py`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea makes sense, will do that
closed = date_range(begin, end, closed=None, freq=freq) | ||
left = date_range(begin, end, closed="left", freq=freq) | ||
right = date_range(begin, end, closed="right", freq=freq) | ||
both = date_range(begin, end, inclusive="both", freq=freq) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
closed = date_range(begin, end, closed=None, freq=freq, tz="US/Eastern") | ||
left = date_range(begin, end, closed="left", freq=freq, tz="US/Eastern") | ||
right = date_range(begin, end, closed="right", freq=freq, tz="US/Eastern") | ||
both = date_range(begin, end, inclusive="both", freq=freq, tz="US/Eastern") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
|
||
@pytest.mark.parametrize("closed", ["right", "left", None]) | ||
def test_range_closed_boundary(self, closed): | ||
@pytest.mark.parametrize("inclusive", ["right", "left", "both", "neither"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
is this also closing #43394 ? e.g. a bug fix, if so this needs a separate note |
What do you mean by separate note by the way? |
go ahead and rebase this |
d84b4ae
to
ff9b329
Compare
ff9b329
to
203733e
Compare
pandas/core/arrays/datetimes.py
Outdated
# and removing would leave index empty | ||
to_remove_any = not ( | ||
(left_inclusive or right_inclusive) | ||
and len(index) == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be len(index)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mm yeah, changed that and changed the tests in test_date_range
pandas/conftest.py
Outdated
@@ -299,6 +299,11 @@ def nselect_method(request): | |||
return request.param | |||
|
|||
|
|||
@pytest.fixture(params=["both", "neither", "left", "right"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put next to this one and add a doc-string that is indicative
@pytest.fixture(params=["left", "right", "both", "neither"])
def closed(request):
"""
Fixture for trying all interval closed parameters.
"""
return request.param
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good a few comments
Argument `closed` have been deprecated to standardize boundary inputs. | ||
Use `inclusive` instead, to set each bound as closed or open. | ||
inclusive : {"both", "neither", "left", "right"}, default "both" | ||
Include boundaries; Whether to set each bound as closed or open. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
versionadded 1.4.0
left_match = begin == both_range[0] | ||
right_match = end == both_range[-1] | ||
|
||
if inclusive_endpoints_fixture == "left" and right_match: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider making this a function and share among these tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I defined the function on the top of the test_date_range.py file, does it belong elsewhere?
right_match = endtz == both_range[-1] | ||
|
||
if inclusive_endpoints_fixture == "left" and right_match: | ||
expected_range = both_range[:-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same (so we don't repeat ourselves)
pandas/core/arrays/datetimes.py
Outdated
(left_inclusive or right_inclusive) | ||
and len(index) | ||
and start == index[0] | ||
and start == end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when is start != index[0]
?
when is len(index) == 0
i.e. when will len(index)
resolve as False
?
when is start == end
?
Do any of these cases imply the others?
pandas/core/arrays/datetimes.py
Outdated
) | ||
|
||
if to_remove_any: | ||
if (not left_inclusive) and len(index) and index[0] == start: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are reusing the same logic tests deriving to_remove_any
. I haven't come up with a better way but instinctively this feels a complicated logic path. Have you tried refactoring to a simpler setup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored the logic a little and I am not sure if it is any better though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgttm. @attack68 if comments
@@ -1029,6 +1038,28 @@ def date_range( | |||
DatetimeIndex(['2017-01-02', '2017-01-03', '2017-01-04'], | |||
dtype='datetime64[ns]', freq='D') | |||
""" | |||
if inclusive is not None and not isinstance(closed, lib.NoDefault): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its possible we would want to share this (across functoions), but ok here
https://github.com/pandas-dev/pandas/pull/43504/checks?check_run_id=3612446736 couple of tests that need addressing (you can either assert the future warning or just change closed -> inclusive in the test) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm pending green tests and some grammar corrections.
pandas/core/indexes/datetimes.py
Outdated
@@ -919,6 +920,14 @@ def date_range( | |||
closed : {None, 'left', 'right'}, optional | |||
Make the interval closed with respect to the given frequency to | |||
the 'left', 'right', or both sides (None, the default). | |||
|
|||
.. deprecated:: 1.4.0 | |||
Argument `closed` have been deprecated to standardize boundary inputs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'has' not 'have'.
pandas/core/indexes/datetimes.py
Outdated
@@ -1090,6 +1122,14 @@ def bdate_range( | |||
closed : str, default None | |||
Make the interval closed with respect to the given frequency to | |||
the 'left', 'right', or both sides (None). | |||
|
|||
.. deprecated:: 1.4.0 | |||
Argument `closed` have been deprecated to standardize boundary inputs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'has'
some comments & can you merge master. ping on green. |
thanks @zyc09 |
…-dev#43504) Co-authored-by: JHM Darbyshire <[email protected]>
closes #43394
xref #40245