Skip to content

Fix+test timedelta64(nat) ops #23425

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

Merged
merged 6 commits into from
Nov 6, 2018
Merged

Conversation

jbrockmendel
Copy link
Member

Fixes issues mentioned as follow-ups to #23320

  • closes #xxxx
  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

@pep8speaks
Copy link

Hello @jbrockmendel! Thanks for submitting the PR.

@codecov
Copy link

codecov bot commented Oct 31, 2018

Codecov Report

Merging #23425 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #23425      +/-   ##
==========================================
+ Coverage   92.25%   92.25%   +<.01%     
==========================================
  Files         161      161              
  Lines       51186    51195       +9     
==========================================
+ Hits        47222    47231       +9     
  Misses       3964     3964
Flag Coverage Δ
#multiple 90.64% <100%> (ø) ⬆️
#single 42.27% <23.07%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/base.py 96.46% <ø> (ø) ⬆️
pandas/core/arrays/datetimelike.py 96.16% <100%> (+0.06%) ⬆️
pandas/core/arrays/period.py 98.09% <100%> (ø) ⬆️

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 6bf6cd2...6d3d38d. Read the comment docs.

@gfyoung gfyoung added Bug Timedelta Timedelta data type Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff labels Oct 31, 2018
result = obj - other
tm.assert_equal(result, expected)
with pytest.raises(TypeError):
other - obj
Copy link
Member

Choose a reason for hiding this comment

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

I see a lot of error message changes. Are they tested anywhere with assert_raises_regex?

Copy link
Member Author

Choose a reason for hiding this comment

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

The way these tests are parametrized, the messages are all over the place.

@gfyoung gfyoung added the Error Reporting Incorrect or improved errors from pandas label Oct 31, 2018
@@ -382,6 +383,12 @@ def _add_timedeltalike_scalar(self, other):
Add a delta of a timedeltalike
return the i8 result view
"""
if isna(other):
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't this pretty much what _nat_new did? I find this is repeating lots of code, why don't you make a method to create the null array instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

_nat_new also did casting+boxing that this doesn't. These two lines aren't repeated often enough to merit a method.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@@ -757,7 +759,12 @@ def _add_timedeltalike_scalar(self, other):
assert isinstance(self.freq, Tick) # checked by calling function
assert isinstance(other, (timedelta, np.timedelta64, Tick))

delta = self._check_timedeltalike_freq_compat(other)
if isna(other):
Copy link
Contributor

Choose a reason for hiding this comment

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

I find the need to do this pretty odd, why would't the check routine handle this? I do not like special cases

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the first thing the check function does is call delta_to_nanoseconds, which will raise if we pass np.timedelta64("NaT"). Even if we change that method to avoid calling delta_to_nanoseconds in that case, we'd need to handle a null return value after the check.

I do not like special cases

+1. As Joris mentioned in another thread, a lot of gymnastics seems to be driven by the fact that pd.NaT does double-duty as both Not-A-Time and Not-A-Timedelta.

@@ -149,6 +149,8 @@ class PeriodArray(dtl.DatetimeLikeArrayMixin, ExtensionArray):
period_array : Create a new PeriodArray
pandas.PeriodIndex : Immutable Index for period data
"""
# array priority higher than numpy scalars
__array_priority__ = 1000
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a test that needs this? I agree should set it....

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. IIRC it was timedelta64 - TimedeltaIndex

if isna(other):
# special handling for np.timedelta64("NaT"), avoid calling
# _check_timedeltalike_freq_compat as that would raise TypeError
delta = other
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 write this like

if notna(other):
   # your current commnet
    other = self.........

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure

@@ -1169,6 +1169,25 @@ def test_dti_isub_timedeltalike(self, tz_naive_fixture, two_hours):
rng -= two_hours
tm.assert_index_equal(rng, expected)

def test_dt64arr_add_sub_td64_nat(self, box, tz_naive_fixture):
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 parameterize other with pd.NaT as well here (I bet we already have a test for that, can you consolidate)

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really; pd.NaT behavior is pretty starkly different from the behavior we’re testing here.

That said, there are several rounds of de duplication and parameterization coming up

Copy link
Contributor

Choose a reason for hiding this comment

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

how is it different at all?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because this is a timedelta64 and pd.NaT is in most cases datetime-like. The "most cases" part makes it especially hairy.

@@ -785,6 +785,24 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self,
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
rng -= other

def test_parr_add_sub_td64_nat(self, box):
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

@@ -735,6 +735,24 @@ def test_td64arr_add_sub_tdi(self, box_df_broadcast_failure, names):
else:
assert result.dtypes[0] == 'timedelta64[ns]'

def test_td64arr_add_sub_td64_nat(self, box):
# GH#23320 special handling for timedelta64("NaT")
Copy link
Contributor

Choose a reason for hiding this comment

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

same

@jreback jreback added this to the 0.24.0 milestone Nov 6, 2018
@jreback
Copy link
Contributor

jreback commented Nov 6, 2018

lgtm. can you rebase. ping on green.

@gfyoung gfyoung merged commit d0691e0 into pandas-dev:master Nov 6, 2018
@jbrockmendel jbrockmendel deleted the bigups2 branch November 7, 2018 00:10
JustinZhengBC pushed a commit to JustinZhengBC/pandas that referenced this pull request Nov 14, 2018
tm9k1 pushed a commit to tm9k1/pandas that referenced this pull request Nov 19, 2018
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this pull request Feb 28, 2019
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this pull request Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Error Reporting Incorrect or improved errors from pandas Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants