-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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 - remove scaling multiplier from Period
diff result
#23915
Conversation
Hello @ArtinSarraf! Thanks for updating the PR.
Comment last updated on December 07, 2018 at 04:37 Hours UTC |
Codecov Report
@@ Coverage Diff @@
## master #23915 +/- ##
==========================================
- Coverage 92.2% 92.2% -0.01%
==========================================
Files 162 162
Lines 51701 51700 -1
==========================================
- Hits 47672 47670 -2
- Misses 4029 4030 +1
Continue to review full report at Codecov.
|
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.
@ArtinSarraf : Good start! I have a couple of comments regarding the tests.
Also, don't forget to add a whatsnew
for this bug.
]) | ||
def test_period_diff(self, freq, expected): | ||
# GH 23878 | ||
for i in range(1, 4): |
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.
Parameterize on i
as well.
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.
Will do.
tm.assert_equal(result, expected) | ||
# This test is broken | ||
# result = to_offset('3M') + pi | ||
# tm.assert_equal(result, expected) |
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.
Why are we commenting this out?
Existing tests shouldn't be broken (or xfailed
) unless we have a very good reason for this...
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.
Ah yea, sorry meant to make a comment addressing this in the PR discussion. This was failing for me on a clean checkout of the latest master code, before I made my changes. I can provide more details tomorrow.
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 you could revert the change here and let CI run on it, that would be great actually, so that we can also check if it's just a local failure or a more likely implementation issue.
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.
put it back and tests passed, looks like it was some transient local issue.
@gfyoung - the reason I didn’t add a whatsnew entry is that this Offset result from diffing periods is already new behavior in 0.24 so the bug had not been released yet. Should I still add an entry for it? |
Ah, gotcha. In that case, just add a reference to your issue number to the existing |
pandas/_libs/tslibs/period.pyx
Outdated
@@ -1685,7 +1685,7 @@ cdef class _Period(object): | |||
if other.freq != self.freq: | |||
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr) | |||
raise IncompatibleFrequency(msg) | |||
return (self.ordinal - other.ordinal) * self.freq | |||
return (self.ordinal - other.ordinal) * type(self.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.
Won't this be wrong for offsets that have relevant keywords?
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’re right. So one option would be to do
... * type(self.freq)(normalize=self.freq.normalize, **self.freq.kwds)
This way no other classes need to be modified. However, I think it might be worth adding a property to the DateOffset objet to do the above suggestion. Something like DateOffset.base
?
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.
fixed
@@ -1085,3 +1085,21 @@ def test_pi_sub_period_nat(self): | |||
exp = pd.TimedeltaIndex([np.nan, np.nan, np.nan, np.nan], name='idx') | |||
tm.assert_index_equal(idx - pd.Period('NaT', freq='M'), exp) | |||
tm.assert_index_equal(pd.Period('NaT', freq='M') - idx, exp) | |||
|
|||
|
|||
class TestPeriodArithmetic(object): |
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.
This all belongs in pandas.tests.scalar.period. If you want to make a new file test_arithmetic.py in that directory, that'd be OK. Otherwise put in test_period.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.
moved
(pd.offsets.Day, 214), | ||
(pd.offsets.MonthEnd, 7), | ||
(pd.offsets.YearEnd, 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.
definitely needs cases with kwargs passed to offset constructors. Putting it in tests.tseries.offsets might be useful since there are test classes that construct a bunch of these
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.
parameterized the tests on some kwargs too, only YearEnd takes any kwds besides normalize ('month'). And only non-Tick offsets (in this case MonthEnd and YearEnd) can take normalize=True.
@gfyoung / @jbrockmendel - any other changes to consider? |
pandas/_libs/tslibs/period.pyx
Outdated
@@ -1685,7 +1685,9 @@ cdef class _Period(object): | |||
if other.freq != self.freq: | |||
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr) | |||
raise IncompatibleFrequency(msg) | |||
return (self.ordinal - other.ordinal) * self.freq | |||
base_freq = type(self.freq)(normalize=self.freq.normalize, | |||
**self.freq.kwds) |
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.
pass n=1
explicitly here.
add a reference # GH#23915
for future readers
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.
done
expected = 0 | ||
else: | ||
return | ||
# Only non-Tick frequencies can have normalize set to True |
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.
probably cleaner to test separately
also this gets a couple of non-tick frequencies, but using the structure in tests.tseries.offsets should make it feasible to be a lot more thorough
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.
Separated.
Only 4 of the non-tick frequencies in pd.offsets are valid Period frequencies so I explicitly parameterized those in the tests. The Tick fixture worked well though.
It looks like the same bug exists in the analogous |
@gfyoung if you have any comments. |
@jbrockmendel if you'd have a look at the tests and see if they are sufficient |
@ArtinSarraf can you merge master and see if you can resolve failures |
@jreback / @jbrockmendel - is there any way to recreate the testing envs of the automated tests. My tests run fine locally and from the failed test output its not obvious what the error is (since the repr of the result and expected are the same), I'm assuming either the normalize kwd attributes are differing somehow. |
Found the issue. Looks like this is due to an existing bug with PeriodIndex (I will open a separate issue for this). See the example below.
Restart the process
Looks like the I will remove the normalize parameterization from the tests for now. |
Good catch. Best guess is it is in pandas.tseries.frequencies |
@jbrockmendel - looks like the only failing test in the pandas-dev.pandas tests are linting errors now due to importing the tick_classes fixture (since it's not in the discovery path for those tests).
Is there another way to discover this fixture? I could move the test, but I think it makes sense to keep it grouped where it is. |
@@ -16,6 +16,7 @@ | |||
from pandas.core import ops | |||
from pandas import Period, PeriodIndex, period_range, Series | |||
from pandas.tseries.frequencies import to_offset |
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.
so we can't do this, instead move the tick_classes fixture to pandas/conftest.py. I think everything should still work
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.
done.
@jreback / @jbrockmendel - anything else to consider? |
@ArtinSarraf lgtm. can you add a whatsnew note. ping on green. @gfyoung good? |
@jreback - tests are all clean. |
thanks @ArtinSarraf |
git diff upstream/master -u -- "*.py" | flake8 --diff