-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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: PeriodIndex and Period subtraction error #13071
Closed
Closed
Changes from all commits
Commits
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 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 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 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 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import pandas.tseries.frequencies as frequencies | ||
from pandas.tseries.frequencies import get_freq_code as _gfc | ||
from pandas.tseries.index import DatetimeIndex, Int64Index, Index | ||
from pandas.tseries.tdi import TimedeltaIndex | ||
from pandas.tseries.base import DatelikeOps, DatetimeIndexOpsMixin | ||
from pandas.tseries.tools import parse_time_string | ||
import pandas.tseries.offsets as offsets | ||
|
@@ -595,6 +596,32 @@ def _add_delta(self, other): | |
ordinal_delta = self._maybe_convert_timedelta(other) | ||
return self.shift(ordinal_delta) | ||
|
||
def _sub_datelike(self, other): | ||
if other is tslib.NaT: | ||
new_data = np.empty(len(self), dtype=np.int64) | ||
new_data.fill(tslib.iNaT) | ||
return TimedeltaIndex(new_data, name=self.name) | ||
return NotImplemented | ||
|
||
def _sub_period(self, other): | ||
if self.freq != other.freq: | ||
msg = _DIFFERENT_FREQ_INDEX.format(self.freqstr, other.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
|
||
if other.ordinal == tslib.iNaT: | ||
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. is this always a 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. Always |
||
new_data = np.empty(len(self)) | ||
new_data.fill(np.nan) | ||
else: | ||
asi8 = self.asi8 | ||
new_data = asi8 - other.ordinal | ||
|
||
if self.hasnans: | ||
mask = asi8 == tslib.iNaT | ||
new_data = new_data.astype(np.float64) | ||
new_data[mask] = np.nan | ||
# result must be Int64Index or Float64Index | ||
return Index(new_data, name=self.name) | ||
|
||
def shift(self, n): | ||
""" | ||
Specialized shift which produces an PeriodIndex | ||
|
This file contains 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
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.
whatif
Period(...) - pd.NaT
?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.
NaT
will be handled_sub_datelike
, currently raisesAbstractMethodError
.As in #12759,
Period
doesn't supportNaT
and use it's ownPeriod(NaT)
ATM.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.
does this issue cover the 2nd case?
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.
Yes, covered (both
Period
andPeriodIndex
). Added tests.PeriodIndexP
-pd.NaT
results inTimedeltaIndex
fulfilled withpd.NaT
.