Skip to content

split off scalar tests to submodules #19752

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 5 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 1 addition & 132 deletions pandas/tests/scalar/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@ def test_timedelta_ops_scalar(self):
result = base - offset
assert result == expected_sub
Copy link
Contributor

Choose a reason for hiding this comment

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

move this to the sub module as well

Copy link
Member Author

Choose a reason for hiding this comment

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

This will get moved (and parametrized) in an upcoming pass. For right now this PR is intended to be a minimal set that will allow for #19378 to go through.

Copy link
Contributor

Choose a reason for hiding this comment

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

better to move it now

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, but just this one...


def test_ops_offsets(self):
td = Timedelta(10, unit='d')
assert Timedelta(241, unit='h') == td + pd.offsets.Hour(1)
assert Timedelta(241, unit='h') == pd.offsets.Hour(1) + td
assert 240 == td / pd.offsets.Hour(1)
assert 1 / 240.0 == pd.offsets.Hour(1) / td
assert Timedelta(239, unit='h') == td - pd.offsets.Hour(1)
assert Timedelta(-239, unit='h') == pd.offsets.Hour(1) - td

def test_unary_ops(self):
td = Timedelta(10, unit='d')

Expand All @@ -129,130 +120,8 @@ def test_unary_ops(self):

def test_binary_ops_nat(self):
td = Timedelta(10, unit='d')

assert (td - pd.NaT) is pd.NaT
assert (td + pd.NaT) is pd.NaT
# FIXME: The next test is wrong: td * NaT should raise
assert (td * pd.NaT) is pd.NaT
assert (td / pd.NaT) is np.nan
assert (td // pd.NaT) is np.nan
assert (td // np.timedelta64('NaT')) is np.nan

def test_binary_ops_integers(self):
td = Timedelta(10, unit='d')

assert td * 2 == Timedelta(20, unit='d')
assert td / 2 == Timedelta(5, unit='d')
assert td // 2 == Timedelta(5, unit='d')

# invert
assert td * -1 == Timedelta('-10d')
assert -1 * td == Timedelta('-10d')

# can't operate with integers
pytest.raises(TypeError, lambda: td + 2)
pytest.raises(TypeError, lambda: td - 2)

def test_binary_ops_with_timedelta(self):
td = Timedelta(10, unit='d')

assert td - td == Timedelta(0, unit='ns')
assert td + td == Timedelta(20, unit='d')
assert td / td == 1

# invalid multiply with another timedelta
pytest.raises(TypeError, lambda: td * td)

def test_floordiv(self):
# GH#18846
td = Timedelta(hours=3, minutes=4)
scalar = Timedelta(hours=3, minutes=3)

# scalar others
assert td // scalar == 1
assert -td // scalar.to_pytimedelta() == -2
assert (2 * td) // scalar.to_timedelta64() == 2

assert td // np.nan is pd.NaT
assert np.isnan(td // pd.NaT)
assert np.isnan(td // np.timedelta64('NaT'))

with pytest.raises(TypeError):
td // np.datetime64('2016-01-01', dtype='datetime64[us]')

expected = Timedelta(hours=1, minutes=32)
assert td // 2 == expected
assert td // 2.0 == expected
assert td // np.float64(2.0) == expected
assert td // np.int32(2.0) == expected
assert td // np.uint8(2.0) == expected

# Array-like others
assert td // np.array(scalar.to_timedelta64()) == 1

res = (3 * td) // np.array([scalar.to_timedelta64()])
expected = np.array([3], dtype=np.int64)
tm.assert_numpy_array_equal(res, expected)

res = (10 * td) // np.array([scalar.to_timedelta64(),
np.timedelta64('NaT')])
expected = np.array([10, np.nan])
tm.assert_numpy_array_equal(res, expected)

ser = pd.Series([1], dtype=np.int64)
res = td // ser
assert res.dtype.kind == 'm'

def test_rfloordiv(self):
# GH#18846
td = Timedelta(hours=3, minutes=3)
scalar = Timedelta(hours=3, minutes=4)

# scalar others
# x // Timedelta is defined only for timedelta-like x. int-like,
# float-like, and date-like, in particular, should all either
# a) raise TypeError directly or
# b) return NotImplemented, following which the reversed
# operation will raise TypeError.
assert td.__rfloordiv__(scalar) == 1
assert (-td).__rfloordiv__(scalar.to_pytimedelta()) == -2
assert (2 * td).__rfloordiv__(scalar.to_timedelta64()) == 0

assert np.isnan(td.__rfloordiv__(pd.NaT))
assert np.isnan(td.__rfloordiv__(np.timedelta64('NaT')))

dt64 = np.datetime64('2016-01-01', dtype='datetime64[us]')
with pytest.raises(TypeError):
td.__rfloordiv__(dt64)

assert td.__rfloordiv__(np.nan) is NotImplemented
assert td.__rfloordiv__(3.5) is NotImplemented
assert td.__rfloordiv__(2) is NotImplemented

with pytest.raises(TypeError):
td.__rfloordiv__(np.float64(2.0))
with pytest.raises(TypeError):
td.__rfloordiv__(np.int32(2.0))
with pytest.raises(TypeError):
td.__rfloordiv__(np.uint8(9))

# Array-like others
assert td.__rfloordiv__(np.array(scalar.to_timedelta64())) == 1

res = td.__rfloordiv__(np.array([(3 * scalar).to_timedelta64()]))
expected = np.array([3], dtype=np.int64)
tm.assert_numpy_array_equal(res, expected)

arr = np.array([(10 * scalar).to_timedelta64(),
np.timedelta64('NaT')])
res = td.__rfloordiv__(arr)
expected = np.array([10, np.nan])
tm.assert_numpy_array_equal(res, expected)

ser = pd.Series([1], dtype=np.int64)
res = td.__rfloordiv__(ser)
assert res is NotImplemented
with pytest.raises(TypeError):
ser // td


class TestTimedeltaComparison(object):
Expand Down
Empty file.
Loading