Skip to content

Commit

Permalink
split off scalar tests to submodules (#19752)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Feb 19, 2018
1 parent 73ecfe6 commit 718d067
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 163 deletions.
Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
Empty file.
422 changes: 422 additions & 0 deletions pandas/tests/scalar/timedelta/test_arithmetic.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,6 @@ class Other:
assert td.__mul__(other) is NotImplemented
assert td.__floordiv__(other) is NotImplemented

def test_timedelta_ops_scalar(self):
# GH 6808
base = pd.to_datetime('20130101 09:01:12.123456')
expected_add = pd.to_datetime('20130101 09:01:22.123456')
expected_sub = pd.to_datetime('20130101 09:01:02.123456')

for offset in [pd.to_timedelta(10, unit='s'), timedelta(seconds=10),
np.timedelta64(10, 's'),
np.timedelta64(10000000000, 'ns'),
pd.offsets.Second(10)]:
result = base + offset
assert result == expected_add

result = base - offset
assert result == expected_sub

base = pd.to_datetime('20130102 09:01:12.123456')
expected_add = pd.to_datetime('20130103 09:01:22.123456')
expected_sub = pd.to_datetime('20130101 09:01:02.123456')

for offset in [pd.to_timedelta('1 day, 00:00:10'),
pd.to_timedelta('1 days, 00:00:10'),
timedelta(days=1, seconds=10),
np.timedelta64(1, 'D') + np.timedelta64(10, 's'),
pd.offsets.Day() + pd.offsets.Second(10)]:
result = base + offset
assert result == expected_add

result = base - offset
assert result == expected_sub

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 +89,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
File renamed without changes.

0 comments on commit 718d067

Please sign in to comment.