-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add end and end_day options for origin from resample #37805
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
Closed
Changes from 20 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
39f1e8f
ENH: Add 'end' option in resample's origin
GYHHAHA cd5aa64
Update resample.py
GYHHAHA 0184b1d
Update resample.py
GYHHAHA ff35b6f
Update test_resample_api.py
GYHHAHA 8c4549e
Update resample.py
GYHHAHA b835d1a
Update test_resample_api.py
GYHHAHA bf15c67
Update test_resample_api.py
GYHHAHA e4b01d8
Update test_datetime_index.py
GYHHAHA d096ccd
add backward para and end_day option
GYHHAHA 222ef8d
add doc-string
GYHHAHA 90c9c5f
add test cases
GYHHAHA eae898c
fix format
GYHHAHA 2ee1000
Update test_resample_api.py
GYHHAHA 3442e00
Update test_resample_api.py
GYHHAHA a33acac
Update test_resample_api.py
GYHHAHA 7c54839
Update test_resample_api.py
GYHHAHA a4e0a39
flake8 fix
GYHHAHA 0e2e390
break lines
GYHHAHA 9f4844a
Update resample.py
GYHHAHA 5b7f396
fix docstring
GYHHAHA 115c92a
split tests
GYHHAHA 7d8d67a
Update generic.py
GYHHAHA 77fc4a3
doc added & tests fix
GYHHAHA 0cff41e
Merge branch 'master' into master
GYHHAHA b492293
fix doc
GYHHAHA 561096c
Merge remote-tracking branch 'upstream/master'
GYHHAHA 76a015a
Revert "Merge remote-tracking branch 'upstream/master'"
GYHHAHA a0262ab
Revert "fix doc"
GYHHAHA b990c5f
Revert "Merge branch 'master' into master"
GYHHAHA 8e8c1e6
Revert "doc added & tests fix"
GYHHAHA cc9f2e0
Revert "Update generic.py"
GYHHAHA 629773a
Revert "split tests"
GYHHAHA c79155b
Revert "fix docstring"
GYHHAHA f46c924
Revert "Update resample.py"
GYHHAHA af99a33
Revert "break lines"
GYHHAHA d7db83b
Revert "flake8 fix"
GYHHAHA 69183f6
Revert "Update test_resample_api.py"
GYHHAHA 5b9afee
Revert "Update test_resample_api.py"
GYHHAHA 216bff3
Revert "Update test_resample_api.py"
GYHHAHA 5409a75
Revert "Update test_resample_api.py"
GYHHAHA 90ddc36
Revert "fix format"
GYHHAHA 7b3cffb
Revert "add test cases"
GYHHAHA 2d51a8a
Revert "add doc-string"
GYHHAHA c24d8f9
Revert "add backward para and end_day option"
GYHHAHA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1366,6 +1366,7 @@ def __init__( | |
convention: Optional[str] = None, | ||
base: Optional[int] = None, | ||
origin: Union[str, TimestampConvertibleTypes] = "start_day", | ||
backward: Optional[bool] = None, | ||
offset: Optional[TimedeltaConvertibleTypes] = None, | ||
**kwargs, | ||
): | ||
|
@@ -1389,9 +1390,15 @@ def __init__( | |
label = "right" | ||
else: | ||
if closed is None: | ||
closed = "left" | ||
if origin in ["end", "end_day"] or backward: | ||
closed = "right" | ||
else: | ||
closed = "left" | ||
if label is None: | ||
label = "left" | ||
if origin in ["end", "end_day"] or backward: | ||
label = "right" | ||
else: | ||
label = "left" | ||
|
||
self.closed = closed | ||
self.label = label | ||
|
@@ -1404,17 +1411,37 @@ def __init__( | |
self.fill_method = fill_method | ||
self.limit = limit | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you edit the doc-string and add a versionupdate 1.2 tag (mention that end is added). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
if origin in ("epoch", "start", "start_day"): | ||
if origin in ("epoch", "start", "start_day", "end", "end_day"): | ||
self.origin = origin | ||
else: | ||
try: | ||
self.origin = Timestamp(origin) | ||
except Exception as e: | ||
raise ValueError( | ||
"'origin' should be equal to 'epoch', 'start', 'start_day' or " | ||
f"should be a Timestamp convertible type. Got '{origin}' instead." | ||
"'origin' should be equal to 'epoch', 'start', 'start_day', " | ||
"'end', 'end_day' or should be a Timestamp convertible " | ||
f"type. Got '{origin}' instead." | ||
) from e | ||
|
||
if backward is None: | ||
if self.origin in ("end", "end_day"): | ||
self.backward = True | ||
else: | ||
self.backward = False | ||
elif backward: | ||
if origin in ("start", "start_day"): | ||
raise ValueError( | ||
"`start` or `start_day` origin isn't allowed when " | ||
"`backward` is True" | ||
) | ||
self.backward = backward | ||
else: | ||
if origin in ("end", "end_day"): | ||
raise ValueError( | ||
"`end` or `end_day` origin isn't allowed when `backward` is False" | ||
) | ||
self.backward = backward | ||
|
||
try: | ||
self.offset = Timedelta(offset) if offset is not None else None | ||
except Exception as e: | ||
|
@@ -1502,6 +1529,7 @@ def _get_time_bins(self, ax): | |
self.freq, | ||
closed=self.closed, | ||
origin=self.origin, | ||
backward=self.backward, | ||
offset=self.offset, | ||
) | ||
# GH #12037 | ||
|
@@ -1655,6 +1683,7 @@ def _get_period_bins(self, ax: PeriodIndex): | |
self.freq, | ||
closed=self.closed, | ||
origin=self.origin, | ||
backward=self.backward, | ||
offset=self.offset, | ||
) | ||
|
||
|
@@ -1708,7 +1737,7 @@ def _take_new_index(obj, indexer, new_index, axis=0): | |
|
||
|
||
def _get_timestamp_range_edges( | ||
first, last, freq, closed="left", origin="start_day", offset=None | ||
first, last, freq, closed="left", origin="start_day", backward=False, offset=None | ||
): | ||
""" | ||
Adjust the `first` Timestamp to the preceding Timestamp that resides on | ||
|
@@ -1761,7 +1790,13 @@ def _get_timestamp_range_edges( | |
origin = origin.tz_localize(None) | ||
|
||
first, last = _adjust_dates_anchored( | ||
first, last, freq, closed=closed, origin=origin, offset=offset | ||
first, | ||
last, | ||
freq, | ||
closed=closed, | ||
origin=origin, | ||
backward=backward, | ||
offset=offset, | ||
) | ||
if isinstance(freq, Day): | ||
first = first.tz_localize(index_tz) | ||
|
@@ -1781,7 +1816,7 @@ def _get_timestamp_range_edges( | |
|
||
|
||
def _get_period_range_edges( | ||
first, last, freq, closed="left", origin="start_day", offset=None | ||
first, last, freq, closed="left", origin="start_day", backward=False, offset=None | ||
): | ||
""" | ||
Adjust the provided `first` and `last` Periods to the respective Period of | ||
|
@@ -1823,7 +1858,13 @@ def _get_period_range_edges( | |
adjust_last = freq.is_on_offset(last) | ||
|
||
first, last = _get_timestamp_range_edges( | ||
first, last, freq, closed=closed, origin=origin, offset=offset | ||
first, | ||
last, | ||
freq, | ||
closed=closed, | ||
origin=origin, | ||
backward=backward, | ||
offset=offset, | ||
) | ||
|
||
first = (first + int(adjust_first) * freq).to_period(freq) | ||
|
@@ -1832,7 +1873,7 @@ def _get_period_range_edges( | |
|
||
|
||
def _adjust_dates_anchored( | ||
first, last, freq, closed="right", origin="start_day", offset=None | ||
first, last, freq, closed="right", origin="start_day", backward=False, offset=None | ||
): | ||
# First and last offsets should be calculated from the start day to fix an | ||
# error cause by resampling across multiple days when a one day period is | ||
|
@@ -1844,8 +1885,19 @@ def _adjust_dates_anchored( | |
origin_nanos = first.normalize().value | ||
elif origin == "start": | ||
origin_nanos = first.value | ||
elif isinstance(origin, Timestamp): | ||
origin_nanos = origin.value | ||
elif isinstance(origin, Timestamp) or origin in ("end", "end_day"): | ||
if backward: | ||
if origin == "end": | ||
origin = last | ||
elif origin == "end_day": | ||
origin = last.ceil("D") | ||
sub_freq_times = (origin.value - first.value) // freq.nanos | ||
if closed == "left": | ||
sub_freq_times += 1 | ||
first = origin - sub_freq_times * freq | ||
origin_nanos = first.value | ||
else: | ||
origin_nanos = origin.value | ||
origin_nanos += offset.value if offset else 0 | ||
|
||
# GH 10117 & GH 19375. If first and last contain timezone information, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
if anything this should be a separate issue / PR discussion. I actually am -1 on adding this as yet another keyword.
pls revert everything in this PR and focus only on adding the end/end_day), though then do these become useless?
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.
Sorry for adding without discussion. I will make the reversion in this pr. Maybe the backward resampling on an arbitrary Timestamp needs a new strategy instead of new keyword. (origin=('20201129', 'B') seems to be a possible solution, but a little strange to take this tuple input?)