-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Parametrize tests for Series[timedelta64] integer arithmetic #19166
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
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4644972
break up repetitive test; make more thorough and explicit
jbrockmendel 77b3ad7
further break up big test, parametrize, test missing cases, identify …
jbrockmendel 5e6281e
parametrize the last bits of test_timedelta64_operations_with_integers
jbrockmendel a376bf5
Merge branch 'master' of https://github.com/pandas-dev/pandas into pa…
jbrockmendel 2750c20
remove setup_class
jbrockmendel 69703e1
rename test_timedelta_series_ops more informative name
jbrockmendel de624d5
Merge branch 'master' of https://github.com/pandas-dev/pandas into pa…
jbrockmendel 5560f07
use fixture as requested
jbrockmendel 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -667,83 +667,182 @@ def test_div(self): | |
assert_series_equal(result, expected) | ||
|
||
|
||
class TestTimedeltaSeriesArithmetic(object): | ||
def test_timedelta_series_ops(self): | ||
# GH11925 | ||
s = Series(timedelta_range('1 day', periods=3)) | ||
ts = Timestamp('2012-01-01') | ||
expected = Series(date_range('2012-01-02', periods=3)) | ||
assert_series_equal(ts + s, expected) | ||
assert_series_equal(s + ts, expected) | ||
class TestTimedeltaSeriesArithmeticWithIntegers(object): | ||
@classmethod | ||
def setup_class(cls): | ||
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. don't setup the class, rather use a fixture |
||
cls.tdser = Series(['59 Days', '59 Days', 'NaT'], | ||
dtype='timedelta64[ns]') | ||
cls.intser = Series([2, 3, 4]) | ||
|
||
expected2 = Series(date_range('2011-12-31', periods=3, freq='-1D')) | ||
assert_series_equal(ts - s, expected2) | ||
assert_series_equal(ts + (-s), expected2) | ||
# ------------------------------------------------------------------ | ||
# Addition and Subtraction | ||
|
||
def test_td64series_add_int_series_invalid(self): | ||
with pytest.raises(TypeError): | ||
self.tdser + self.intser | ||
|
||
@pytest.mark.xfail(reason='GH#19123 integer interpreted as nanoseconds') | ||
def test_td64series_radd_int_series_invalid(self): | ||
with pytest.raises(TypeError): | ||
self.intser + self.tdser | ||
|
||
def test_td64series_sub_int_series_invalid(self): | ||
with pytest.raises(TypeError): | ||
self.tdser - self.intser | ||
|
||
@pytest.mark.xfail(reason='GH#19123 integer interpreted as nanoseconds') | ||
def test_td64series_rsub_int_series_invalid(self): | ||
with pytest.raises(TypeError): | ||
self.intser - self.tdser | ||
|
||
def test_timedelta64_operations_with_integers(self): | ||
@pytest.mark.parametrize('scalar', [1, 1.5, np.array(2)]) | ||
def test_td64series_add_sub_numeric_scalar_invalid(self, scalar): | ||
with pytest.raises(TypeError): | ||
self.tdser + scalar | ||
with pytest.raises(TypeError): | ||
scalar + self.tdser | ||
with pytest.raises(TypeError): | ||
self.tdser - scalar | ||
with pytest.raises(TypeError): | ||
scalar - self.tdser | ||
|
||
@pytest.mark.parametrize('dtype', ['int64', 'int32', 'int16', | ||
'uint64', 'uint32', 'uint16', 'uint8', | ||
'float64', 'float32', 'float16']) | ||
@pytest.mark.parametrize('vector', [ | ||
np.array([1, 2, 3]), | ||
pd.Index([1, 2, 3]), | ||
pytest.param(Series([1, 2, 3]), | ||
marks=pytest.mark.xfail(reason='GH#19123 integer ' | ||
'interpreted as nanos')) | ||
]) | ||
def test_td64series_add_sub_numeric_array_invalid(self, vector, dtype): | ||
vector = vector.astype(dtype) | ||
with pytest.raises(TypeError): | ||
self.tdser + vector | ||
with pytest.raises(TypeError): | ||
vector + self.tdser | ||
with pytest.raises(TypeError): | ||
self.tdser - vector | ||
with pytest.raises(TypeError): | ||
vector - self.tdser | ||
|
||
# ------------------------------------------------------------------ | ||
# Multiplicaton and Division | ||
|
||
@pytest.mark.parametrize('dtype', ['int64', 'int32', 'int16', | ||
'uint64', 'uint32', 'uint16', 'uint8', | ||
'float64', 'float32', 'float16']) | ||
@pytest.mark.parametrize('vector', [np.array([20, 30, 40]), | ||
pd.Index([20, 30, 40]), | ||
Series([20, 30, 40])]) | ||
def test_td64series_div_numeric_array(self, vector, dtype): | ||
# GH 4521 | ||
# divide/multiply by integers | ||
startdate = Series(date_range('2013-01-01', '2013-01-03')) | ||
enddate = Series(date_range('2013-03-01', '2013-03-03')) | ||
tdser = self.tdser | ||
vector = vector.astype(dtype) | ||
expected = Series(['2.95D', '1D 23H 12m', 'NaT'], | ||
dtype='timedelta64[ns]') | ||
|
||
s1 = enddate - startdate | ||
s1[2] = np.nan | ||
s2 = Series([2, 3, 4]) | ||
expected = Series(s1.values.astype(np.int64) / s2, dtype='m8[ns]') | ||
expected[2] = np.nan | ||
result = s1 / s2 | ||
result = tdser / vector | ||
assert_series_equal(result, expected) | ||
|
||
s2 = Series([20, 30, 40]) | ||
expected = Series(s1.values.astype(np.int64) / s2, dtype='m8[ns]') | ||
expected[2] = np.nan | ||
result = s1 / s2 | ||
with pytest.raises(TypeError): | ||
vector / tdser | ||
|
||
@pytest.mark.parametrize('dtype', ['int64', 'int32', 'int16', | ||
'uint64', 'uint32', 'uint16', 'uint8', | ||
'float64', 'float32', 'float16']) | ||
@pytest.mark.parametrize('vector', [np.array([20, 30, 40]), | ||
pd.Index([20, 30, 40]), | ||
Series([20, 30, 40])]) | ||
def test_td64series_mul_numeric_array(self, vector, dtype): | ||
# GH 4521 | ||
# divide/multiply by integers | ||
tdser = self.tdser | ||
vector = vector.astype(dtype) | ||
|
||
expected = Series(['1180 Days', '1770 Days', 'NaT'], | ||
dtype='timedelta64[ns]') | ||
|
||
result = tdser * vector | ||
assert_series_equal(result, expected) | ||
|
||
result = s1 / 2 | ||
expected = Series(s1.values.astype(np.int64) / 2, dtype='m8[ns]') | ||
expected[2] = np.nan | ||
@pytest.mark.parametrize('dtype', ['int64', 'int32', 'int16', | ||
'uint64', 'uint32', 'uint16', 'uint8', | ||
'float64', 'float32', 'float16']) | ||
@pytest.mark.parametrize('vector', [ | ||
np.array([20, 30, 40]), | ||
pytest.param(pd.Index([20, 30, 40]), | ||
marks=pytest.mark.xfail(reason='__mul__ raises ' | ||
'instead of returning ' | ||
'NotImplemented')), | ||
Series([20, 30, 40]) | ||
]) | ||
def test_td64series_rmul_numeric_array(self, vector, dtype): | ||
# GH 4521 | ||
# divide/multiply by integers | ||
tdser = self.tdser | ||
vector = vector.astype(dtype) | ||
|
||
expected = Series(['1180 Days', '1770 Days', 'NaT'], | ||
dtype='timedelta64[ns]') | ||
|
||
result = vector * tdser | ||
assert_series_equal(result, expected) | ||
|
||
s2 = Series([20, 30, 40]) | ||
expected = Series(s1.values.astype(np.int64) * s2, dtype='m8[ns]') | ||
expected[2] = np.nan | ||
result = s1 * s2 | ||
@pytest.mark.parametrize('one', [1, np.array(1), 1.0, np.array(1.0)]) | ||
def test_td64series_mul_numeric_scalar(self, one): | ||
# GH 4521 | ||
# divide/multiply by integers | ||
tdser = self.tdser | ||
expected = Series(['-59 Days', '-59 Days', 'NaT'], | ||
dtype='timedelta64[ns]') | ||
|
||
result = tdser * (-one) | ||
assert_series_equal(result, expected) | ||
result = (-one) * tdser | ||
assert_series_equal(result, expected) | ||
|
||
for dtype in ['int32', 'int16', 'uint32', 'uint64', 'uint32', 'uint16', | ||
'uint8']: | ||
s2 = Series([20, 30, 40], dtype=dtype) | ||
expected = Series( | ||
s1.values.astype(np.int64) * s2.astype(np.int64), | ||
dtype='m8[ns]') | ||
expected[2] = np.nan | ||
result = s1 * s2 | ||
assert_series_equal(result, expected) | ||
expected = Series(['118 Days', '118 Days', 'NaT'], | ||
dtype='timedelta64[ns]') | ||
|
||
result = s1 * 2 | ||
expected = Series(s1.values.astype(np.int64) * 2, dtype='m8[ns]') | ||
expected[2] = np.nan | ||
result = tdser * (2 * one) | ||
assert_series_equal(result, expected) | ||
result = (2 * one) * tdser | ||
assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('two', [ | ||
2, 2.0, | ||
pytest.param(np.array(2), | ||
marks=pytest.mark.xfail(reason='GH#19011 is_list_like ' | ||
'incorrectly True.')), | ||
pytest.param(np.array(2.0), | ||
marks=pytest.mark.xfail(reason='GH#19011 is_list_like ' | ||
'incorrectly True.')), | ||
]) | ||
def test_td64series_div_numeric_scalar(self, two): | ||
# GH 4521 | ||
# divide/multiply by integers | ||
tdser = self.tdser | ||
expected = Series(['29.5D', '29.5D', 'NaT'], dtype='timedelta64[ns]') | ||
|
||
result = s1 * -1 | ||
expected = Series(s1.values.astype(np.int64) * -1, dtype='m8[ns]') | ||
expected[2] = np.nan | ||
result = tdser / two | ||
assert_series_equal(result, expected) | ||
|
||
# invalid ops | ||
assert_series_equal(s1 / s2.astype(float), | ||
Series([Timedelta('2 days 22:48:00'), Timedelta( | ||
'1 days 23:12:00'), Timedelta('NaT')])) | ||
assert_series_equal(s1 / 2.0, | ||
Series([Timedelta('29 days 12:00:00'), Timedelta( | ||
'29 days 12:00:00'), Timedelta('NaT')])) | ||
|
||
for op in ['__add__', '__sub__']: | ||
sop = getattr(s1, op, None) | ||
if sop is not None: | ||
pytest.raises(TypeError, sop, 1) | ||
pytest.raises(TypeError, sop, s2.values) | ||
|
||
class TestTimedeltaSeriesArithmetic(object): | ||
def test_timedelta_series_ops(self): | ||
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. doc-string |
||
# GH11925 | ||
s = Series(timedelta_range('1 day', periods=3)) | ||
ts = Timestamp('2012-01-01') | ||
expected = Series(date_range('2012-01-02', periods=3)) | ||
assert_series_equal(ts + s, expected) | ||
assert_series_equal(s + ts, expected) | ||
|
||
expected2 = Series(date_range('2011-12-31', periods=3, freq='-1D')) | ||
assert_series_equal(ts - s, expected2) | ||
assert_series_equal(ts + (-s), expected2) | ||
|
||
def test_timedelta64_operations_with_DateOffset(self): | ||
# GH 10699 | ||
|
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.
I know this seems obvious but add a comment on what this class is testing