8th day of python challenges 111-117
This commit is contained in:
@@ -0,0 +1,691 @@
|
||||
"""
|
||||
Tests for scalar Timedelta arithmetic ops
|
||||
"""
|
||||
from datetime import datetime, timedelta
|
||||
import operator
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
from pandas import NaT, Timedelta, Timestamp
|
||||
from pandas.core import ops
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
class TestTimedeltaAdditionSubtraction:
|
||||
"""
|
||||
Tests for Timedelta methods:
|
||||
|
||||
__add__, __radd__,
|
||||
__sub__, __rsub__
|
||||
"""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"ten_seconds",
|
||||
[
|
||||
Timedelta(10, unit="s"),
|
||||
timedelta(seconds=10),
|
||||
np.timedelta64(10, "s"),
|
||||
np.timedelta64(10000000000, "ns"),
|
||||
pd.offsets.Second(10),
|
||||
],
|
||||
)
|
||||
def test_td_add_sub_ten_seconds(self, ten_seconds):
|
||||
# GH#6808
|
||||
base = Timestamp("20130101 09:01:12.123456")
|
||||
expected_add = Timestamp("20130101 09:01:22.123456")
|
||||
expected_sub = Timestamp("20130101 09:01:02.123456")
|
||||
|
||||
result = base + ten_seconds
|
||||
assert result == expected_add
|
||||
|
||||
result = base - ten_seconds
|
||||
assert result == expected_sub
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"one_day_ten_secs",
|
||||
[
|
||||
Timedelta("1 day, 00:00:10"),
|
||||
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),
|
||||
],
|
||||
)
|
||||
def test_td_add_sub_one_day_ten_seconds(self, one_day_ten_secs):
|
||||
# GH#6808
|
||||
base = Timestamp("20130102 09:01:12.123456")
|
||||
expected_add = Timestamp("20130103 09:01:22.123456")
|
||||
expected_sub = Timestamp("20130101 09:01:02.123456")
|
||||
|
||||
result = base + one_day_ten_secs
|
||||
assert result == expected_add
|
||||
|
||||
result = base - one_day_ten_secs
|
||||
assert result == expected_sub
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
||||
def test_td_add_datetimelike_scalar(self, op):
|
||||
# GH#19738
|
||||
td = Timedelta(10, unit="d")
|
||||
|
||||
result = op(td, datetime(2016, 1, 1))
|
||||
if op is operator.add:
|
||||
# datetime + Timedelta does _not_ call Timedelta.__radd__,
|
||||
# so we get a datetime back instead of a Timestamp
|
||||
assert isinstance(result, Timestamp)
|
||||
assert result == Timestamp(2016, 1, 11)
|
||||
|
||||
result = op(td, Timestamp("2018-01-12 18:09"))
|
||||
assert isinstance(result, Timestamp)
|
||||
assert result == Timestamp("2018-01-22 18:09")
|
||||
|
||||
result = op(td, np.datetime64("2018-01-12"))
|
||||
assert isinstance(result, Timestamp)
|
||||
assert result == Timestamp("2018-01-22")
|
||||
|
||||
result = op(td, NaT)
|
||||
assert result is NaT
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
||||
def test_td_add_td(self, op):
|
||||
td = Timedelta(10, unit="d")
|
||||
|
||||
result = op(td, Timedelta(days=10))
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(days=20)
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
||||
def test_td_add_pytimedelta(self, op):
|
||||
td = Timedelta(10, unit="d")
|
||||
result = op(td, timedelta(days=9))
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(days=19)
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
||||
def test_td_add_timedelta64(self, op):
|
||||
td = Timedelta(10, unit="d")
|
||||
result = op(td, np.timedelta64(-4, "D"))
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(days=6)
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
||||
def test_td_add_offset(self, op):
|
||||
td = Timedelta(10, unit="d")
|
||||
|
||||
result = op(td, pd.offsets.Hour(6))
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(days=10, hours=6)
|
||||
|
||||
def test_td_sub_td(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
expected = Timedelta(0, unit="ns")
|
||||
result = td - td
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == expected
|
||||
|
||||
def test_td_sub_pytimedelta(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
expected = Timedelta(0, unit="ns")
|
||||
|
||||
result = td - td.to_pytimedelta()
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == expected
|
||||
|
||||
result = td.to_pytimedelta() - td
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == expected
|
||||
|
||||
def test_td_sub_timedelta64(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
expected = Timedelta(0, unit="ns")
|
||||
|
||||
result = td - td.to_timedelta64()
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == expected
|
||||
|
||||
result = td.to_timedelta64() - td
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == expected
|
||||
|
||||
def test_td_sub_nat(self):
|
||||
# In this context pd.NaT is treated as timedelta-like
|
||||
td = Timedelta(10, unit="d")
|
||||
result = td - NaT
|
||||
assert result is NaT
|
||||
|
||||
def test_td_sub_td64_nat(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
td_nat = np.timedelta64("NaT")
|
||||
|
||||
result = td - td_nat
|
||||
assert result is NaT
|
||||
|
||||
result = td_nat - td
|
||||
assert result is NaT
|
||||
|
||||
def test_td_sub_offset(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
result = td - pd.offsets.Hour(1)
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(239, unit="h")
|
||||
|
||||
def test_td_add_sub_numeric_raises(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
for other in [2, 2.0, np.int64(2), np.float64(2)]:
|
||||
with pytest.raises(TypeError):
|
||||
td + other
|
||||
with pytest.raises(TypeError):
|
||||
other + td
|
||||
with pytest.raises(TypeError):
|
||||
td - other
|
||||
with pytest.raises(TypeError):
|
||||
other - td
|
||||
|
||||
def test_td_rsub_nat(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
result = NaT - td
|
||||
assert result is NaT
|
||||
|
||||
result = np.datetime64("NaT") - td
|
||||
assert result is NaT
|
||||
|
||||
def test_td_rsub_offset(self):
|
||||
result = pd.offsets.Hour(1) - Timedelta(10, unit="d")
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(-239, unit="h")
|
||||
|
||||
def test_td_sub_timedeltalike_object_dtype_array(self):
|
||||
# GH#21980
|
||||
arr = np.array([Timestamp("20130101 9:01"), Timestamp("20121230 9:02")])
|
||||
exp = np.array([Timestamp("20121231 9:01"), Timestamp("20121229 9:02")])
|
||||
res = arr - Timedelta("1D")
|
||||
tm.assert_numpy_array_equal(res, exp)
|
||||
|
||||
def test_td_sub_mixed_most_timedeltalike_object_dtype_array(self):
|
||||
# GH#21980
|
||||
now = Timestamp.now()
|
||||
arr = np.array([now, Timedelta("1D"), np.timedelta64(2, "h")])
|
||||
exp = np.array(
|
||||
[
|
||||
now - Timedelta("1D"),
|
||||
Timedelta("0D"),
|
||||
np.timedelta64(2, "h") - Timedelta("1D"),
|
||||
]
|
||||
)
|
||||
res = arr - Timedelta("1D")
|
||||
tm.assert_numpy_array_equal(res, exp)
|
||||
|
||||
def test_td_rsub_mixed_most_timedeltalike_object_dtype_array(self):
|
||||
# GH#21980
|
||||
now = Timestamp.now()
|
||||
arr = np.array([now, Timedelta("1D"), np.timedelta64(2, "h")])
|
||||
with pytest.raises(TypeError):
|
||||
Timedelta("1D") - arr
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
||||
def test_td_add_timedeltalike_object_dtype_array(self, op):
|
||||
# GH#21980
|
||||
arr = np.array([Timestamp("20130101 9:01"), Timestamp("20121230 9:02")])
|
||||
exp = np.array([Timestamp("20130102 9:01"), Timestamp("20121231 9:02")])
|
||||
res = op(arr, Timedelta("1D"))
|
||||
tm.assert_numpy_array_equal(res, exp)
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
||||
def test_td_add_mixed_timedeltalike_object_dtype_array(self, op):
|
||||
# GH#21980
|
||||
now = Timestamp.now()
|
||||
arr = np.array([now, Timedelta("1D")])
|
||||
exp = np.array([now + Timedelta("1D"), Timedelta("2D")])
|
||||
res = op(arr, Timedelta("1D"))
|
||||
tm.assert_numpy_array_equal(res, exp)
|
||||
|
||||
|
||||
class TestTimedeltaMultiplicationDivision:
|
||||
"""
|
||||
Tests for Timedelta methods:
|
||||
|
||||
__mul__, __rmul__,
|
||||
__div__, __rdiv__,
|
||||
__truediv__, __rtruediv__,
|
||||
__floordiv__, __rfloordiv__,
|
||||
__mod__, __rmod__,
|
||||
__divmod__, __rdivmod__
|
||||
"""
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# Timedelta.__mul__, __rmul__
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"td_nat", [NaT, np.timedelta64("NaT", "ns"), np.timedelta64("NaT")]
|
||||
)
|
||||
@pytest.mark.parametrize("op", [operator.mul, ops.rmul])
|
||||
def test_td_mul_nat(self, op, td_nat):
|
||||
# GH#19819
|
||||
td = Timedelta(10, unit="d")
|
||||
with pytest.raises(TypeError):
|
||||
op(td, td_nat)
|
||||
|
||||
@pytest.mark.parametrize("nan", [np.nan, np.float64("NaN"), float("nan")])
|
||||
@pytest.mark.parametrize("op", [operator.mul, ops.rmul])
|
||||
def test_td_mul_nan(self, op, nan):
|
||||
# np.float64('NaN') has a 'dtype' attr, avoid treating as array
|
||||
td = Timedelta(10, unit="d")
|
||||
result = op(td, nan)
|
||||
assert result is NaT
|
||||
|
||||
@pytest.mark.parametrize("op", [operator.mul, ops.rmul])
|
||||
def test_td_mul_scalar(self, op):
|
||||
# GH#19738
|
||||
td = Timedelta(minutes=3)
|
||||
|
||||
result = op(td, 2)
|
||||
assert result == Timedelta(minutes=6)
|
||||
|
||||
result = op(td, 1.5)
|
||||
assert result == Timedelta(minutes=4, seconds=30)
|
||||
|
||||
assert op(td, np.nan) is NaT
|
||||
|
||||
assert op(-1, td).value == -1 * td.value
|
||||
assert op(-1.0, td).value == -1.0 * td.value
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
# timedelta * datetime is gibberish
|
||||
op(td, Timestamp(2016, 1, 2))
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
# invalid multiply with another timedelta
|
||||
op(td, td)
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# Timedelta.__div__, __truediv__
|
||||
|
||||
def test_td_div_timedeltalike_scalar(self):
|
||||
# GH#19738
|
||||
td = Timedelta(10, unit="d")
|
||||
|
||||
result = td / pd.offsets.Hour(1)
|
||||
assert result == 240
|
||||
|
||||
assert td / td == 1
|
||||
assert td / np.timedelta64(60, "h") == 4
|
||||
|
||||
assert np.isnan(td / NaT)
|
||||
|
||||
def test_td_div_numeric_scalar(self):
|
||||
# GH#19738
|
||||
td = Timedelta(10, unit="d")
|
||||
|
||||
result = td / 2
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(days=5)
|
||||
|
||||
result = td / 5.0
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(days=2)
|
||||
|
||||
@pytest.mark.parametrize("nan", [np.nan, np.float64("NaN"), float("nan")])
|
||||
def test_td_div_nan(self, nan):
|
||||
# np.float64('NaN') has a 'dtype' attr, avoid treating as array
|
||||
td = Timedelta(10, unit="d")
|
||||
result = td / nan
|
||||
assert result is NaT
|
||||
|
||||
result = td // nan
|
||||
assert result is NaT
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# Timedelta.__rdiv__
|
||||
|
||||
def test_td_rdiv_timedeltalike_scalar(self):
|
||||
# GH#19738
|
||||
td = Timedelta(10, unit="d")
|
||||
result = pd.offsets.Hour(1) / td
|
||||
assert result == 1 / 240.0
|
||||
|
||||
assert np.timedelta64(60, "h") / td == 0.25
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# Timedelta.__floordiv__
|
||||
|
||||
def test_td_floordiv_timedeltalike_scalar(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=4)
|
||||
scalar = Timedelta(hours=3, minutes=3)
|
||||
|
||||
assert td // scalar == 1
|
||||
assert -td // scalar.to_pytimedelta() == -2
|
||||
assert (2 * td) // scalar.to_timedelta64() == 2
|
||||
|
||||
def test_td_floordiv_null_scalar(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=4)
|
||||
|
||||
assert td // np.nan is NaT
|
||||
assert np.isnan(td // NaT)
|
||||
assert np.isnan(td // np.timedelta64("NaT"))
|
||||
|
||||
def test_td_floordiv_offsets(self):
|
||||
# GH#19738
|
||||
td = Timedelta(hours=3, minutes=4)
|
||||
assert td // pd.offsets.Hour(1) == 3
|
||||
assert td // pd.offsets.Minute(2) == 92
|
||||
|
||||
def test_td_floordiv_invalid_scalar(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=4)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
td // np.datetime64("2016-01-01", dtype="datetime64[us]")
|
||||
|
||||
def test_td_floordiv_numeric_scalar(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=4)
|
||||
|
||||
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
|
||||
|
||||
def test_td_floordiv_timedeltalike_array(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=4)
|
||||
scalar = Timedelta(hours=3, minutes=3)
|
||||
|
||||
# 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)
|
||||
|
||||
def test_td_floordiv_numeric_series(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=4)
|
||||
ser = pd.Series([1], dtype=np.int64)
|
||||
res = td // ser
|
||||
assert res.dtype.kind == "m"
|
||||
|
||||
# ---------------------------------------------------------------
|
||||
# Timedelta.__rfloordiv__
|
||||
|
||||
def test_td_rfloordiv_timedeltalike_scalar(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
|
||||
|
||||
def test_td_rfloordiv_null_scalar(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=3)
|
||||
|
||||
assert np.isnan(td.__rfloordiv__(NaT))
|
||||
assert np.isnan(td.__rfloordiv__(np.timedelta64("NaT")))
|
||||
|
||||
def test_td_rfloordiv_offsets(self):
|
||||
# GH#19738
|
||||
assert pd.offsets.Hour(1) // Timedelta(minutes=25) == 2
|
||||
|
||||
def test_td_rfloordiv_invalid_scalar(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=3)
|
||||
|
||||
dt64 = np.datetime64("2016-01-01", dtype="datetime64[us]")
|
||||
with pytest.raises(TypeError):
|
||||
td.__rfloordiv__(dt64)
|
||||
|
||||
def test_td_rfloordiv_numeric_scalar(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=3)
|
||||
|
||||
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.uint8(9))
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
# GH-19761: Change to TypeError.
|
||||
td.__rfloordiv__(np.int32(2.0))
|
||||
|
||||
def test_td_rfloordiv_timedeltalike_array(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=3)
|
||||
scalar = Timedelta(hours=3, minutes=4)
|
||||
|
||||
# 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)
|
||||
|
||||
def test_td_rfloordiv_numeric_series(self):
|
||||
# GH#18846
|
||||
td = Timedelta(hours=3, minutes=3)
|
||||
ser = pd.Series([1], dtype=np.int64)
|
||||
res = td.__rfloordiv__(ser)
|
||||
assert res is NotImplemented
|
||||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
|
||||
# TODO: GH-19761. Change to TypeError.
|
||||
ser // td
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Timedelta.__mod__, __rmod__
|
||||
|
||||
def test_mod_timedeltalike(self):
|
||||
# GH#19365
|
||||
td = Timedelta(hours=37)
|
||||
|
||||
# Timedelta-like others
|
||||
result = td % Timedelta(hours=6)
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(hours=1)
|
||||
|
||||
result = td % timedelta(minutes=60)
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(0)
|
||||
|
||||
result = td % NaT
|
||||
assert result is NaT
|
||||
|
||||
def test_mod_timedelta64_nat(self):
|
||||
# GH#19365
|
||||
td = Timedelta(hours=37)
|
||||
|
||||
result = td % np.timedelta64("NaT", "ns")
|
||||
assert result is NaT
|
||||
|
||||
def test_mod_timedelta64(self):
|
||||
# GH#19365
|
||||
td = Timedelta(hours=37)
|
||||
|
||||
result = td % np.timedelta64(2, "h")
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(hours=1)
|
||||
|
||||
def test_mod_offset(self):
|
||||
# GH#19365
|
||||
td = Timedelta(hours=37)
|
||||
|
||||
result = td % pd.offsets.Hour(5)
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(hours=2)
|
||||
|
||||
def test_mod_numeric(self):
|
||||
# GH#19365
|
||||
td = Timedelta(hours=37)
|
||||
|
||||
# Numeric Others
|
||||
result = td % 2
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(0)
|
||||
|
||||
result = td % 1e12
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(minutes=3, seconds=20)
|
||||
|
||||
result = td % int(1e12)
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(minutes=3, seconds=20)
|
||||
|
||||
def test_mod_invalid(self):
|
||||
# GH#19365
|
||||
td = Timedelta(hours=37)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
td % Timestamp("2018-01-22")
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
td % []
|
||||
|
||||
def test_rmod_pytimedelta(self):
|
||||
# GH#19365
|
||||
td = Timedelta(minutes=3)
|
||||
|
||||
result = timedelta(minutes=4) % td
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(minutes=1)
|
||||
|
||||
def test_rmod_timedelta64(self):
|
||||
# GH#19365
|
||||
td = Timedelta(minutes=3)
|
||||
result = np.timedelta64(5, "m") % td
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == Timedelta(minutes=2)
|
||||
|
||||
def test_rmod_invalid(self):
|
||||
# GH#19365
|
||||
td = Timedelta(minutes=3)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
Timestamp("2018-01-22") % td
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
15 % td
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
16.0 % td
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
np.array([22, 24]) % td
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Timedelta.__divmod__, __rdivmod__
|
||||
|
||||
def test_divmod_numeric(self):
|
||||
# GH#19365
|
||||
td = Timedelta(days=2, hours=6)
|
||||
|
||||
result = divmod(td, 53 * 3600 * 1e9)
|
||||
assert result[0] == Timedelta(1, unit="ns")
|
||||
assert isinstance(result[1], Timedelta)
|
||||
assert result[1] == Timedelta(hours=1)
|
||||
|
||||
assert result
|
||||
result = divmod(td, np.nan)
|
||||
assert result[0] is NaT
|
||||
assert result[1] is NaT
|
||||
|
||||
def test_divmod(self):
|
||||
# GH#19365
|
||||
td = Timedelta(days=2, hours=6)
|
||||
|
||||
result = divmod(td, timedelta(days=1))
|
||||
assert result[0] == 2
|
||||
assert isinstance(result[1], Timedelta)
|
||||
assert result[1] == Timedelta(hours=6)
|
||||
|
||||
result = divmod(td, 54)
|
||||
assert result[0] == Timedelta(hours=1)
|
||||
assert isinstance(result[1], Timedelta)
|
||||
assert result[1] == Timedelta(0)
|
||||
|
||||
result = divmod(td, NaT)
|
||||
assert np.isnan(result[0])
|
||||
assert result[1] is NaT
|
||||
|
||||
def test_divmod_offset(self):
|
||||
# GH#19365
|
||||
td = Timedelta(days=2, hours=6)
|
||||
|
||||
result = divmod(td, pd.offsets.Hour(-4))
|
||||
assert result[0] == -14
|
||||
assert isinstance(result[1], Timedelta)
|
||||
assert result[1] == Timedelta(hours=-2)
|
||||
|
||||
def test_divmod_invalid(self):
|
||||
# GH#19365
|
||||
td = Timedelta(days=2, hours=6)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
divmod(td, Timestamp("2018-01-22"))
|
||||
|
||||
def test_rdivmod_pytimedelta(self):
|
||||
# GH#19365
|
||||
result = divmod(timedelta(days=2, hours=6), Timedelta(days=1))
|
||||
assert result[0] == 2
|
||||
assert isinstance(result[1], Timedelta)
|
||||
assert result[1] == Timedelta(hours=6)
|
||||
|
||||
def test_rdivmod_offset(self):
|
||||
result = divmod(pd.offsets.Hour(54), Timedelta(hours=-4))
|
||||
assert result[0] == -14
|
||||
assert isinstance(result[1], Timedelta)
|
||||
assert result[1] == Timedelta(hours=-2)
|
||||
|
||||
def test_rdivmod_invalid(self):
|
||||
# GH#19365
|
||||
td = Timedelta(minutes=3)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
divmod(Timestamp("2018-01-22"), td)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
divmod(15, td)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
divmod(16.0, td)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
divmod(np.array([22, 24]), td)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"op", [operator.mul, ops.rmul, operator.truediv, ops.rdiv, ops.rsub]
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"arr",
|
||||
[
|
||||
np.array([Timestamp("20130101 9:01"), Timestamp("20121230 9:02")]),
|
||||
np.array([Timestamp.now(), Timedelta("1D")]),
|
||||
],
|
||||
)
|
||||
def test_td_op_timedelta_timedeltalike_array(self, op, arr):
|
||||
with pytest.raises(TypeError):
|
||||
op(arr, Timedelta("1D"))
|
@@ -0,0 +1,276 @@
|
||||
from datetime import timedelta
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import Timedelta, offsets, to_timedelta
|
||||
|
||||
|
||||
def test_construction():
|
||||
expected = np.timedelta64(10, "D").astype("m8[ns]").view("i8")
|
||||
assert Timedelta(10, unit="d").value == expected
|
||||
assert Timedelta(10.0, unit="d").value == expected
|
||||
assert Timedelta("10 days").value == expected
|
||||
assert Timedelta(days=10).value == expected
|
||||
assert Timedelta(days=10.0).value == expected
|
||||
|
||||
expected += np.timedelta64(10, "s").astype("m8[ns]").view("i8")
|
||||
assert Timedelta("10 days 00:00:10").value == expected
|
||||
assert Timedelta(days=10, seconds=10).value == expected
|
||||
assert Timedelta(days=10, milliseconds=10 * 1000).value == expected
|
||||
assert Timedelta(days=10, microseconds=10 * 1000 * 1000).value == expected
|
||||
|
||||
# rounding cases
|
||||
assert Timedelta(82739999850000).value == 82739999850000
|
||||
assert "0 days 22:58:59.999850" in str(Timedelta(82739999850000))
|
||||
assert Timedelta(123072001000000).value == 123072001000000
|
||||
assert "1 days 10:11:12.001" in str(Timedelta(123072001000000))
|
||||
|
||||
# string conversion with/without leading zero
|
||||
# GH#9570
|
||||
assert Timedelta("0:00:00") == timedelta(hours=0)
|
||||
assert Timedelta("00:00:00") == timedelta(hours=0)
|
||||
assert Timedelta("-1:00:00") == -timedelta(hours=1)
|
||||
assert Timedelta("-01:00:00") == -timedelta(hours=1)
|
||||
|
||||
# more strings & abbrevs
|
||||
# GH#8190
|
||||
assert Timedelta("1 h") == timedelta(hours=1)
|
||||
assert Timedelta("1 hour") == timedelta(hours=1)
|
||||
assert Timedelta("1 hr") == timedelta(hours=1)
|
||||
assert Timedelta("1 hours") == timedelta(hours=1)
|
||||
assert Timedelta("-1 hours") == -timedelta(hours=1)
|
||||
assert Timedelta("1 m") == timedelta(minutes=1)
|
||||
assert Timedelta("1.5 m") == timedelta(seconds=90)
|
||||
assert Timedelta("1 minute") == timedelta(minutes=1)
|
||||
assert Timedelta("1 minutes") == timedelta(minutes=1)
|
||||
assert Timedelta("1 s") == timedelta(seconds=1)
|
||||
assert Timedelta("1 second") == timedelta(seconds=1)
|
||||
assert Timedelta("1 seconds") == timedelta(seconds=1)
|
||||
assert Timedelta("1 ms") == timedelta(milliseconds=1)
|
||||
assert Timedelta("1 milli") == timedelta(milliseconds=1)
|
||||
assert Timedelta("1 millisecond") == timedelta(milliseconds=1)
|
||||
assert Timedelta("1 us") == timedelta(microseconds=1)
|
||||
assert Timedelta("1 micros") == timedelta(microseconds=1)
|
||||
assert Timedelta("1 microsecond") == timedelta(microseconds=1)
|
||||
assert Timedelta("1.5 microsecond") == Timedelta("00:00:00.000001500")
|
||||
assert Timedelta("1 ns") == Timedelta("00:00:00.000000001")
|
||||
assert Timedelta("1 nano") == Timedelta("00:00:00.000000001")
|
||||
assert Timedelta("1 nanosecond") == Timedelta("00:00:00.000000001")
|
||||
|
||||
# combos
|
||||
assert Timedelta("10 days 1 hour") == timedelta(days=10, hours=1)
|
||||
assert Timedelta("10 days 1 h") == timedelta(days=10, hours=1)
|
||||
assert Timedelta("10 days 1 h 1m 1s") == timedelta(
|
||||
days=10, hours=1, minutes=1, seconds=1
|
||||
)
|
||||
assert Timedelta("-10 days 1 h 1m 1s") == -timedelta(
|
||||
days=10, hours=1, minutes=1, seconds=1
|
||||
)
|
||||
assert Timedelta("-10 days 1 h 1m 1s") == -timedelta(
|
||||
days=10, hours=1, minutes=1, seconds=1
|
||||
)
|
||||
assert Timedelta("-10 days 1 h 1m 1s 3us") == -timedelta(
|
||||
days=10, hours=1, minutes=1, seconds=1, microseconds=3
|
||||
)
|
||||
assert Timedelta("-10 days 1 h 1.5m 1s 3us") == -timedelta(
|
||||
days=10, hours=1, minutes=1, seconds=31, microseconds=3
|
||||
)
|
||||
|
||||
# Currently invalid as it has a - on the hh:mm:dd part
|
||||
# (only allowed on the days)
|
||||
with pytest.raises(ValueError):
|
||||
Timedelta("-10 days -1 h 1.5m 1s 3us")
|
||||
|
||||
# only leading neg signs are allowed
|
||||
with pytest.raises(ValueError):
|
||||
Timedelta("10 days -1 h 1.5m 1s 3us")
|
||||
|
||||
# no units specified
|
||||
with pytest.raises(ValueError):
|
||||
Timedelta("3.1415")
|
||||
|
||||
# invalid construction
|
||||
with pytest.raises(ValueError, match="cannot construct a Timedelta"):
|
||||
Timedelta()
|
||||
|
||||
with pytest.raises(ValueError, match="unit abbreviation w/o a number"):
|
||||
Timedelta("foo")
|
||||
|
||||
msg = (
|
||||
"cannot construct a Timedelta from "
|
||||
"the passed arguments, allowed keywords are "
|
||||
)
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
Timedelta(day=10)
|
||||
|
||||
# floats
|
||||
expected = np.timedelta64(10, "s").astype("m8[ns]").view("i8") + np.timedelta64(
|
||||
500, "ms"
|
||||
).astype("m8[ns]").view("i8")
|
||||
assert Timedelta(10.5, unit="s").value == expected
|
||||
|
||||
# offset
|
||||
assert to_timedelta(offsets.Hour(2)) == Timedelta(hours=2)
|
||||
assert Timedelta(offsets.Hour(2)) == Timedelta(hours=2)
|
||||
assert Timedelta(offsets.Second(2)) == Timedelta(seconds=2)
|
||||
|
||||
# GH#11995: unicode
|
||||
expected = Timedelta("1H")
|
||||
result = Timedelta("1H")
|
||||
assert result == expected
|
||||
assert to_timedelta(offsets.Hour(2)) == Timedelta("0 days, 02:00:00")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Timedelta("foo bar")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"item",
|
||||
list(
|
||||
{
|
||||
"days": "D",
|
||||
"seconds": "s",
|
||||
"microseconds": "us",
|
||||
"milliseconds": "ms",
|
||||
"minutes": "m",
|
||||
"hours": "h",
|
||||
"weeks": "W",
|
||||
}.items()
|
||||
),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"npdtype", [np.int64, np.int32, np.int16, np.float64, np.float32, np.float16]
|
||||
)
|
||||
def test_td_construction_with_np_dtypes(npdtype, item):
|
||||
# GH#8757: test construction with np dtypes
|
||||
pykwarg, npkwarg = item
|
||||
expected = np.timedelta64(1, npkwarg).astype("m8[ns]").view("i8")
|
||||
assert Timedelta(**{pykwarg: npdtype(1)}).value == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"val",
|
||||
[
|
||||
"1s",
|
||||
"-1s",
|
||||
"1us",
|
||||
"-1us",
|
||||
"1 day",
|
||||
"-1 day",
|
||||
"-23:59:59.999999",
|
||||
"-1 days +23:59:59.999999",
|
||||
"-1ns",
|
||||
"1ns",
|
||||
"-23:59:59.999999999",
|
||||
],
|
||||
)
|
||||
def test_td_from_repr_roundtrip(val):
|
||||
# round-trip both for string and value
|
||||
td = Timedelta(val)
|
||||
assert Timedelta(td.value) == td
|
||||
|
||||
# str does not normally display nanos
|
||||
if not td.nanoseconds:
|
||||
assert Timedelta(str(td)) == td
|
||||
assert Timedelta(td._repr_base(format="all")) == td
|
||||
|
||||
|
||||
def test_overflow_on_construction():
|
||||
# GH#3374
|
||||
value = Timedelta("1day").value * 20169940
|
||||
with pytest.raises(OverflowError):
|
||||
Timedelta(value)
|
||||
|
||||
# xref GH#17637
|
||||
with pytest.raises(OverflowError):
|
||||
Timedelta(7 * 19999, unit="D")
|
||||
|
||||
with pytest.raises(OverflowError):
|
||||
Timedelta(timedelta(days=13 * 19999))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fmt,exp",
|
||||
[
|
||||
(
|
||||
"P6DT0H50M3.010010012S",
|
||||
Timedelta(
|
||||
days=6,
|
||||
minutes=50,
|
||||
seconds=3,
|
||||
milliseconds=10,
|
||||
microseconds=10,
|
||||
nanoseconds=12,
|
||||
),
|
||||
),
|
||||
(
|
||||
"P-6DT0H50M3.010010012S",
|
||||
Timedelta(
|
||||
days=-6,
|
||||
minutes=50,
|
||||
seconds=3,
|
||||
milliseconds=10,
|
||||
microseconds=10,
|
||||
nanoseconds=12,
|
||||
),
|
||||
),
|
||||
("P4DT12H30M5S", Timedelta(days=4, hours=12, minutes=30, seconds=5)),
|
||||
("P0DT0H0M0.000000123S", Timedelta(nanoseconds=123)),
|
||||
("P0DT0H0M0.00001S", Timedelta(microseconds=10)),
|
||||
("P0DT0H0M0.001S", Timedelta(milliseconds=1)),
|
||||
("P0DT0H1M0S", Timedelta(minutes=1)),
|
||||
("P1DT25H61M61S", Timedelta(days=1, hours=25, minutes=61, seconds=61)),
|
||||
],
|
||||
)
|
||||
def test_iso_constructor(fmt, exp):
|
||||
assert Timedelta(fmt) == exp
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fmt",
|
||||
[
|
||||
"PPPPPPPPPPPP",
|
||||
"PDTHMS",
|
||||
"P0DT999H999M999S",
|
||||
"P1DT0H0M0.0000000000000S",
|
||||
"P1DT0H0M00000000000S",
|
||||
"P1DT0H0M0.S",
|
||||
],
|
||||
)
|
||||
def test_iso_constructor_raises(fmt):
|
||||
msg = "Invalid ISO 8601 Duration format - {}".format(fmt)
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
Timedelta(fmt)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"constructed_td, conversion",
|
||||
[
|
||||
(Timedelta(nanoseconds=100), "100ns"),
|
||||
(
|
||||
Timedelta(
|
||||
days=1,
|
||||
hours=1,
|
||||
minutes=1,
|
||||
weeks=1,
|
||||
seconds=1,
|
||||
milliseconds=1,
|
||||
microseconds=1,
|
||||
nanoseconds=1,
|
||||
),
|
||||
694861001001001,
|
||||
),
|
||||
(Timedelta(microseconds=1) + Timedelta(nanoseconds=1), "1us1ns"),
|
||||
(Timedelta(microseconds=1) - Timedelta(nanoseconds=1), "999ns"),
|
||||
(Timedelta(microseconds=1) + 5 * Timedelta(nanoseconds=-2), "990ns"),
|
||||
],
|
||||
)
|
||||
def test_td_constructor_on_nanoseconds(constructed_td, conversion):
|
||||
# GH#9273
|
||||
assert constructed_td == Timedelta(conversion)
|
||||
|
||||
|
||||
def test_td_constructor_value_error():
|
||||
with pytest.raises(TypeError):
|
||||
Timedelta(nanoseconds="abc")
|
@@ -0,0 +1,44 @@
|
||||
import pytest
|
||||
|
||||
from pandas import Timedelta
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"td, expected_repr",
|
||||
[
|
||||
(Timedelta(10, unit="d"), "Timedelta('10 days 00:00:00')"),
|
||||
(Timedelta(10, unit="s"), "Timedelta('0 days 00:00:10')"),
|
||||
(Timedelta(10, unit="ms"), "Timedelta('0 days 00:00:00.010000')"),
|
||||
(Timedelta(-10, unit="ms"), "Timedelta('-1 days +23:59:59.990000')"),
|
||||
],
|
||||
)
|
||||
def test_repr(td, expected_repr):
|
||||
assert repr(td) == expected_repr
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"td, expected_iso",
|
||||
[
|
||||
(
|
||||
Timedelta(
|
||||
days=6,
|
||||
minutes=50,
|
||||
seconds=3,
|
||||
milliseconds=10,
|
||||
microseconds=10,
|
||||
nanoseconds=12,
|
||||
),
|
||||
"P6DT0H50M3.010010012S",
|
||||
),
|
||||
(Timedelta(days=4, hours=12, minutes=30, seconds=5), "P4DT12H30M5S"),
|
||||
(Timedelta(nanoseconds=123), "P0DT0H0M0.000000123S"),
|
||||
# trim nano
|
||||
(Timedelta(microseconds=10), "P0DT0H0M0.00001S"),
|
||||
# trim micro
|
||||
(Timedelta(milliseconds=1), "P0DT0H0M0.001S"),
|
||||
# don't strip every 0
|
||||
(Timedelta(minutes=1), "P0DT0H1M0S"),
|
||||
],
|
||||
)
|
||||
def test_isoformat(td, expected_iso):
|
||||
assert td.isoformat() == expected_iso
|
@@ -0,0 +1,832 @@
|
||||
""" test the scalar Timedelta """
|
||||
from datetime import timedelta
|
||||
import re
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas._libs.tslibs import NaT, iNaT
|
||||
|
||||
import pandas as pd
|
||||
from pandas import Series, Timedelta, TimedeltaIndex, timedelta_range, to_timedelta
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
class TestTimedeltaArithmetic:
|
||||
def test_arithmetic_overflow(self):
|
||||
with pytest.raises(OverflowError):
|
||||
pd.Timestamp("1700-01-01") + pd.Timedelta(13 * 19999, unit="D")
|
||||
|
||||
with pytest.raises(OverflowError):
|
||||
pd.Timestamp("1700-01-01") + timedelta(days=13 * 19999)
|
||||
|
||||
def test_array_timedelta_floordiv(self):
|
||||
# https://github.com/pandas-dev/pandas/issues/19761
|
||||
ints = pd.date_range("2012-10-08", periods=4, freq="D").view("i8")
|
||||
msg = r"Use 'array // timedelta.value'"
|
||||
with tm.assert_produces_warning(FutureWarning) as m:
|
||||
result = ints // pd.Timedelta(1, unit="s")
|
||||
|
||||
assert msg in str(m[0].message)
|
||||
expected = np.array(
|
||||
[1349654400, 1349740800, 1349827200, 1349913600], dtype="i8"
|
||||
)
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
def test_ops_error_str(self):
|
||||
# GH 13624
|
||||
td = Timedelta("1 day")
|
||||
|
||||
for left, right in [(td, "a"), ("a", td)]:
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
left + right
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
left > right
|
||||
|
||||
assert not left == right
|
||||
assert left != right
|
||||
|
||||
def test_ops_notimplemented(self):
|
||||
class Other:
|
||||
pass
|
||||
|
||||
other = Other()
|
||||
|
||||
td = Timedelta("1 day")
|
||||
assert td.__add__(other) is NotImplemented
|
||||
assert td.__sub__(other) is NotImplemented
|
||||
assert td.__truediv__(other) is NotImplemented
|
||||
assert td.__mul__(other) is NotImplemented
|
||||
assert td.__floordiv__(other) is NotImplemented
|
||||
|
||||
def test_unary_ops(self):
|
||||
td = Timedelta(10, unit="d")
|
||||
|
||||
# __neg__, __pos__
|
||||
assert -td == Timedelta(-10, unit="d")
|
||||
assert -td == Timedelta("-10d")
|
||||
assert +td == Timedelta(10, unit="d")
|
||||
|
||||
# __abs__, __abs__(__neg__)
|
||||
assert abs(td) == td
|
||||
assert abs(-td) == td
|
||||
assert abs(-td) == Timedelta("10d")
|
||||
|
||||
|
||||
class TestTimedeltaComparison:
|
||||
def test_compare_tick(self, tick_classes):
|
||||
cls = tick_classes
|
||||
|
||||
off = cls(4)
|
||||
td = off.delta
|
||||
assert isinstance(td, Timedelta)
|
||||
|
||||
assert td == off
|
||||
assert not td != off
|
||||
assert td <= off
|
||||
assert td >= off
|
||||
assert not td < off
|
||||
assert not td > off
|
||||
|
||||
assert not td == 2 * off
|
||||
assert td != 2 * off
|
||||
assert td <= 2 * off
|
||||
assert td < 2 * off
|
||||
assert not td >= 2 * off
|
||||
assert not td > 2 * off
|
||||
|
||||
def test_comparison_object_array(self):
|
||||
# analogous to GH#15183
|
||||
td = Timedelta("2 days")
|
||||
other = Timedelta("3 hours")
|
||||
|
||||
arr = np.array([other, td], dtype=object)
|
||||
res = arr == td
|
||||
expected = np.array([False, True], dtype=bool)
|
||||
assert (res == expected).all()
|
||||
|
||||
# 2D case
|
||||
arr = np.array([[other, td], [td, other]], dtype=object)
|
||||
res = arr != td
|
||||
expected = np.array([[True, False], [False, True]], dtype=bool)
|
||||
assert res.shape == expected.shape
|
||||
assert (res == expected).all()
|
||||
|
||||
def test_compare_timedelta_ndarray(self):
|
||||
# GH11835
|
||||
periods = [Timedelta("0 days 01:00:00"), Timedelta("0 days 01:00:00")]
|
||||
arr = np.array(periods)
|
||||
result = arr[0] > arr
|
||||
expected = np.array([False, False])
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
@pytest.mark.skip(reason="GH#20829 is reverted until after 0.24.0")
|
||||
def test_compare_custom_object(self):
|
||||
"""
|
||||
Make sure non supported operations on Timedelta returns NonImplemented
|
||||
and yields to other operand (GH#20829).
|
||||
"""
|
||||
|
||||
class CustomClass:
|
||||
def __init__(self, cmp_result=None):
|
||||
self.cmp_result = cmp_result
|
||||
|
||||
def generic_result(self):
|
||||
if self.cmp_result is None:
|
||||
return NotImplemented
|
||||
else:
|
||||
return self.cmp_result
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.generic_result()
|
||||
|
||||
def __gt__(self, other):
|
||||
return self.generic_result()
|
||||
|
||||
t = Timedelta("1s")
|
||||
|
||||
assert not (t == "string")
|
||||
assert not (t == 1)
|
||||
assert not (t == CustomClass())
|
||||
assert not (t == CustomClass(cmp_result=False))
|
||||
|
||||
assert t < CustomClass(cmp_result=True)
|
||||
assert not (t < CustomClass(cmp_result=False))
|
||||
|
||||
assert t == CustomClass(cmp_result=True)
|
||||
|
||||
@pytest.mark.parametrize("val", ["string", 1])
|
||||
def test_compare_unknown_type(self, val):
|
||||
# GH20829
|
||||
t = Timedelta("1s")
|
||||
with pytest.raises(TypeError):
|
||||
t >= val
|
||||
with pytest.raises(TypeError):
|
||||
t > val
|
||||
with pytest.raises(TypeError):
|
||||
t <= val
|
||||
with pytest.raises(TypeError):
|
||||
t < val
|
||||
|
||||
|
||||
class TestTimedeltas:
|
||||
@pytest.mark.parametrize(
|
||||
"unit, value, expected",
|
||||
[
|
||||
("us", 9.999, 9999),
|
||||
("ms", 9.999999, 9999999),
|
||||
("s", 9.999999999, 9999999999),
|
||||
],
|
||||
)
|
||||
def test_rounding_on_int_unit_construction(self, unit, value, expected):
|
||||
# GH 12690
|
||||
result = Timedelta(value, unit=unit)
|
||||
assert result.value == expected
|
||||
result = Timedelta(str(value) + unit)
|
||||
assert result.value == expected
|
||||
|
||||
def test_total_seconds_scalar(self):
|
||||
# see gh-10939
|
||||
rng = Timedelta("1 days, 10:11:12.100123456")
|
||||
expt = 1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456.0 / 1e9
|
||||
tm.assert_almost_equal(rng.total_seconds(), expt)
|
||||
|
||||
rng = Timedelta(np.nan)
|
||||
assert np.isnan(rng.total_seconds())
|
||||
|
||||
def test_conversion(self):
|
||||
|
||||
for td in [Timedelta(10, unit="d"), Timedelta("1 days, 10:11:12.012345")]:
|
||||
pydt = td.to_pytimedelta()
|
||||
assert td == Timedelta(pydt)
|
||||
assert td == pydt
|
||||
assert isinstance(pydt, timedelta) and not isinstance(pydt, Timedelta)
|
||||
|
||||
assert td == np.timedelta64(td.value, "ns")
|
||||
td64 = td.to_timedelta64()
|
||||
|
||||
assert td64 == np.timedelta64(td.value, "ns")
|
||||
assert td == td64
|
||||
|
||||
assert isinstance(td64, np.timedelta64)
|
||||
|
||||
# this is NOT equal and cannot be roundtripped (because of the nanos)
|
||||
td = Timedelta("1 days, 10:11:12.012345678")
|
||||
assert td != td.to_pytimedelta()
|
||||
|
||||
def test_freq_conversion(self):
|
||||
|
||||
# truediv
|
||||
td = Timedelta("1 days 2 hours 3 ns")
|
||||
result = td / np.timedelta64(1, "D")
|
||||
assert result == td.value / float(86400 * 1e9)
|
||||
result = td / np.timedelta64(1, "s")
|
||||
assert result == td.value / float(1e9)
|
||||
result = td / np.timedelta64(1, "ns")
|
||||
assert result == td.value
|
||||
|
||||
# floordiv
|
||||
td = Timedelta("1 days 2 hours 3 ns")
|
||||
result = td // np.timedelta64(1, "D")
|
||||
assert result == 1
|
||||
result = td // np.timedelta64(1, "s")
|
||||
assert result == 93600
|
||||
result = td // np.timedelta64(1, "ns")
|
||||
assert result == td.value
|
||||
|
||||
def test_fields(self):
|
||||
def check(value):
|
||||
# that we are int
|
||||
assert isinstance(value, int)
|
||||
|
||||
# compat to datetime.timedelta
|
||||
rng = to_timedelta("1 days, 10:11:12")
|
||||
assert rng.days == 1
|
||||
assert rng.seconds == 10 * 3600 + 11 * 60 + 12
|
||||
assert rng.microseconds == 0
|
||||
assert rng.nanoseconds == 0
|
||||
|
||||
msg = "'Timedelta' object has no attribute '{}'"
|
||||
with pytest.raises(AttributeError, match=msg.format("hours")):
|
||||
rng.hours
|
||||
with pytest.raises(AttributeError, match=msg.format("minutes")):
|
||||
rng.minutes
|
||||
with pytest.raises(AttributeError, match=msg.format("milliseconds")):
|
||||
rng.milliseconds
|
||||
|
||||
# GH 10050
|
||||
check(rng.days)
|
||||
check(rng.seconds)
|
||||
check(rng.microseconds)
|
||||
check(rng.nanoseconds)
|
||||
|
||||
td = Timedelta("-1 days, 10:11:12")
|
||||
assert abs(td) == Timedelta("13:48:48")
|
||||
assert str(td) == "-1 days +10:11:12"
|
||||
assert -td == Timedelta("0 days 13:48:48")
|
||||
assert -Timedelta("-1 days, 10:11:12").value == 49728000000000
|
||||
assert Timedelta("-1 days, 10:11:12").value == -49728000000000
|
||||
|
||||
rng = to_timedelta("-1 days, 10:11:12.100123456")
|
||||
assert rng.days == -1
|
||||
assert rng.seconds == 10 * 3600 + 11 * 60 + 12
|
||||
assert rng.microseconds == 100 * 1000 + 123
|
||||
assert rng.nanoseconds == 456
|
||||
msg = "'Timedelta' object has no attribute '{}'"
|
||||
with pytest.raises(AttributeError, match=msg.format("hours")):
|
||||
rng.hours
|
||||
with pytest.raises(AttributeError, match=msg.format("minutes")):
|
||||
rng.minutes
|
||||
with pytest.raises(AttributeError, match=msg.format("milliseconds")):
|
||||
rng.milliseconds
|
||||
|
||||
# components
|
||||
tup = pd.to_timedelta(-1, "us").components
|
||||
assert tup.days == -1
|
||||
assert tup.hours == 23
|
||||
assert tup.minutes == 59
|
||||
assert tup.seconds == 59
|
||||
assert tup.milliseconds == 999
|
||||
assert tup.microseconds == 999
|
||||
assert tup.nanoseconds == 0
|
||||
|
||||
# GH 10050
|
||||
check(tup.days)
|
||||
check(tup.hours)
|
||||
check(tup.minutes)
|
||||
check(tup.seconds)
|
||||
check(tup.milliseconds)
|
||||
check(tup.microseconds)
|
||||
check(tup.nanoseconds)
|
||||
|
||||
tup = Timedelta("-1 days 1 us").components
|
||||
assert tup.days == -2
|
||||
assert tup.hours == 23
|
||||
assert tup.minutes == 59
|
||||
assert tup.seconds == 59
|
||||
assert tup.milliseconds == 999
|
||||
assert tup.microseconds == 999
|
||||
assert tup.nanoseconds == 0
|
||||
|
||||
def test_iso_conversion(self):
|
||||
# GH #21877
|
||||
expected = Timedelta(1, unit="s")
|
||||
assert to_timedelta("P0DT0H0M1S") == expected
|
||||
|
||||
def test_nat_converters(self):
|
||||
result = to_timedelta("nat").to_numpy()
|
||||
assert result.dtype.kind == "M"
|
||||
assert result.astype("int64") == iNaT
|
||||
|
||||
result = to_timedelta("nan").to_numpy()
|
||||
assert result.dtype.kind == "M"
|
||||
assert result.astype("int64") == iNaT
|
||||
|
||||
@pytest.mark.filterwarnings("ignore:M and Y units are deprecated")
|
||||
@pytest.mark.parametrize(
|
||||
"units, np_unit",
|
||||
[
|
||||
(["Y", "y"], "Y"),
|
||||
(["M"], "M"),
|
||||
(["W", "w"], "W"),
|
||||
(["D", "d", "days", "day", "Days", "Day"], "D"),
|
||||
(
|
||||
["m", "minute", "min", "minutes", "t", "Minute", "Min", "Minutes", "T"],
|
||||
"m",
|
||||
),
|
||||
(["s", "seconds", "sec", "second", "S", "Seconds", "Sec", "Second"], "s"),
|
||||
(
|
||||
[
|
||||
"ms",
|
||||
"milliseconds",
|
||||
"millisecond",
|
||||
"milli",
|
||||
"millis",
|
||||
"l",
|
||||
"MS",
|
||||
"Milliseconds",
|
||||
"Millisecond",
|
||||
"Milli",
|
||||
"Millis",
|
||||
"L",
|
||||
],
|
||||
"ms",
|
||||
),
|
||||
(
|
||||
[
|
||||
"us",
|
||||
"microseconds",
|
||||
"microsecond",
|
||||
"micro",
|
||||
"micros",
|
||||
"u",
|
||||
"US",
|
||||
"Microseconds",
|
||||
"Microsecond",
|
||||
"Micro",
|
||||
"Micros",
|
||||
"U",
|
||||
],
|
||||
"us",
|
||||
),
|
||||
(
|
||||
[
|
||||
"ns",
|
||||
"nanoseconds",
|
||||
"nanosecond",
|
||||
"nano",
|
||||
"nanos",
|
||||
"n",
|
||||
"NS",
|
||||
"Nanoseconds",
|
||||
"Nanosecond",
|
||||
"Nano",
|
||||
"Nanos",
|
||||
"N",
|
||||
],
|
||||
"ns",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("wrapper", [np.array, list, pd.Index])
|
||||
def test_unit_parser(self, units, np_unit, wrapper):
|
||||
# validate all units, GH 6855, GH 21762
|
||||
for unit in units:
|
||||
# array-likes
|
||||
expected = TimedeltaIndex(
|
||||
[np.timedelta64(i, np_unit) for i in np.arange(5).tolist()]
|
||||
)
|
||||
result = to_timedelta(wrapper(range(5)), unit=unit)
|
||||
tm.assert_index_equal(result, expected)
|
||||
result = TimedeltaIndex(wrapper(range(5)), unit=unit)
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
if unit == "M":
|
||||
# M is treated as minutes in string repr
|
||||
expected = TimedeltaIndex(
|
||||
[np.timedelta64(i, "m") for i in np.arange(5).tolist()]
|
||||
)
|
||||
|
||||
str_repr = ["{}{}".format(x, unit) for x in np.arange(5)]
|
||||
result = to_timedelta(wrapper(str_repr))
|
||||
tm.assert_index_equal(result, expected)
|
||||
result = TimedeltaIndex(wrapper(str_repr))
|
||||
tm.assert_index_equal(result, expected)
|
||||
|
||||
# scalar
|
||||
expected = Timedelta(np.timedelta64(2, np_unit).astype("timedelta64[ns]"))
|
||||
|
||||
result = to_timedelta(2, unit=unit)
|
||||
assert result == expected
|
||||
result = Timedelta(2, unit=unit)
|
||||
assert result == expected
|
||||
|
||||
if unit == "M":
|
||||
expected = Timedelta(np.timedelta64(2, "m").astype("timedelta64[ns]"))
|
||||
|
||||
result = to_timedelta("2{}".format(unit))
|
||||
assert result == expected
|
||||
result = Timedelta("2{}".format(unit))
|
||||
assert result == expected
|
||||
|
||||
@pytest.mark.parametrize("unit", ["Y", "y", "M"])
|
||||
def test_unit_m_y_deprecated(self, unit):
|
||||
with tm.assert_produces_warning(FutureWarning) as w1:
|
||||
Timedelta(10, unit)
|
||||
msg = r".* units are deprecated .*"
|
||||
assert re.match(msg, str(w1[0].message))
|
||||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False) as w2:
|
||||
to_timedelta(10, unit)
|
||||
msg = r".* units are deprecated .*"
|
||||
assert re.match(msg, str(w2[0].message))
|
||||
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False) as w3:
|
||||
to_timedelta([1, 2], unit)
|
||||
msg = r".* units are deprecated .*"
|
||||
assert re.match(msg, str(w3[0].message))
|
||||
|
||||
def test_numeric_conversions(self):
|
||||
assert Timedelta(0) == np.timedelta64(0, "ns")
|
||||
assert Timedelta(10) == np.timedelta64(10, "ns")
|
||||
assert Timedelta(10, unit="ns") == np.timedelta64(10, "ns")
|
||||
|
||||
assert Timedelta(10, unit="us") == np.timedelta64(10, "us")
|
||||
assert Timedelta(10, unit="ms") == np.timedelta64(10, "ms")
|
||||
assert Timedelta(10, unit="s") == np.timedelta64(10, "s")
|
||||
assert Timedelta(10, unit="d") == np.timedelta64(10, "D")
|
||||
|
||||
def test_timedelta_conversions(self):
|
||||
assert Timedelta(timedelta(seconds=1)) == np.timedelta64(1, "s").astype(
|
||||
"m8[ns]"
|
||||
)
|
||||
assert Timedelta(timedelta(microseconds=1)) == np.timedelta64(1, "us").astype(
|
||||
"m8[ns]"
|
||||
)
|
||||
assert Timedelta(timedelta(days=1)) == np.timedelta64(1, "D").astype("m8[ns]")
|
||||
|
||||
def test_to_numpy_alias(self):
|
||||
# GH 24653: alias .to_numpy() for scalars
|
||||
td = Timedelta("10m7s")
|
||||
assert td.to_timedelta64() == td.to_numpy()
|
||||
|
||||
def test_round(self):
|
||||
|
||||
t1 = Timedelta("1 days 02:34:56.789123456")
|
||||
t2 = Timedelta("-1 days 02:34:56.789123456")
|
||||
|
||||
for (freq, s1, s2) in [
|
||||
("N", t1, t2),
|
||||
(
|
||||
"U",
|
||||
Timedelta("1 days 02:34:56.789123000"),
|
||||
Timedelta("-1 days 02:34:56.789123000"),
|
||||
),
|
||||
(
|
||||
"L",
|
||||
Timedelta("1 days 02:34:56.789000000"),
|
||||
Timedelta("-1 days 02:34:56.789000000"),
|
||||
),
|
||||
("S", Timedelta("1 days 02:34:57"), Timedelta("-1 days 02:34:57")),
|
||||
("2S", Timedelta("1 days 02:34:56"), Timedelta("-1 days 02:34:56")),
|
||||
("5S", Timedelta("1 days 02:34:55"), Timedelta("-1 days 02:34:55")),
|
||||
("T", Timedelta("1 days 02:35:00"), Timedelta("-1 days 02:35:00")),
|
||||
("12T", Timedelta("1 days 02:36:00"), Timedelta("-1 days 02:36:00")),
|
||||
("H", Timedelta("1 days 03:00:00"), Timedelta("-1 days 03:00:00")),
|
||||
("d", Timedelta("1 days"), Timedelta("-1 days")),
|
||||
]:
|
||||
r1 = t1.round(freq)
|
||||
assert r1 == s1
|
||||
r2 = t2.round(freq)
|
||||
assert r2 == s2
|
||||
|
||||
# invalid
|
||||
for freq, msg in [
|
||||
("Y", "<YearEnd: month=12> is a non-fixed frequency"),
|
||||
("M", "<MonthEnd> is a non-fixed frequency"),
|
||||
("foobar", "Invalid frequency: foobar"),
|
||||
]:
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
t1.round(freq)
|
||||
|
||||
t1 = timedelta_range("1 days", periods=3, freq="1 min 2 s 3 us")
|
||||
t2 = -1 * t1
|
||||
t1a = timedelta_range("1 days", periods=3, freq="1 min 2 s")
|
||||
t1c = pd.TimedeltaIndex([1, 1, 1], unit="D")
|
||||
|
||||
# note that negative times round DOWN! so don't give whole numbers
|
||||
for (freq, s1, s2) in [
|
||||
("N", t1, t2),
|
||||
("U", t1, t2),
|
||||
(
|
||||
"L",
|
||||
t1a,
|
||||
TimedeltaIndex(
|
||||
["-1 days +00:00:00", "-2 days +23:58:58", "-2 days +23:57:56"],
|
||||
dtype="timedelta64[ns]",
|
||||
freq=None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"S",
|
||||
t1a,
|
||||
TimedeltaIndex(
|
||||
["-1 days +00:00:00", "-2 days +23:58:58", "-2 days +23:57:56"],
|
||||
dtype="timedelta64[ns]",
|
||||
freq=None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"12T",
|
||||
t1c,
|
||||
TimedeltaIndex(
|
||||
["-1 days", "-1 days", "-1 days"],
|
||||
dtype="timedelta64[ns]",
|
||||
freq=None,
|
||||
),
|
||||
),
|
||||
(
|
||||
"H",
|
||||
t1c,
|
||||
TimedeltaIndex(
|
||||
["-1 days", "-1 days", "-1 days"],
|
||||
dtype="timedelta64[ns]",
|
||||
freq=None,
|
||||
),
|
||||
),
|
||||
("d", t1c, pd.TimedeltaIndex([-1, -1, -1], unit="D")),
|
||||
]:
|
||||
|
||||
r1 = t1.round(freq)
|
||||
tm.assert_index_equal(r1, s1)
|
||||
r2 = t2.round(freq)
|
||||
tm.assert_index_equal(r2, s2)
|
||||
|
||||
# invalid
|
||||
for freq, msg in [
|
||||
("Y", "<YearEnd: month=12> is a non-fixed frequency"),
|
||||
("M", "<MonthEnd> is a non-fixed frequency"),
|
||||
("foobar", "Invalid frequency: foobar"),
|
||||
]:
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
t1.round(freq)
|
||||
|
||||
def test_contains(self):
|
||||
# Checking for any NaT-like objects
|
||||
# GH 13603
|
||||
td = to_timedelta(range(5), unit="d") + pd.offsets.Hour(1)
|
||||
for v in [pd.NaT, None, float("nan"), np.nan]:
|
||||
assert not (v in td)
|
||||
|
||||
td = to_timedelta([pd.NaT])
|
||||
for v in [pd.NaT, None, float("nan"), np.nan]:
|
||||
assert v in td
|
||||
|
||||
def test_identity(self):
|
||||
|
||||
td = Timedelta(10, unit="d")
|
||||
assert isinstance(td, Timedelta)
|
||||
assert isinstance(td, timedelta)
|
||||
|
||||
def test_short_format_converters(self):
|
||||
def conv(v):
|
||||
return v.astype("m8[ns]")
|
||||
|
||||
assert Timedelta("10") == np.timedelta64(10, "ns")
|
||||
assert Timedelta("10ns") == np.timedelta64(10, "ns")
|
||||
assert Timedelta("100") == np.timedelta64(100, "ns")
|
||||
assert Timedelta("100ns") == np.timedelta64(100, "ns")
|
||||
|
||||
assert Timedelta("1000") == np.timedelta64(1000, "ns")
|
||||
assert Timedelta("1000ns") == np.timedelta64(1000, "ns")
|
||||
assert Timedelta("1000NS") == np.timedelta64(1000, "ns")
|
||||
|
||||
assert Timedelta("10us") == np.timedelta64(10000, "ns")
|
||||
assert Timedelta("100us") == np.timedelta64(100000, "ns")
|
||||
assert Timedelta("1000us") == np.timedelta64(1000000, "ns")
|
||||
assert Timedelta("1000Us") == np.timedelta64(1000000, "ns")
|
||||
assert Timedelta("1000uS") == np.timedelta64(1000000, "ns")
|
||||
|
||||
assert Timedelta("1ms") == np.timedelta64(1000000, "ns")
|
||||
assert Timedelta("10ms") == np.timedelta64(10000000, "ns")
|
||||
assert Timedelta("100ms") == np.timedelta64(100000000, "ns")
|
||||
assert Timedelta("1000ms") == np.timedelta64(1000000000, "ns")
|
||||
|
||||
assert Timedelta("-1s") == -np.timedelta64(1000000000, "ns")
|
||||
assert Timedelta("1s") == np.timedelta64(1000000000, "ns")
|
||||
assert Timedelta("10s") == np.timedelta64(10000000000, "ns")
|
||||
assert Timedelta("100s") == np.timedelta64(100000000000, "ns")
|
||||
assert Timedelta("1000s") == np.timedelta64(1000000000000, "ns")
|
||||
|
||||
assert Timedelta("1d") == conv(np.timedelta64(1, "D"))
|
||||
assert Timedelta("-1d") == -conv(np.timedelta64(1, "D"))
|
||||
assert Timedelta("1D") == conv(np.timedelta64(1, "D"))
|
||||
assert Timedelta("10D") == conv(np.timedelta64(10, "D"))
|
||||
assert Timedelta("100D") == conv(np.timedelta64(100, "D"))
|
||||
assert Timedelta("1000D") == conv(np.timedelta64(1000, "D"))
|
||||
assert Timedelta("10000D") == conv(np.timedelta64(10000, "D"))
|
||||
|
||||
# space
|
||||
assert Timedelta(" 10000D ") == conv(np.timedelta64(10000, "D"))
|
||||
assert Timedelta(" - 10000D ") == -conv(np.timedelta64(10000, "D"))
|
||||
|
||||
# invalid
|
||||
with pytest.raises(ValueError):
|
||||
Timedelta("1foo")
|
||||
with pytest.raises(ValueError):
|
||||
Timedelta("foo")
|
||||
|
||||
def test_full_format_converters(self):
|
||||
def conv(v):
|
||||
return v.astype("m8[ns]")
|
||||
|
||||
d1 = np.timedelta64(1, "D")
|
||||
|
||||
assert Timedelta("1days") == conv(d1)
|
||||
assert Timedelta("1days,") == conv(d1)
|
||||
assert Timedelta("- 1days,") == -conv(d1)
|
||||
|
||||
assert Timedelta("00:00:01") == conv(np.timedelta64(1, "s"))
|
||||
assert Timedelta("06:00:01") == conv(np.timedelta64(6 * 3600 + 1, "s"))
|
||||
assert Timedelta("06:00:01.0") == conv(np.timedelta64(6 * 3600 + 1, "s"))
|
||||
assert Timedelta("06:00:01.01") == conv(
|
||||
np.timedelta64(1000 * (6 * 3600 + 1) + 10, "ms")
|
||||
)
|
||||
|
||||
assert Timedelta("- 1days, 00:00:01") == conv(-d1 + np.timedelta64(1, "s"))
|
||||
assert Timedelta("1days, 06:00:01") == conv(
|
||||
d1 + np.timedelta64(6 * 3600 + 1, "s")
|
||||
)
|
||||
assert Timedelta("1days, 06:00:01.01") == conv(
|
||||
d1 + np.timedelta64(1000 * (6 * 3600 + 1) + 10, "ms")
|
||||
)
|
||||
|
||||
# invalid
|
||||
with pytest.raises(ValueError):
|
||||
Timedelta("- 1days, 00")
|
||||
|
||||
def test_overflow(self):
|
||||
# GH 9442
|
||||
s = Series(pd.date_range("20130101", periods=100000, freq="H"))
|
||||
s[0] += pd.Timedelta("1s 1ms")
|
||||
|
||||
# mean
|
||||
result = (s - s.min()).mean()
|
||||
expected = pd.Timedelta((pd.TimedeltaIndex((s - s.min())).asi8 / len(s)).sum())
|
||||
|
||||
# the computation is converted to float so
|
||||
# might be some loss of precision
|
||||
assert np.allclose(result.value / 1000, expected.value / 1000)
|
||||
|
||||
# sum
|
||||
msg = "overflow in timedelta operation"
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
(s - s.min()).sum()
|
||||
s1 = s[0:10000]
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
(s1 - s1.min()).sum()
|
||||
s2 = s[0:1000]
|
||||
result = (s2 - s2.min()).sum()
|
||||
|
||||
def test_pickle(self):
|
||||
|
||||
v = Timedelta("1 days 10:11:12.0123456")
|
||||
v_p = tm.round_trip_pickle(v)
|
||||
assert v == v_p
|
||||
|
||||
def test_timedelta_hash_equality(self):
|
||||
# GH 11129
|
||||
v = Timedelta(1, "D")
|
||||
td = timedelta(days=1)
|
||||
assert hash(v) == hash(td)
|
||||
|
||||
d = {td: 2}
|
||||
assert d[v] == 2
|
||||
|
||||
tds = timedelta_range("1 second", periods=20)
|
||||
assert all(hash(td) == hash(td.to_pytimedelta()) for td in tds)
|
||||
|
||||
# python timedeltas drop ns resolution
|
||||
ns_td = Timedelta(1, "ns")
|
||||
assert hash(ns_td) != hash(ns_td.to_pytimedelta())
|
||||
|
||||
def test_implementation_limits(self):
|
||||
min_td = Timedelta(Timedelta.min)
|
||||
max_td = Timedelta(Timedelta.max)
|
||||
|
||||
# GH 12727
|
||||
# timedelta limits correspond to int64 boundaries
|
||||
assert min_td.value == np.iinfo(np.int64).min + 1
|
||||
assert max_td.value == np.iinfo(np.int64).max
|
||||
|
||||
# Beyond lower limit, a NAT before the Overflow
|
||||
assert (min_td - Timedelta(1, "ns")) is NaT
|
||||
|
||||
with pytest.raises(OverflowError):
|
||||
min_td - Timedelta(2, "ns")
|
||||
|
||||
with pytest.raises(OverflowError):
|
||||
max_td + Timedelta(1, "ns")
|
||||
|
||||
# Same tests using the internal nanosecond values
|
||||
td = Timedelta(min_td.value - 1, "ns")
|
||||
assert td is NaT
|
||||
|
||||
with pytest.raises(OverflowError):
|
||||
Timedelta(min_td.value - 2, "ns")
|
||||
|
||||
with pytest.raises(OverflowError):
|
||||
Timedelta(max_td.value + 1, "ns")
|
||||
|
||||
def test_total_seconds_precision(self):
|
||||
# GH 19458
|
||||
assert Timedelta("30S").total_seconds() == 30.0
|
||||
assert Timedelta("0").total_seconds() == 0.0
|
||||
assert Timedelta("-2S").total_seconds() == -2.0
|
||||
assert Timedelta("5.324S").total_seconds() == 5.324
|
||||
assert (Timedelta("30S").total_seconds() - 30.0) < 1e-20
|
||||
assert (30.0 - Timedelta("30S").total_seconds()) < 1e-20
|
||||
|
||||
def test_timedelta_arithmetic(self):
|
||||
data = pd.Series(["nat", "32 days"], dtype="timedelta64[ns]")
|
||||
deltas = [timedelta(days=1), Timedelta(1, unit="D")]
|
||||
for delta in deltas:
|
||||
result_method = data.add(delta)
|
||||
result_operator = data + delta
|
||||
expected = pd.Series(["nat", "33 days"], dtype="timedelta64[ns]")
|
||||
tm.assert_series_equal(result_operator, expected)
|
||||
tm.assert_series_equal(result_method, expected)
|
||||
|
||||
result_method = data.sub(delta)
|
||||
result_operator = data - delta
|
||||
expected = pd.Series(["nat", "31 days"], dtype="timedelta64[ns]")
|
||||
tm.assert_series_equal(result_operator, expected)
|
||||
tm.assert_series_equal(result_method, expected)
|
||||
# GH 9396
|
||||
result_method = data.div(delta)
|
||||
result_operator = data / delta
|
||||
expected = pd.Series([np.nan, 32.0], dtype="float64")
|
||||
tm.assert_series_equal(result_operator, expected)
|
||||
tm.assert_series_equal(result_method, expected)
|
||||
|
||||
def test_apply_to_timedelta(self):
|
||||
timedelta_NaT = pd.to_timedelta("NaT")
|
||||
|
||||
list_of_valid_strings = ["00:00:01", "00:00:02"]
|
||||
a = pd.to_timedelta(list_of_valid_strings)
|
||||
b = Series(list_of_valid_strings).apply(pd.to_timedelta)
|
||||
# Can't compare until apply on a Series gives the correct dtype
|
||||
# assert_series_equal(a, b)
|
||||
|
||||
list_of_strings = ["00:00:01", np.nan, pd.NaT, timedelta_NaT]
|
||||
|
||||
# TODO: unused?
|
||||
a = pd.to_timedelta(list_of_strings) # noqa
|
||||
b = Series(list_of_strings).apply(pd.to_timedelta) # noqa
|
||||
# Can't compare until apply on a Series gives the correct dtype
|
||||
# assert_series_equal(a, b)
|
||||
|
||||
def test_components(self):
|
||||
rng = timedelta_range("1 days, 10:11:12", periods=2, freq="s")
|
||||
rng.components
|
||||
|
||||
# with nat
|
||||
s = Series(rng)
|
||||
s[1] = np.nan
|
||||
|
||||
result = s.dt.components
|
||||
assert not result.iloc[0].isna().all()
|
||||
assert result.iloc[1].isna().all()
|
||||
|
||||
def test_resolution_string(self):
|
||||
assert Timedelta(days=1).resolution_string == "D"
|
||||
assert Timedelta(days=1, hours=6).resolution_string == "H"
|
||||
assert Timedelta(days=1, minutes=6).resolution_string == "T"
|
||||
assert Timedelta(days=1, seconds=6).resolution_string == "S"
|
||||
assert Timedelta(days=1, milliseconds=6).resolution_string == "L"
|
||||
assert Timedelta(days=1, microseconds=6).resolution_string == "U"
|
||||
assert Timedelta(days=1, nanoseconds=6).resolution_string == "N"
|
||||
|
||||
def test_resolution_deprecated(self):
|
||||
# GH#21344
|
||||
td = Timedelta(days=4, hours=3)
|
||||
with tm.assert_produces_warning(FutureWarning) as w:
|
||||
td.resolution
|
||||
assert "Use Timedelta.resolution_string instead" in str(w[0].message)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value, expected",
|
||||
[
|
||||
(Timedelta("10S"), True),
|
||||
(Timedelta("-10S"), True),
|
||||
(Timedelta(10, unit="ns"), True),
|
||||
(Timedelta(0, unit="ns"), False),
|
||||
(Timedelta(-10, unit="ns"), True),
|
||||
(Timedelta(None), True),
|
||||
(pd.NaT, True),
|
||||
],
|
||||
)
|
||||
def test_truthiness(value, expected):
|
||||
# https://github.com/pandas-dev/pandas/issues/21484
|
||||
assert bool(value) is expected
|
Reference in New Issue
Block a user