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: Fix PeriodIndex +/- TimedeltaIndex #23031

Merged
merged 14 commits into from
Oct 15, 2018

Conversation

jbrockmendel
Copy link
Member

@jbrockmendel jbrockmendel commented Oct 7, 2018

@pep8speaks
Copy link

pep8speaks commented Oct 7, 2018

Hello @jbrockmendel! Thanks for updating the PR.

file_to_check.py:2456:-258: W605 invalid escape sequence '('
file_to_check.py:2456:-255: W605 invalid escape sequence ')'


Comment last updated on October 09, 2018 at 23:56 Hours UTC

@@ -805,6 +805,7 @@ def assert_index_equal(left, right, exact='equiv', check_names=True,
Specify object name being compared, internally used to show appropriate
assertion message
"""
__tracebackhide__ = True
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #22643

# Explicitly catch invalid dtypes
raise TypeError("cannot add {dtype}-dtype to {cls}"
.format(dtype=other.dtype,
cls=type(self).__name__))
elif is_period_dtype(other):
# PeriodIndex + TimedeltaIndex _sometimes_ is valid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you clarify?

@jreback jreback added Timedelta Timedelta data type Period Period data type labels Oct 7, 2018
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a whatsnew note

@codecov
Copy link

codecov bot commented Oct 7, 2018

Codecov Report

Merging #23031 into master will decrease coverage by 0.05%.
The diff coverage is 85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #23031      +/-   ##
==========================================
- Coverage   92.19%   92.14%   -0.06%     
==========================================
  Files         169      170       +1     
  Lines       50911    51103     +192     
==========================================
+ Hits        46939    47087     +148     
- Misses       3972     4016      +44
Flag Coverage Δ
#multiple 90.56% <85%> (-0.06%) ⬇️
#single 42.27% <15%> (-0.04%) ⬇️
Impacted Files Coverage Δ
pandas/core/arrays/datetimes.py 96.94% <ø> (+0.02%) ⬆️
pandas/util/testing.py 86.45% <100%> (+0.26%) ⬆️
pandas/core/arrays/datetimelike.py 94.89% <60%> (-0.67%) ⬇️
pandas/core/arrays/period.py 95.56% <87.87%> (+2.71%) ⬆️
pandas/core/ops.py 94.39% <0%> (-2.8%) ⬇️
pandas/compat/numpy/function.py 86.66% <0%> (-1.82%) ⬇️
pandas/core/dtypes/concat.py 97.69% <0%> (-1.49%) ⬇️
pandas/core/internals/concat.py 96.83% <0%> (-1.17%) ⬇️
pandas/core/internals/managers.py 95.77% <0%> (-0.9%) ⬇️
pandas/core/internals/blocks.py 92.91% <0%> (-0.58%) ⬇️
... and 37 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f6da1f1...ed50b9f. Read the comment docs.

@jbrockmendel
Copy link
Member Author

Updated with a whatsnew and more tests. Doing this right required more a refactor than I expected, but in the end it touches only arithmetic code and doesn't fiddle with indexing code (like the previous attempt did)


def _add_delta(self, other):
ordinal_delta = self._maybe_convert_timedelta(other)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting the _maybe_convert_timedelta call out of the arithmetic ops makes everything simpler

new_values = self._add_delta_tdi(other)
return self._shallow_copy(new_values)
else: # pragma: no cover
raise TypeError(type(other).__name__)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this and some of the other methods in the datetime/timedelta/period array mixins, I think some de-duplication can be done on the next pass

@jbrockmendel
Copy link
Member Author

gentle ping

@@ -454,6 +480,38 @@ def _maybe_convert_timedelta(self, other):
raise IncompatibleFrequency(msg.format(cls=type(self).__name__,
freqstr=self.freqstr))

def _check_timedeltalike_freq_compat(self, other):
assert isinstance(self.freq, Tick) # checked by calling function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u add a doc string here

if isinstance(other, (timedelta, np.timedelta64, Tick)):
nanos = delta_to_nanoseconds(other)

elif isinstance(other, np.ndarray):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u comments these cases a bit

@jreback jreback added this to the 0.24.0 milestone Oct 14, 2018
@jreback
Copy link
Contributor

jreback commented Oct 14, 2018

lgtm. just add a couple of comments. merge when green

@jbrockmendel
Copy link
Member Author

Ping

@jbrockmendel jbrockmendel merged commit cf11f71 into pandas-dev:master Oct 15, 2018
@jbrockmendel jbrockmendel deleted the pi_add branch October 15, 2018 00:49
tm9k1 pushed a commit to tm9k1/pandas that referenced this pull request Nov 19, 2018
* Fix PeriodIndex +- TimedeltaIndex

* better comment

* typo fixup

* Allow _add_delta_tdi to handle ndarray[timedelta64] gracefully

* Implement specialized versions of _add_delta, _add_delta_tdi

* Add tests for adding offset scalar

* add tests for subtracting offset scalar

* remove superfluous comment

* whatsnew

* Revert incorrect docstring change

* docstring

* comment

* comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Period Period data type Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: PeriodIndex + offset incorrect
3 participants