Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +3 -0
- videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/np_datetime.cpython-310-x86_64-linux-gnu.so +3 -0
- videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/timezones.cpython-310-x86_64-linux-gnu.so +3 -0
- videochat2/lib/python3.10/site-packages/pandas/core/groupby/__pycache__/groupby.cpython-310.pyc +3 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/test_generic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/test_inference.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/test_missing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_na_scalar.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_nat.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_interval.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_ops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_arithmetic.py +63 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_interval.py +279 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_ops.py +119 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__pycache__/test_asfreq.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__pycache__/test_period.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/test_asfreq.py +801 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/test_period.py +1570 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/test_na_scalar.py +316 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/test_nat.py +696 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_formats.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_timedelta.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/test_arithmetic.py +1181 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/test_constructors.py +534 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/test_formats.py +44 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/test_timedelta.py +962 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_comparisons.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_formats.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_rendering.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_timestamp.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_timezones.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_unary_ops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/test_arithmetic.py +295 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/test_comparisons.py +313 -0
.gitattributes
CHANGED
|
@@ -1272,3 +1272,6 @@ videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/vectorized.cpython-3
|
|
| 1272 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/strptime.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1273 |
videochat2/lib/python3.10/site-packages/pandas/_libs/window/indexers.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1274 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/conversion.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 1272 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/strptime.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1273 |
videochat2/lib/python3.10/site-packages/pandas/_libs/window/indexers.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1274 |
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/conversion.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1275 |
+
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/np_datetime.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1276 |
+
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/timezones.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 1277 |
+
videochat2/lib/python3.10/site-packages/pandas/core/groupby/__pycache__/groupby.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/np_datetime.cpython-310-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e49aef0f892312fb2c399daffcca41bd244b26819d94f1bfb9d6c233e9974abc
|
| 3 |
+
size 147584
|
videochat2/lib/python3.10/site-packages/pandas/_libs/tslibs/timezones.cpython-310-x86_64-linux-gnu.so
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:220a0fd56c7c888c891034662c4b8250e836c7a15e24ec70febe04b00ed60448
|
| 3 |
+
size 262472
|
videochat2/lib/python3.10/site-packages/pandas/core/groupby/__pycache__/groupby.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2cf4020328f28abd54d440b3e79f59a3df748039272f3560484ca61200208bed
|
| 3 |
+
size 114977
|
videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (175 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/test_generic.cpython-310.pyc
ADDED
|
Binary file (4.66 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/test_inference.cpython-310.pyc
ADDED
|
Binary file (56.9 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/dtypes/__pycache__/test_missing.cpython-310.pyc
ADDED
|
Binary file (22.3 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (175 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_na_scalar.cpython-310.pyc
ADDED
|
Binary file (7.96 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/__pycache__/test_nat.cpython-310.pyc
ADDED
|
Binary file (14.9 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (184 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
|
Binary file (1.74 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_interval.cpython-310.pyc
ADDED
|
Binary file (7.99 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/__pycache__/test_ops.cpython-310.pyc
ADDED
|
Binary file (4.31 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_arithmetic.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import timedelta
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas import (
|
| 7 |
+
Interval,
|
| 8 |
+
Timedelta,
|
| 9 |
+
Timestamp,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@pytest.mark.parametrize("method", ["__add__", "__sub__"])
|
| 14 |
+
@pytest.mark.parametrize(
|
| 15 |
+
"interval",
|
| 16 |
+
[
|
| 17 |
+
Interval(Timestamp("2017-01-01 00:00:00"), Timestamp("2018-01-01 00:00:00")),
|
| 18 |
+
Interval(Timedelta(days=7), Timedelta(days=14)),
|
| 19 |
+
],
|
| 20 |
+
)
|
| 21 |
+
@pytest.mark.parametrize(
|
| 22 |
+
"delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")]
|
| 23 |
+
)
|
| 24 |
+
def test_time_interval_add_subtract_timedelta(interval, delta, method):
|
| 25 |
+
# https://github.com/pandas-dev/pandas/issues/32023
|
| 26 |
+
result = getattr(interval, method)(delta)
|
| 27 |
+
left = getattr(interval.left, method)(delta)
|
| 28 |
+
right = getattr(interval.right, method)(delta)
|
| 29 |
+
expected = Interval(left, right)
|
| 30 |
+
|
| 31 |
+
assert result == expected
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@pytest.mark.parametrize("interval", [Interval(1, 2), Interval(1.0, 2.0)])
|
| 35 |
+
@pytest.mark.parametrize(
|
| 36 |
+
"delta", [Timedelta(days=7), timedelta(7), np.timedelta64(7, "D")]
|
| 37 |
+
)
|
| 38 |
+
def test_numeric_interval_add_timedelta_raises(interval, delta):
|
| 39 |
+
# https://github.com/pandas-dev/pandas/issues/32023
|
| 40 |
+
msg = "|".join(
|
| 41 |
+
[
|
| 42 |
+
"unsupported operand",
|
| 43 |
+
"cannot use operands",
|
| 44 |
+
"Only numeric, Timestamp and Timedelta endpoints are allowed",
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
with pytest.raises((TypeError, ValueError), match=msg):
|
| 48 |
+
interval + delta
|
| 49 |
+
|
| 50 |
+
with pytest.raises((TypeError, ValueError), match=msg):
|
| 51 |
+
delta + interval
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@pytest.mark.parametrize("klass", [timedelta, np.timedelta64, Timedelta])
|
| 55 |
+
def test_timedelta_add_timestamp_interval(klass):
|
| 56 |
+
delta = klass(0)
|
| 57 |
+
expected = Interval(Timestamp("2020-01-01"), Timestamp("2020-02-01"))
|
| 58 |
+
|
| 59 |
+
result = delta + expected
|
| 60 |
+
assert result == expected
|
| 61 |
+
|
| 62 |
+
result = expected + delta
|
| 63 |
+
assert result == expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_interval.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Interval,
|
| 6 |
+
Period,
|
| 7 |
+
Timedelta,
|
| 8 |
+
Timestamp,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
import pandas.core.common as com
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@pytest.fixture
|
| 15 |
+
def interval():
|
| 16 |
+
return Interval(0, 1)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class TestInterval:
|
| 20 |
+
def test_properties(self, interval):
|
| 21 |
+
assert interval.closed == "right"
|
| 22 |
+
assert interval.left == 0
|
| 23 |
+
assert interval.right == 1
|
| 24 |
+
assert interval.mid == 0.5
|
| 25 |
+
|
| 26 |
+
def test_repr(self, interval):
|
| 27 |
+
assert repr(interval) == "Interval(0, 1, closed='right')"
|
| 28 |
+
assert str(interval) == "(0, 1]"
|
| 29 |
+
|
| 30 |
+
interval_left = Interval(0, 1, closed="left")
|
| 31 |
+
assert repr(interval_left) == "Interval(0, 1, closed='left')"
|
| 32 |
+
assert str(interval_left) == "[0, 1)"
|
| 33 |
+
|
| 34 |
+
def test_contains(self, interval):
|
| 35 |
+
assert 0.5 in interval
|
| 36 |
+
assert 1 in interval
|
| 37 |
+
assert 0 not in interval
|
| 38 |
+
|
| 39 |
+
interval_both = Interval(0, 1, "both")
|
| 40 |
+
assert 0 in interval_both
|
| 41 |
+
assert 1 in interval_both
|
| 42 |
+
|
| 43 |
+
interval_neither = Interval(0, 1, closed="neither")
|
| 44 |
+
assert 0 not in interval_neither
|
| 45 |
+
assert 0.5 in interval_neither
|
| 46 |
+
assert 1 not in interval_neither
|
| 47 |
+
|
| 48 |
+
def test_equal(self):
|
| 49 |
+
assert Interval(0, 1) == Interval(0, 1, closed="right")
|
| 50 |
+
assert Interval(0, 1) != Interval(0, 1, closed="left")
|
| 51 |
+
assert Interval(0, 1) != 0
|
| 52 |
+
|
| 53 |
+
def test_comparison(self):
|
| 54 |
+
msg = (
|
| 55 |
+
"'<' not supported between instances of "
|
| 56 |
+
"'pandas._libs.interval.Interval' and 'int'"
|
| 57 |
+
)
|
| 58 |
+
with pytest.raises(TypeError, match=msg):
|
| 59 |
+
Interval(0, 1) < 2
|
| 60 |
+
|
| 61 |
+
assert Interval(0, 1) < Interval(1, 2)
|
| 62 |
+
assert Interval(0, 1) < Interval(0, 2)
|
| 63 |
+
assert Interval(0, 1) < Interval(0.5, 1.5)
|
| 64 |
+
assert Interval(0, 1) <= Interval(0, 1)
|
| 65 |
+
assert Interval(0, 1) > Interval(-1, 2)
|
| 66 |
+
assert Interval(0, 1) >= Interval(0, 1)
|
| 67 |
+
|
| 68 |
+
def test_hash(self, interval):
|
| 69 |
+
# should not raise
|
| 70 |
+
hash(interval)
|
| 71 |
+
|
| 72 |
+
@pytest.mark.parametrize(
|
| 73 |
+
"left, right, expected",
|
| 74 |
+
[
|
| 75 |
+
(0, 5, 5),
|
| 76 |
+
(-2, 5.5, 7.5),
|
| 77 |
+
(10, 10, 0),
|
| 78 |
+
(10, np.inf, np.inf),
|
| 79 |
+
(-np.inf, -5, np.inf),
|
| 80 |
+
(-np.inf, np.inf, np.inf),
|
| 81 |
+
(Timedelta("0 days"), Timedelta("5 days"), Timedelta("5 days")),
|
| 82 |
+
(Timedelta("10 days"), Timedelta("10 days"), Timedelta("0 days")),
|
| 83 |
+
(Timedelta("1H10min"), Timedelta("5H5min"), Timedelta("3H55min")),
|
| 84 |
+
(Timedelta("5S"), Timedelta("1H"), Timedelta("59min55S")),
|
| 85 |
+
],
|
| 86 |
+
)
|
| 87 |
+
def test_length(self, left, right, expected):
|
| 88 |
+
# GH 18789
|
| 89 |
+
iv = Interval(left, right)
|
| 90 |
+
result = iv.length
|
| 91 |
+
assert result == expected
|
| 92 |
+
|
| 93 |
+
@pytest.mark.parametrize(
|
| 94 |
+
"left, right, expected",
|
| 95 |
+
[
|
| 96 |
+
("2017-01-01", "2017-01-06", "5 days"),
|
| 97 |
+
("2017-01-01", "2017-01-01 12:00:00", "12 hours"),
|
| 98 |
+
("2017-01-01 12:00", "2017-01-01 12:00:00", "0 days"),
|
| 99 |
+
("2017-01-01 12:01", "2017-01-05 17:31:00", "4 days 5 hours 30 min"),
|
| 100 |
+
],
|
| 101 |
+
)
|
| 102 |
+
@pytest.mark.parametrize("tz", (None, "UTC", "CET", "US/Eastern"))
|
| 103 |
+
def test_length_timestamp(self, tz, left, right, expected):
|
| 104 |
+
# GH 18789
|
| 105 |
+
iv = Interval(Timestamp(left, tz=tz), Timestamp(right, tz=tz))
|
| 106 |
+
result = iv.length
|
| 107 |
+
expected = Timedelta(expected)
|
| 108 |
+
assert result == expected
|
| 109 |
+
|
| 110 |
+
@pytest.mark.parametrize(
|
| 111 |
+
"left, right",
|
| 112 |
+
[
|
| 113 |
+
(0, 1),
|
| 114 |
+
(Timedelta("0 days"), Timedelta("1 day")),
|
| 115 |
+
(Timestamp("2018-01-01"), Timestamp("2018-01-02")),
|
| 116 |
+
(
|
| 117 |
+
Timestamp("2018-01-01", tz="US/Eastern"),
|
| 118 |
+
Timestamp("2018-01-02", tz="US/Eastern"),
|
| 119 |
+
),
|
| 120 |
+
],
|
| 121 |
+
)
|
| 122 |
+
def test_is_empty(self, left, right, closed):
|
| 123 |
+
# GH27219
|
| 124 |
+
# non-empty always return False
|
| 125 |
+
iv = Interval(left, right, closed)
|
| 126 |
+
assert iv.is_empty is False
|
| 127 |
+
|
| 128 |
+
# same endpoint is empty except when closed='both' (contains one point)
|
| 129 |
+
iv = Interval(left, left, closed)
|
| 130 |
+
result = iv.is_empty
|
| 131 |
+
expected = closed != "both"
|
| 132 |
+
assert result is expected
|
| 133 |
+
|
| 134 |
+
@pytest.mark.parametrize(
|
| 135 |
+
"left, right",
|
| 136 |
+
[
|
| 137 |
+
("a", "z"),
|
| 138 |
+
(("a", "b"), ("c", "d")),
|
| 139 |
+
(list("AB"), list("ab")),
|
| 140 |
+
(Interval(0, 1), Interval(1, 2)),
|
| 141 |
+
(Period("2018Q1", freq="Q"), Period("2018Q1", freq="Q")),
|
| 142 |
+
],
|
| 143 |
+
)
|
| 144 |
+
def test_construct_errors(self, left, right):
|
| 145 |
+
# GH 23013
|
| 146 |
+
msg = "Only numeric, Timestamp and Timedelta endpoints are allowed"
|
| 147 |
+
with pytest.raises(ValueError, match=msg):
|
| 148 |
+
Interval(left, right)
|
| 149 |
+
|
| 150 |
+
def test_math_add(self, closed):
|
| 151 |
+
interval = Interval(0, 1, closed=closed)
|
| 152 |
+
expected = Interval(1, 2, closed=closed)
|
| 153 |
+
|
| 154 |
+
result = interval + 1
|
| 155 |
+
assert result == expected
|
| 156 |
+
|
| 157 |
+
result = 1 + interval
|
| 158 |
+
assert result == expected
|
| 159 |
+
|
| 160 |
+
result = interval
|
| 161 |
+
result += 1
|
| 162 |
+
assert result == expected
|
| 163 |
+
|
| 164 |
+
msg = r"unsupported operand type\(s\) for \+"
|
| 165 |
+
with pytest.raises(TypeError, match=msg):
|
| 166 |
+
interval + interval
|
| 167 |
+
|
| 168 |
+
with pytest.raises(TypeError, match=msg):
|
| 169 |
+
interval + "foo"
|
| 170 |
+
|
| 171 |
+
def test_math_sub(self, closed):
|
| 172 |
+
interval = Interval(0, 1, closed=closed)
|
| 173 |
+
expected = Interval(-1, 0, closed=closed)
|
| 174 |
+
|
| 175 |
+
result = interval - 1
|
| 176 |
+
assert result == expected
|
| 177 |
+
|
| 178 |
+
result = interval
|
| 179 |
+
result -= 1
|
| 180 |
+
assert result == expected
|
| 181 |
+
|
| 182 |
+
msg = r"unsupported operand type\(s\) for -"
|
| 183 |
+
with pytest.raises(TypeError, match=msg):
|
| 184 |
+
interval - interval
|
| 185 |
+
|
| 186 |
+
with pytest.raises(TypeError, match=msg):
|
| 187 |
+
interval - "foo"
|
| 188 |
+
|
| 189 |
+
def test_math_mult(self, closed):
|
| 190 |
+
interval = Interval(0, 1, closed=closed)
|
| 191 |
+
expected = Interval(0, 2, closed=closed)
|
| 192 |
+
|
| 193 |
+
result = interval * 2
|
| 194 |
+
assert result == expected
|
| 195 |
+
|
| 196 |
+
result = 2 * interval
|
| 197 |
+
assert result == expected
|
| 198 |
+
|
| 199 |
+
result = interval
|
| 200 |
+
result *= 2
|
| 201 |
+
assert result == expected
|
| 202 |
+
|
| 203 |
+
msg = r"unsupported operand type\(s\) for \*"
|
| 204 |
+
with pytest.raises(TypeError, match=msg):
|
| 205 |
+
interval * interval
|
| 206 |
+
|
| 207 |
+
msg = r"can\'t multiply sequence by non-int"
|
| 208 |
+
with pytest.raises(TypeError, match=msg):
|
| 209 |
+
interval * "foo"
|
| 210 |
+
|
| 211 |
+
def test_math_div(self, closed):
|
| 212 |
+
interval = Interval(0, 1, closed=closed)
|
| 213 |
+
expected = Interval(0, 0.5, closed=closed)
|
| 214 |
+
|
| 215 |
+
result = interval / 2.0
|
| 216 |
+
assert result == expected
|
| 217 |
+
|
| 218 |
+
result = interval
|
| 219 |
+
result /= 2.0
|
| 220 |
+
assert result == expected
|
| 221 |
+
|
| 222 |
+
msg = r"unsupported operand type\(s\) for /"
|
| 223 |
+
with pytest.raises(TypeError, match=msg):
|
| 224 |
+
interval / interval
|
| 225 |
+
|
| 226 |
+
with pytest.raises(TypeError, match=msg):
|
| 227 |
+
interval / "foo"
|
| 228 |
+
|
| 229 |
+
def test_math_floordiv(self, closed):
|
| 230 |
+
interval = Interval(1, 2, closed=closed)
|
| 231 |
+
expected = Interval(0, 1, closed=closed)
|
| 232 |
+
|
| 233 |
+
result = interval // 2
|
| 234 |
+
assert result == expected
|
| 235 |
+
|
| 236 |
+
result = interval
|
| 237 |
+
result //= 2
|
| 238 |
+
assert result == expected
|
| 239 |
+
|
| 240 |
+
msg = r"unsupported operand type\(s\) for //"
|
| 241 |
+
with pytest.raises(TypeError, match=msg):
|
| 242 |
+
interval // interval
|
| 243 |
+
|
| 244 |
+
with pytest.raises(TypeError, match=msg):
|
| 245 |
+
interval // "foo"
|
| 246 |
+
|
| 247 |
+
def test_constructor_errors(self):
|
| 248 |
+
msg = "invalid option for 'closed': foo"
|
| 249 |
+
with pytest.raises(ValueError, match=msg):
|
| 250 |
+
Interval(0, 1, closed="foo")
|
| 251 |
+
|
| 252 |
+
msg = "left side of interval must be <= right side"
|
| 253 |
+
with pytest.raises(ValueError, match=msg):
|
| 254 |
+
Interval(1, 0)
|
| 255 |
+
|
| 256 |
+
@pytest.mark.parametrize(
|
| 257 |
+
"tz_left, tz_right", [(None, "UTC"), ("UTC", None), ("UTC", "US/Eastern")]
|
| 258 |
+
)
|
| 259 |
+
def test_constructor_errors_tz(self, tz_left, tz_right):
|
| 260 |
+
# GH 18538
|
| 261 |
+
left = Timestamp("2017-01-01", tz=tz_left)
|
| 262 |
+
right = Timestamp("2017-01-02", tz=tz_right)
|
| 263 |
+
|
| 264 |
+
if com.any_none(tz_left, tz_right):
|
| 265 |
+
error = TypeError
|
| 266 |
+
msg = "Cannot compare tz-naive and tz-aware timestamps"
|
| 267 |
+
else:
|
| 268 |
+
error = ValueError
|
| 269 |
+
msg = "left and right must have the same time zone"
|
| 270 |
+
with pytest.raises(error, match=msg):
|
| 271 |
+
Interval(left, right)
|
| 272 |
+
|
| 273 |
+
def test_equality_comparison_broadcasts_over_array(self):
|
| 274 |
+
# https://github.com/pandas-dev/pandas/issues/35931
|
| 275 |
+
interval = Interval(0, 1)
|
| 276 |
+
arr = np.array([interval, interval])
|
| 277 |
+
result = interval == arr
|
| 278 |
+
expected = np.array([True, True])
|
| 279 |
+
tm.assert_numpy_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/interval/test_ops.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for Interval-Interval operations, such as overlaps, contains, etc."""
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Interval,
|
| 6 |
+
Timedelta,
|
| 7 |
+
Timestamp,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@pytest.fixture(
|
| 12 |
+
params=[
|
| 13 |
+
(Timedelta("0 days"), Timedelta("1 day")),
|
| 14 |
+
(Timestamp("2018-01-01"), Timedelta("1 day")),
|
| 15 |
+
(0, 1),
|
| 16 |
+
],
|
| 17 |
+
ids=lambda x: type(x[0]).__name__,
|
| 18 |
+
)
|
| 19 |
+
def start_shift(request):
|
| 20 |
+
"""
|
| 21 |
+
Fixture for generating intervals of types from a start value and a shift
|
| 22 |
+
value that can be added to start to generate an endpoint
|
| 23 |
+
"""
|
| 24 |
+
return request.param
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class TestOverlaps:
|
| 28 |
+
def test_overlaps_self(self, start_shift, closed):
|
| 29 |
+
start, shift = start_shift
|
| 30 |
+
interval = Interval(start, start + shift, closed)
|
| 31 |
+
assert interval.overlaps(interval)
|
| 32 |
+
|
| 33 |
+
def test_overlaps_nested(self, start_shift, closed, other_closed):
|
| 34 |
+
start, shift = start_shift
|
| 35 |
+
interval1 = Interval(start, start + 3 * shift, other_closed)
|
| 36 |
+
interval2 = Interval(start + shift, start + 2 * shift, closed)
|
| 37 |
+
|
| 38 |
+
# nested intervals should always overlap
|
| 39 |
+
assert interval1.overlaps(interval2)
|
| 40 |
+
|
| 41 |
+
def test_overlaps_disjoint(self, start_shift, closed, other_closed):
|
| 42 |
+
start, shift = start_shift
|
| 43 |
+
interval1 = Interval(start, start + shift, other_closed)
|
| 44 |
+
interval2 = Interval(start + 2 * shift, start + 3 * shift, closed)
|
| 45 |
+
|
| 46 |
+
# disjoint intervals should never overlap
|
| 47 |
+
assert not interval1.overlaps(interval2)
|
| 48 |
+
|
| 49 |
+
def test_overlaps_endpoint(self, start_shift, closed, other_closed):
|
| 50 |
+
start, shift = start_shift
|
| 51 |
+
interval1 = Interval(start, start + shift, other_closed)
|
| 52 |
+
interval2 = Interval(start + shift, start + 2 * shift, closed)
|
| 53 |
+
|
| 54 |
+
# overlap if shared endpoint is closed for both (overlap at a point)
|
| 55 |
+
result = interval1.overlaps(interval2)
|
| 56 |
+
expected = interval1.closed_right and interval2.closed_left
|
| 57 |
+
assert result == expected
|
| 58 |
+
|
| 59 |
+
@pytest.mark.parametrize(
|
| 60 |
+
"other",
|
| 61 |
+
[10, True, "foo", Timedelta("1 day"), Timestamp("2018-01-01")],
|
| 62 |
+
ids=lambda x: type(x).__name__,
|
| 63 |
+
)
|
| 64 |
+
def test_overlaps_invalid_type(self, other):
|
| 65 |
+
interval = Interval(0, 1)
|
| 66 |
+
msg = f"`other` must be an Interval, got {type(other).__name__}"
|
| 67 |
+
with pytest.raises(TypeError, match=msg):
|
| 68 |
+
interval.overlaps(other)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class TestContains:
|
| 72 |
+
def test_contains_interval(self, inclusive_endpoints_fixture):
|
| 73 |
+
interval1 = Interval(0, 1, "both")
|
| 74 |
+
interval2 = Interval(0, 1, inclusive_endpoints_fixture)
|
| 75 |
+
assert interval1 in interval1
|
| 76 |
+
assert interval2 in interval2
|
| 77 |
+
assert interval2 in interval1
|
| 78 |
+
assert interval1 not in interval2 or inclusive_endpoints_fixture == "both"
|
| 79 |
+
|
| 80 |
+
def test_contains_infinite_length(self):
|
| 81 |
+
interval1 = Interval(0, 1, "both")
|
| 82 |
+
interval2 = Interval(float("-inf"), float("inf"), "neither")
|
| 83 |
+
assert interval1 in interval2
|
| 84 |
+
assert interval2 not in interval1
|
| 85 |
+
|
| 86 |
+
def test_contains_zero_length(self):
|
| 87 |
+
interval1 = Interval(0, 1, "both")
|
| 88 |
+
interval2 = Interval(-1, -1, "both")
|
| 89 |
+
interval3 = Interval(0.5, 0.5, "both")
|
| 90 |
+
assert interval2 not in interval1
|
| 91 |
+
assert interval3 in interval1
|
| 92 |
+
assert interval2 not in interval3 and interval3 not in interval2
|
| 93 |
+
assert interval1 not in interval2 and interval1 not in interval3
|
| 94 |
+
|
| 95 |
+
@pytest.mark.parametrize(
|
| 96 |
+
"type1",
|
| 97 |
+
[
|
| 98 |
+
(0, 1),
|
| 99 |
+
(Timestamp(2000, 1, 1, 0), Timestamp(2000, 1, 1, 1)),
|
| 100 |
+
(Timedelta("0h"), Timedelta("1h")),
|
| 101 |
+
],
|
| 102 |
+
)
|
| 103 |
+
@pytest.mark.parametrize(
|
| 104 |
+
"type2",
|
| 105 |
+
[
|
| 106 |
+
(0, 1),
|
| 107 |
+
(Timestamp(2000, 1, 1, 0), Timestamp(2000, 1, 1, 1)),
|
| 108 |
+
(Timedelta("0h"), Timedelta("1h")),
|
| 109 |
+
],
|
| 110 |
+
)
|
| 111 |
+
def test_contains_mixed_types(self, type1, type2):
|
| 112 |
+
interval1 = Interval(*type1)
|
| 113 |
+
interval2 = Interval(*type2)
|
| 114 |
+
if type1 == type2:
|
| 115 |
+
assert interval1 in interval2
|
| 116 |
+
else:
|
| 117 |
+
msg = "^'<=' not supported between instances of"
|
| 118 |
+
with pytest.raises(TypeError, match=msg):
|
| 119 |
+
interval1 in interval2
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (182 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__pycache__/test_asfreq.cpython-310.pyc
ADDED
|
Binary file (20.4 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/__pycache__/test_period.cpython-310.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas._libs.tslibs.dtypes import _period_code_map
|
| 4 |
+
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
|
| 5 |
+
from pandas.errors import OutOfBoundsDatetime
|
| 6 |
+
|
| 7 |
+
from pandas import (
|
| 8 |
+
Period,
|
| 9 |
+
Timestamp,
|
| 10 |
+
offsets,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestFreqConversion:
|
| 15 |
+
"""Test frequency conversion of date objects"""
|
| 16 |
+
|
| 17 |
+
@pytest.mark.parametrize("freq", ["A", "Q", "M", "W", "B", "D"])
|
| 18 |
+
def test_asfreq_near_zero(self, freq):
|
| 19 |
+
# GH#19643, GH#19650
|
| 20 |
+
per = Period("0001-01-01", freq=freq)
|
| 21 |
+
tup1 = (per.year, per.hour, per.day)
|
| 22 |
+
|
| 23 |
+
prev = per - 1
|
| 24 |
+
assert prev.ordinal == per.ordinal - 1
|
| 25 |
+
tup2 = (prev.year, prev.month, prev.day)
|
| 26 |
+
assert tup2 < tup1
|
| 27 |
+
|
| 28 |
+
def test_asfreq_near_zero_weekly(self):
|
| 29 |
+
# GH#19834
|
| 30 |
+
per1 = Period("0001-01-01", "D") + 6
|
| 31 |
+
per2 = Period("0001-01-01", "D") - 6
|
| 32 |
+
week1 = per1.asfreq("W")
|
| 33 |
+
week2 = per2.asfreq("W")
|
| 34 |
+
assert week1 != week2
|
| 35 |
+
assert week1.asfreq("D", "E") >= per1
|
| 36 |
+
assert week2.asfreq("D", "S") <= per2
|
| 37 |
+
|
| 38 |
+
def test_to_timestamp_out_of_bounds(self):
|
| 39 |
+
# GH#19643, used to incorrectly give Timestamp in 1754
|
| 40 |
+
per = Period("0001-01-01", freq="B")
|
| 41 |
+
msg = "Out of bounds nanosecond timestamp"
|
| 42 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 43 |
+
per.to_timestamp()
|
| 44 |
+
|
| 45 |
+
def test_asfreq_corner(self):
|
| 46 |
+
val = Period(freq="A", year=2007)
|
| 47 |
+
result1 = val.asfreq("5t")
|
| 48 |
+
result2 = val.asfreq("t")
|
| 49 |
+
expected = Period("2007-12-31 23:59", freq="t")
|
| 50 |
+
assert result1.ordinal == expected.ordinal
|
| 51 |
+
assert result1.freqstr == "5T"
|
| 52 |
+
assert result2.ordinal == expected.ordinal
|
| 53 |
+
assert result2.freqstr == "T"
|
| 54 |
+
|
| 55 |
+
def test_conv_annual(self):
|
| 56 |
+
# frequency conversion tests: from Annual Frequency
|
| 57 |
+
|
| 58 |
+
ival_A = Period(freq="A", year=2007)
|
| 59 |
+
|
| 60 |
+
ival_AJAN = Period(freq="A-JAN", year=2007)
|
| 61 |
+
ival_AJUN = Period(freq="A-JUN", year=2007)
|
| 62 |
+
ival_ANOV = Period(freq="A-NOV", year=2007)
|
| 63 |
+
|
| 64 |
+
ival_A_to_Q_start = Period(freq="Q", year=2007, quarter=1)
|
| 65 |
+
ival_A_to_Q_end = Period(freq="Q", year=2007, quarter=4)
|
| 66 |
+
ival_A_to_M_start = Period(freq="M", year=2007, month=1)
|
| 67 |
+
ival_A_to_M_end = Period(freq="M", year=2007, month=12)
|
| 68 |
+
ival_A_to_W_start = Period(freq="W", year=2007, month=1, day=1)
|
| 69 |
+
ival_A_to_W_end = Period(freq="W", year=2007, month=12, day=31)
|
| 70 |
+
ival_A_to_B_start = Period(freq="B", year=2007, month=1, day=1)
|
| 71 |
+
ival_A_to_B_end = Period(freq="B", year=2007, month=12, day=31)
|
| 72 |
+
ival_A_to_D_start = Period(freq="D", year=2007, month=1, day=1)
|
| 73 |
+
ival_A_to_D_end = Period(freq="D", year=2007, month=12, day=31)
|
| 74 |
+
ival_A_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 75 |
+
ival_A_to_H_end = Period(freq="H", year=2007, month=12, day=31, hour=23)
|
| 76 |
+
ival_A_to_T_start = Period(
|
| 77 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0
|
| 78 |
+
)
|
| 79 |
+
ival_A_to_T_end = Period(
|
| 80 |
+
freq="Min", year=2007, month=12, day=31, hour=23, minute=59
|
| 81 |
+
)
|
| 82 |
+
ival_A_to_S_start = Period(
|
| 83 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 84 |
+
)
|
| 85 |
+
ival_A_to_S_end = Period(
|
| 86 |
+
freq="S", year=2007, month=12, day=31, hour=23, minute=59, second=59
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
ival_AJAN_to_D_end = Period(freq="D", year=2007, month=1, day=31)
|
| 90 |
+
ival_AJAN_to_D_start = Period(freq="D", year=2006, month=2, day=1)
|
| 91 |
+
ival_AJUN_to_D_end = Period(freq="D", year=2007, month=6, day=30)
|
| 92 |
+
ival_AJUN_to_D_start = Period(freq="D", year=2006, month=7, day=1)
|
| 93 |
+
ival_ANOV_to_D_end = Period(freq="D", year=2007, month=11, day=30)
|
| 94 |
+
ival_ANOV_to_D_start = Period(freq="D", year=2006, month=12, day=1)
|
| 95 |
+
|
| 96 |
+
assert ival_A.asfreq("Q", "S") == ival_A_to_Q_start
|
| 97 |
+
assert ival_A.asfreq("Q", "e") == ival_A_to_Q_end
|
| 98 |
+
assert ival_A.asfreq("M", "s") == ival_A_to_M_start
|
| 99 |
+
assert ival_A.asfreq("M", "E") == ival_A_to_M_end
|
| 100 |
+
assert ival_A.asfreq("W", "S") == ival_A_to_W_start
|
| 101 |
+
assert ival_A.asfreq("W", "E") == ival_A_to_W_end
|
| 102 |
+
assert ival_A.asfreq("B", "S") == ival_A_to_B_start
|
| 103 |
+
assert ival_A.asfreq("B", "E") == ival_A_to_B_end
|
| 104 |
+
assert ival_A.asfreq("D", "S") == ival_A_to_D_start
|
| 105 |
+
assert ival_A.asfreq("D", "E") == ival_A_to_D_end
|
| 106 |
+
assert ival_A.asfreq("H", "S") == ival_A_to_H_start
|
| 107 |
+
assert ival_A.asfreq("H", "E") == ival_A_to_H_end
|
| 108 |
+
assert ival_A.asfreq("min", "S") == ival_A_to_T_start
|
| 109 |
+
assert ival_A.asfreq("min", "E") == ival_A_to_T_end
|
| 110 |
+
assert ival_A.asfreq("T", "S") == ival_A_to_T_start
|
| 111 |
+
assert ival_A.asfreq("T", "E") == ival_A_to_T_end
|
| 112 |
+
assert ival_A.asfreq("S", "S") == ival_A_to_S_start
|
| 113 |
+
assert ival_A.asfreq("S", "E") == ival_A_to_S_end
|
| 114 |
+
|
| 115 |
+
assert ival_AJAN.asfreq("D", "S") == ival_AJAN_to_D_start
|
| 116 |
+
assert ival_AJAN.asfreq("D", "E") == ival_AJAN_to_D_end
|
| 117 |
+
|
| 118 |
+
assert ival_AJUN.asfreq("D", "S") == ival_AJUN_to_D_start
|
| 119 |
+
assert ival_AJUN.asfreq("D", "E") == ival_AJUN_to_D_end
|
| 120 |
+
|
| 121 |
+
assert ival_ANOV.asfreq("D", "S") == ival_ANOV_to_D_start
|
| 122 |
+
assert ival_ANOV.asfreq("D", "E") == ival_ANOV_to_D_end
|
| 123 |
+
|
| 124 |
+
assert ival_A.asfreq("A") == ival_A
|
| 125 |
+
|
| 126 |
+
def test_conv_quarterly(self):
|
| 127 |
+
# frequency conversion tests: from Quarterly Frequency
|
| 128 |
+
|
| 129 |
+
ival_Q = Period(freq="Q", year=2007, quarter=1)
|
| 130 |
+
ival_Q_end_of_year = Period(freq="Q", year=2007, quarter=4)
|
| 131 |
+
|
| 132 |
+
ival_QEJAN = Period(freq="Q-JAN", year=2007, quarter=1)
|
| 133 |
+
ival_QEJUN = Period(freq="Q-JUN", year=2007, quarter=1)
|
| 134 |
+
|
| 135 |
+
ival_Q_to_A = Period(freq="A", year=2007)
|
| 136 |
+
ival_Q_to_M_start = Period(freq="M", year=2007, month=1)
|
| 137 |
+
ival_Q_to_M_end = Period(freq="M", year=2007, month=3)
|
| 138 |
+
ival_Q_to_W_start = Period(freq="W", year=2007, month=1, day=1)
|
| 139 |
+
ival_Q_to_W_end = Period(freq="W", year=2007, month=3, day=31)
|
| 140 |
+
ival_Q_to_B_start = Period(freq="B", year=2007, month=1, day=1)
|
| 141 |
+
ival_Q_to_B_end = Period(freq="B", year=2007, month=3, day=30)
|
| 142 |
+
ival_Q_to_D_start = Period(freq="D", year=2007, month=1, day=1)
|
| 143 |
+
ival_Q_to_D_end = Period(freq="D", year=2007, month=3, day=31)
|
| 144 |
+
ival_Q_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 145 |
+
ival_Q_to_H_end = Period(freq="H", year=2007, month=3, day=31, hour=23)
|
| 146 |
+
ival_Q_to_T_start = Period(
|
| 147 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0
|
| 148 |
+
)
|
| 149 |
+
ival_Q_to_T_end = Period(
|
| 150 |
+
freq="Min", year=2007, month=3, day=31, hour=23, minute=59
|
| 151 |
+
)
|
| 152 |
+
ival_Q_to_S_start = Period(
|
| 153 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 154 |
+
)
|
| 155 |
+
ival_Q_to_S_end = Period(
|
| 156 |
+
freq="S", year=2007, month=3, day=31, hour=23, minute=59, second=59
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
ival_QEJAN_to_D_start = Period(freq="D", year=2006, month=2, day=1)
|
| 160 |
+
ival_QEJAN_to_D_end = Period(freq="D", year=2006, month=4, day=30)
|
| 161 |
+
|
| 162 |
+
ival_QEJUN_to_D_start = Period(freq="D", year=2006, month=7, day=1)
|
| 163 |
+
ival_QEJUN_to_D_end = Period(freq="D", year=2006, month=9, day=30)
|
| 164 |
+
|
| 165 |
+
assert ival_Q.asfreq("A") == ival_Q_to_A
|
| 166 |
+
assert ival_Q_end_of_year.asfreq("A") == ival_Q_to_A
|
| 167 |
+
|
| 168 |
+
assert ival_Q.asfreq("M", "S") == ival_Q_to_M_start
|
| 169 |
+
assert ival_Q.asfreq("M", "E") == ival_Q_to_M_end
|
| 170 |
+
assert ival_Q.asfreq("W", "S") == ival_Q_to_W_start
|
| 171 |
+
assert ival_Q.asfreq("W", "E") == ival_Q_to_W_end
|
| 172 |
+
assert ival_Q.asfreq("B", "S") == ival_Q_to_B_start
|
| 173 |
+
assert ival_Q.asfreq("B", "E") == ival_Q_to_B_end
|
| 174 |
+
assert ival_Q.asfreq("D", "S") == ival_Q_to_D_start
|
| 175 |
+
assert ival_Q.asfreq("D", "E") == ival_Q_to_D_end
|
| 176 |
+
assert ival_Q.asfreq("H", "S") == ival_Q_to_H_start
|
| 177 |
+
assert ival_Q.asfreq("H", "E") == ival_Q_to_H_end
|
| 178 |
+
assert ival_Q.asfreq("Min", "S") == ival_Q_to_T_start
|
| 179 |
+
assert ival_Q.asfreq("Min", "E") == ival_Q_to_T_end
|
| 180 |
+
assert ival_Q.asfreq("S", "S") == ival_Q_to_S_start
|
| 181 |
+
assert ival_Q.asfreq("S", "E") == ival_Q_to_S_end
|
| 182 |
+
|
| 183 |
+
assert ival_QEJAN.asfreq("D", "S") == ival_QEJAN_to_D_start
|
| 184 |
+
assert ival_QEJAN.asfreq("D", "E") == ival_QEJAN_to_D_end
|
| 185 |
+
assert ival_QEJUN.asfreq("D", "S") == ival_QEJUN_to_D_start
|
| 186 |
+
assert ival_QEJUN.asfreq("D", "E") == ival_QEJUN_to_D_end
|
| 187 |
+
|
| 188 |
+
assert ival_Q.asfreq("Q") == ival_Q
|
| 189 |
+
|
| 190 |
+
def test_conv_monthly(self):
|
| 191 |
+
# frequency conversion tests: from Monthly Frequency
|
| 192 |
+
|
| 193 |
+
ival_M = Period(freq="M", year=2007, month=1)
|
| 194 |
+
ival_M_end_of_year = Period(freq="M", year=2007, month=12)
|
| 195 |
+
ival_M_end_of_quarter = Period(freq="M", year=2007, month=3)
|
| 196 |
+
ival_M_to_A = Period(freq="A", year=2007)
|
| 197 |
+
ival_M_to_Q = Period(freq="Q", year=2007, quarter=1)
|
| 198 |
+
ival_M_to_W_start = Period(freq="W", year=2007, month=1, day=1)
|
| 199 |
+
ival_M_to_W_end = Period(freq="W", year=2007, month=1, day=31)
|
| 200 |
+
ival_M_to_B_start = Period(freq="B", year=2007, month=1, day=1)
|
| 201 |
+
ival_M_to_B_end = Period(freq="B", year=2007, month=1, day=31)
|
| 202 |
+
ival_M_to_D_start = Period(freq="D", year=2007, month=1, day=1)
|
| 203 |
+
ival_M_to_D_end = Period(freq="D", year=2007, month=1, day=31)
|
| 204 |
+
ival_M_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 205 |
+
ival_M_to_H_end = Period(freq="H", year=2007, month=1, day=31, hour=23)
|
| 206 |
+
ival_M_to_T_start = Period(
|
| 207 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0
|
| 208 |
+
)
|
| 209 |
+
ival_M_to_T_end = Period(
|
| 210 |
+
freq="Min", year=2007, month=1, day=31, hour=23, minute=59
|
| 211 |
+
)
|
| 212 |
+
ival_M_to_S_start = Period(
|
| 213 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 214 |
+
)
|
| 215 |
+
ival_M_to_S_end = Period(
|
| 216 |
+
freq="S", year=2007, month=1, day=31, hour=23, minute=59, second=59
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
assert ival_M.asfreq("A") == ival_M_to_A
|
| 220 |
+
assert ival_M_end_of_year.asfreq("A") == ival_M_to_A
|
| 221 |
+
assert ival_M.asfreq("Q") == ival_M_to_Q
|
| 222 |
+
assert ival_M_end_of_quarter.asfreq("Q") == ival_M_to_Q
|
| 223 |
+
|
| 224 |
+
assert ival_M.asfreq("W", "S") == ival_M_to_W_start
|
| 225 |
+
assert ival_M.asfreq("W", "E") == ival_M_to_W_end
|
| 226 |
+
assert ival_M.asfreq("B", "S") == ival_M_to_B_start
|
| 227 |
+
assert ival_M.asfreq("B", "E") == ival_M_to_B_end
|
| 228 |
+
assert ival_M.asfreq("D", "S") == ival_M_to_D_start
|
| 229 |
+
assert ival_M.asfreq("D", "E") == ival_M_to_D_end
|
| 230 |
+
assert ival_M.asfreq("H", "S") == ival_M_to_H_start
|
| 231 |
+
assert ival_M.asfreq("H", "E") == ival_M_to_H_end
|
| 232 |
+
assert ival_M.asfreq("Min", "S") == ival_M_to_T_start
|
| 233 |
+
assert ival_M.asfreq("Min", "E") == ival_M_to_T_end
|
| 234 |
+
assert ival_M.asfreq("S", "S") == ival_M_to_S_start
|
| 235 |
+
assert ival_M.asfreq("S", "E") == ival_M_to_S_end
|
| 236 |
+
|
| 237 |
+
assert ival_M.asfreq("M") == ival_M
|
| 238 |
+
|
| 239 |
+
def test_conv_weekly(self):
|
| 240 |
+
# frequency conversion tests: from Weekly Frequency
|
| 241 |
+
ival_W = Period(freq="W", year=2007, month=1, day=1)
|
| 242 |
+
|
| 243 |
+
ival_WSUN = Period(freq="W", year=2007, month=1, day=7)
|
| 244 |
+
ival_WSAT = Period(freq="W-SAT", year=2007, month=1, day=6)
|
| 245 |
+
ival_WFRI = Period(freq="W-FRI", year=2007, month=1, day=5)
|
| 246 |
+
ival_WTHU = Period(freq="W-THU", year=2007, month=1, day=4)
|
| 247 |
+
ival_WWED = Period(freq="W-WED", year=2007, month=1, day=3)
|
| 248 |
+
ival_WTUE = Period(freq="W-TUE", year=2007, month=1, day=2)
|
| 249 |
+
ival_WMON = Period(freq="W-MON", year=2007, month=1, day=1)
|
| 250 |
+
|
| 251 |
+
ival_WSUN_to_D_start = Period(freq="D", year=2007, month=1, day=1)
|
| 252 |
+
ival_WSUN_to_D_end = Period(freq="D", year=2007, month=1, day=7)
|
| 253 |
+
ival_WSAT_to_D_start = Period(freq="D", year=2006, month=12, day=31)
|
| 254 |
+
ival_WSAT_to_D_end = Period(freq="D", year=2007, month=1, day=6)
|
| 255 |
+
ival_WFRI_to_D_start = Period(freq="D", year=2006, month=12, day=30)
|
| 256 |
+
ival_WFRI_to_D_end = Period(freq="D", year=2007, month=1, day=5)
|
| 257 |
+
ival_WTHU_to_D_start = Period(freq="D", year=2006, month=12, day=29)
|
| 258 |
+
ival_WTHU_to_D_end = Period(freq="D", year=2007, month=1, day=4)
|
| 259 |
+
ival_WWED_to_D_start = Period(freq="D", year=2006, month=12, day=28)
|
| 260 |
+
ival_WWED_to_D_end = Period(freq="D", year=2007, month=1, day=3)
|
| 261 |
+
ival_WTUE_to_D_start = Period(freq="D", year=2006, month=12, day=27)
|
| 262 |
+
ival_WTUE_to_D_end = Period(freq="D", year=2007, month=1, day=2)
|
| 263 |
+
ival_WMON_to_D_start = Period(freq="D", year=2006, month=12, day=26)
|
| 264 |
+
ival_WMON_to_D_end = Period(freq="D", year=2007, month=1, day=1)
|
| 265 |
+
|
| 266 |
+
ival_W_end_of_year = Period(freq="W", year=2007, month=12, day=31)
|
| 267 |
+
ival_W_end_of_quarter = Period(freq="W", year=2007, month=3, day=31)
|
| 268 |
+
ival_W_end_of_month = Period(freq="W", year=2007, month=1, day=31)
|
| 269 |
+
ival_W_to_A = Period(freq="A", year=2007)
|
| 270 |
+
ival_W_to_Q = Period(freq="Q", year=2007, quarter=1)
|
| 271 |
+
ival_W_to_M = Period(freq="M", year=2007, month=1)
|
| 272 |
+
|
| 273 |
+
if Period(freq="D", year=2007, month=12, day=31).weekday == 6:
|
| 274 |
+
ival_W_to_A_end_of_year = Period(freq="A", year=2007)
|
| 275 |
+
else:
|
| 276 |
+
ival_W_to_A_end_of_year = Period(freq="A", year=2008)
|
| 277 |
+
|
| 278 |
+
if Period(freq="D", year=2007, month=3, day=31).weekday == 6:
|
| 279 |
+
ival_W_to_Q_end_of_quarter = Period(freq="Q", year=2007, quarter=1)
|
| 280 |
+
else:
|
| 281 |
+
ival_W_to_Q_end_of_quarter = Period(freq="Q", year=2007, quarter=2)
|
| 282 |
+
|
| 283 |
+
if Period(freq="D", year=2007, month=1, day=31).weekday == 6:
|
| 284 |
+
ival_W_to_M_end_of_month = Period(freq="M", year=2007, month=1)
|
| 285 |
+
else:
|
| 286 |
+
ival_W_to_M_end_of_month = Period(freq="M", year=2007, month=2)
|
| 287 |
+
|
| 288 |
+
ival_W_to_B_start = Period(freq="B", year=2007, month=1, day=1)
|
| 289 |
+
ival_W_to_B_end = Period(freq="B", year=2007, month=1, day=5)
|
| 290 |
+
ival_W_to_D_start = Period(freq="D", year=2007, month=1, day=1)
|
| 291 |
+
ival_W_to_D_end = Period(freq="D", year=2007, month=1, day=7)
|
| 292 |
+
ival_W_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 293 |
+
ival_W_to_H_end = Period(freq="H", year=2007, month=1, day=7, hour=23)
|
| 294 |
+
ival_W_to_T_start = Period(
|
| 295 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0
|
| 296 |
+
)
|
| 297 |
+
ival_W_to_T_end = Period(
|
| 298 |
+
freq="Min", year=2007, month=1, day=7, hour=23, minute=59
|
| 299 |
+
)
|
| 300 |
+
ival_W_to_S_start = Period(
|
| 301 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 302 |
+
)
|
| 303 |
+
ival_W_to_S_end = Period(
|
| 304 |
+
freq="S", year=2007, month=1, day=7, hour=23, minute=59, second=59
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
assert ival_W.asfreq("A") == ival_W_to_A
|
| 308 |
+
assert ival_W_end_of_year.asfreq("A") == ival_W_to_A_end_of_year
|
| 309 |
+
|
| 310 |
+
assert ival_W.asfreq("Q") == ival_W_to_Q
|
| 311 |
+
assert ival_W_end_of_quarter.asfreq("Q") == ival_W_to_Q_end_of_quarter
|
| 312 |
+
|
| 313 |
+
assert ival_W.asfreq("M") == ival_W_to_M
|
| 314 |
+
assert ival_W_end_of_month.asfreq("M") == ival_W_to_M_end_of_month
|
| 315 |
+
|
| 316 |
+
assert ival_W.asfreq("B", "S") == ival_W_to_B_start
|
| 317 |
+
assert ival_W.asfreq("B", "E") == ival_W_to_B_end
|
| 318 |
+
|
| 319 |
+
assert ival_W.asfreq("D", "S") == ival_W_to_D_start
|
| 320 |
+
assert ival_W.asfreq("D", "E") == ival_W_to_D_end
|
| 321 |
+
|
| 322 |
+
assert ival_WSUN.asfreq("D", "S") == ival_WSUN_to_D_start
|
| 323 |
+
assert ival_WSUN.asfreq("D", "E") == ival_WSUN_to_D_end
|
| 324 |
+
assert ival_WSAT.asfreq("D", "S") == ival_WSAT_to_D_start
|
| 325 |
+
assert ival_WSAT.asfreq("D", "E") == ival_WSAT_to_D_end
|
| 326 |
+
assert ival_WFRI.asfreq("D", "S") == ival_WFRI_to_D_start
|
| 327 |
+
assert ival_WFRI.asfreq("D", "E") == ival_WFRI_to_D_end
|
| 328 |
+
assert ival_WTHU.asfreq("D", "S") == ival_WTHU_to_D_start
|
| 329 |
+
assert ival_WTHU.asfreq("D", "E") == ival_WTHU_to_D_end
|
| 330 |
+
assert ival_WWED.asfreq("D", "S") == ival_WWED_to_D_start
|
| 331 |
+
assert ival_WWED.asfreq("D", "E") == ival_WWED_to_D_end
|
| 332 |
+
assert ival_WTUE.asfreq("D", "S") == ival_WTUE_to_D_start
|
| 333 |
+
assert ival_WTUE.asfreq("D", "E") == ival_WTUE_to_D_end
|
| 334 |
+
assert ival_WMON.asfreq("D", "S") == ival_WMON_to_D_start
|
| 335 |
+
assert ival_WMON.asfreq("D", "E") == ival_WMON_to_D_end
|
| 336 |
+
|
| 337 |
+
assert ival_W.asfreq("H", "S") == ival_W_to_H_start
|
| 338 |
+
assert ival_W.asfreq("H", "E") == ival_W_to_H_end
|
| 339 |
+
assert ival_W.asfreq("Min", "S") == ival_W_to_T_start
|
| 340 |
+
assert ival_W.asfreq("Min", "E") == ival_W_to_T_end
|
| 341 |
+
assert ival_W.asfreq("S", "S") == ival_W_to_S_start
|
| 342 |
+
assert ival_W.asfreq("S", "E") == ival_W_to_S_end
|
| 343 |
+
|
| 344 |
+
assert ival_W.asfreq("W") == ival_W
|
| 345 |
+
|
| 346 |
+
msg = INVALID_FREQ_ERR_MSG
|
| 347 |
+
with pytest.raises(ValueError, match=msg):
|
| 348 |
+
ival_W.asfreq("WK")
|
| 349 |
+
|
| 350 |
+
def test_conv_weekly_legacy(self):
|
| 351 |
+
# frequency conversion tests: from Weekly Frequency
|
| 352 |
+
msg = INVALID_FREQ_ERR_MSG
|
| 353 |
+
with pytest.raises(ValueError, match=msg):
|
| 354 |
+
Period(freq="WK", year=2007, month=1, day=1)
|
| 355 |
+
|
| 356 |
+
with pytest.raises(ValueError, match=msg):
|
| 357 |
+
Period(freq="WK-SAT", year=2007, month=1, day=6)
|
| 358 |
+
with pytest.raises(ValueError, match=msg):
|
| 359 |
+
Period(freq="WK-FRI", year=2007, month=1, day=5)
|
| 360 |
+
with pytest.raises(ValueError, match=msg):
|
| 361 |
+
Period(freq="WK-THU", year=2007, month=1, day=4)
|
| 362 |
+
with pytest.raises(ValueError, match=msg):
|
| 363 |
+
Period(freq="WK-WED", year=2007, month=1, day=3)
|
| 364 |
+
with pytest.raises(ValueError, match=msg):
|
| 365 |
+
Period(freq="WK-TUE", year=2007, month=1, day=2)
|
| 366 |
+
with pytest.raises(ValueError, match=msg):
|
| 367 |
+
Period(freq="WK-MON", year=2007, month=1, day=1)
|
| 368 |
+
|
| 369 |
+
def test_conv_business(self):
|
| 370 |
+
# frequency conversion tests: from Business Frequency"
|
| 371 |
+
|
| 372 |
+
ival_B = Period(freq="B", year=2007, month=1, day=1)
|
| 373 |
+
ival_B_end_of_year = Period(freq="B", year=2007, month=12, day=31)
|
| 374 |
+
ival_B_end_of_quarter = Period(freq="B", year=2007, month=3, day=30)
|
| 375 |
+
ival_B_end_of_month = Period(freq="B", year=2007, month=1, day=31)
|
| 376 |
+
ival_B_end_of_week = Period(freq="B", year=2007, month=1, day=5)
|
| 377 |
+
|
| 378 |
+
ival_B_to_A = Period(freq="A", year=2007)
|
| 379 |
+
ival_B_to_Q = Period(freq="Q", year=2007, quarter=1)
|
| 380 |
+
ival_B_to_M = Period(freq="M", year=2007, month=1)
|
| 381 |
+
ival_B_to_W = Period(freq="W", year=2007, month=1, day=7)
|
| 382 |
+
ival_B_to_D = Period(freq="D", year=2007, month=1, day=1)
|
| 383 |
+
ival_B_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 384 |
+
ival_B_to_H_end = Period(freq="H", year=2007, month=1, day=1, hour=23)
|
| 385 |
+
ival_B_to_T_start = Period(
|
| 386 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0
|
| 387 |
+
)
|
| 388 |
+
ival_B_to_T_end = Period(
|
| 389 |
+
freq="Min", year=2007, month=1, day=1, hour=23, minute=59
|
| 390 |
+
)
|
| 391 |
+
ival_B_to_S_start = Period(
|
| 392 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 393 |
+
)
|
| 394 |
+
ival_B_to_S_end = Period(
|
| 395 |
+
freq="S", year=2007, month=1, day=1, hour=23, minute=59, second=59
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
assert ival_B.asfreq("A") == ival_B_to_A
|
| 399 |
+
assert ival_B_end_of_year.asfreq("A") == ival_B_to_A
|
| 400 |
+
assert ival_B.asfreq("Q") == ival_B_to_Q
|
| 401 |
+
assert ival_B_end_of_quarter.asfreq("Q") == ival_B_to_Q
|
| 402 |
+
assert ival_B.asfreq("M") == ival_B_to_M
|
| 403 |
+
assert ival_B_end_of_month.asfreq("M") == ival_B_to_M
|
| 404 |
+
assert ival_B.asfreq("W") == ival_B_to_W
|
| 405 |
+
assert ival_B_end_of_week.asfreq("W") == ival_B_to_W
|
| 406 |
+
|
| 407 |
+
assert ival_B.asfreq("D") == ival_B_to_D
|
| 408 |
+
|
| 409 |
+
assert ival_B.asfreq("H", "S") == ival_B_to_H_start
|
| 410 |
+
assert ival_B.asfreq("H", "E") == ival_B_to_H_end
|
| 411 |
+
assert ival_B.asfreq("Min", "S") == ival_B_to_T_start
|
| 412 |
+
assert ival_B.asfreq("Min", "E") == ival_B_to_T_end
|
| 413 |
+
assert ival_B.asfreq("S", "S") == ival_B_to_S_start
|
| 414 |
+
assert ival_B.asfreq("S", "E") == ival_B_to_S_end
|
| 415 |
+
|
| 416 |
+
assert ival_B.asfreq("B") == ival_B
|
| 417 |
+
|
| 418 |
+
def test_conv_daily(self):
|
| 419 |
+
# frequency conversion tests: from Business Frequency"
|
| 420 |
+
|
| 421 |
+
ival_D = Period(freq="D", year=2007, month=1, day=1)
|
| 422 |
+
ival_D_end_of_year = Period(freq="D", year=2007, month=12, day=31)
|
| 423 |
+
ival_D_end_of_quarter = Period(freq="D", year=2007, month=3, day=31)
|
| 424 |
+
ival_D_end_of_month = Period(freq="D", year=2007, month=1, day=31)
|
| 425 |
+
ival_D_end_of_week = Period(freq="D", year=2007, month=1, day=7)
|
| 426 |
+
|
| 427 |
+
ival_D_friday = Period(freq="D", year=2007, month=1, day=5)
|
| 428 |
+
ival_D_saturday = Period(freq="D", year=2007, month=1, day=6)
|
| 429 |
+
ival_D_sunday = Period(freq="D", year=2007, month=1, day=7)
|
| 430 |
+
|
| 431 |
+
ival_B_friday = Period(freq="B", year=2007, month=1, day=5)
|
| 432 |
+
ival_B_monday = Period(freq="B", year=2007, month=1, day=8)
|
| 433 |
+
|
| 434 |
+
ival_D_to_A = Period(freq="A", year=2007)
|
| 435 |
+
|
| 436 |
+
ival_Deoq_to_AJAN = Period(freq="A-JAN", year=2008)
|
| 437 |
+
ival_Deoq_to_AJUN = Period(freq="A-JUN", year=2007)
|
| 438 |
+
ival_Deoq_to_ADEC = Period(freq="A-DEC", year=2007)
|
| 439 |
+
|
| 440 |
+
ival_D_to_QEJAN = Period(freq="Q-JAN", year=2007, quarter=4)
|
| 441 |
+
ival_D_to_QEJUN = Period(freq="Q-JUN", year=2007, quarter=3)
|
| 442 |
+
ival_D_to_QEDEC = Period(freq="Q-DEC", year=2007, quarter=1)
|
| 443 |
+
|
| 444 |
+
ival_D_to_M = Period(freq="M", year=2007, month=1)
|
| 445 |
+
ival_D_to_W = Period(freq="W", year=2007, month=1, day=7)
|
| 446 |
+
|
| 447 |
+
ival_D_to_H_start = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 448 |
+
ival_D_to_H_end = Period(freq="H", year=2007, month=1, day=1, hour=23)
|
| 449 |
+
ival_D_to_T_start = Period(
|
| 450 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0
|
| 451 |
+
)
|
| 452 |
+
ival_D_to_T_end = Period(
|
| 453 |
+
freq="Min", year=2007, month=1, day=1, hour=23, minute=59
|
| 454 |
+
)
|
| 455 |
+
ival_D_to_S_start = Period(
|
| 456 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 457 |
+
)
|
| 458 |
+
ival_D_to_S_end = Period(
|
| 459 |
+
freq="S", year=2007, month=1, day=1, hour=23, minute=59, second=59
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
assert ival_D.asfreq("A") == ival_D_to_A
|
| 463 |
+
|
| 464 |
+
assert ival_D_end_of_quarter.asfreq("A-JAN") == ival_Deoq_to_AJAN
|
| 465 |
+
assert ival_D_end_of_quarter.asfreq("A-JUN") == ival_Deoq_to_AJUN
|
| 466 |
+
assert ival_D_end_of_quarter.asfreq("A-DEC") == ival_Deoq_to_ADEC
|
| 467 |
+
|
| 468 |
+
assert ival_D_end_of_year.asfreq("A") == ival_D_to_A
|
| 469 |
+
assert ival_D_end_of_quarter.asfreq("Q") == ival_D_to_QEDEC
|
| 470 |
+
assert ival_D.asfreq("Q-JAN") == ival_D_to_QEJAN
|
| 471 |
+
assert ival_D.asfreq("Q-JUN") == ival_D_to_QEJUN
|
| 472 |
+
assert ival_D.asfreq("Q-DEC") == ival_D_to_QEDEC
|
| 473 |
+
assert ival_D.asfreq("M") == ival_D_to_M
|
| 474 |
+
assert ival_D_end_of_month.asfreq("M") == ival_D_to_M
|
| 475 |
+
assert ival_D.asfreq("W") == ival_D_to_W
|
| 476 |
+
assert ival_D_end_of_week.asfreq("W") == ival_D_to_W
|
| 477 |
+
|
| 478 |
+
assert ival_D_friday.asfreq("B") == ival_B_friday
|
| 479 |
+
assert ival_D_saturday.asfreq("B", "S") == ival_B_friday
|
| 480 |
+
assert ival_D_saturday.asfreq("B", "E") == ival_B_monday
|
| 481 |
+
assert ival_D_sunday.asfreq("B", "S") == ival_B_friday
|
| 482 |
+
assert ival_D_sunday.asfreq("B", "E") == ival_B_monday
|
| 483 |
+
|
| 484 |
+
assert ival_D.asfreq("H", "S") == ival_D_to_H_start
|
| 485 |
+
assert ival_D.asfreq("H", "E") == ival_D_to_H_end
|
| 486 |
+
assert ival_D.asfreq("Min", "S") == ival_D_to_T_start
|
| 487 |
+
assert ival_D.asfreq("Min", "E") == ival_D_to_T_end
|
| 488 |
+
assert ival_D.asfreq("S", "S") == ival_D_to_S_start
|
| 489 |
+
assert ival_D.asfreq("S", "E") == ival_D_to_S_end
|
| 490 |
+
|
| 491 |
+
assert ival_D.asfreq("D") == ival_D
|
| 492 |
+
|
| 493 |
+
def test_conv_hourly(self):
|
| 494 |
+
# frequency conversion tests: from Hourly Frequency"
|
| 495 |
+
|
| 496 |
+
ival_H = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 497 |
+
ival_H_end_of_year = Period(freq="H", year=2007, month=12, day=31, hour=23)
|
| 498 |
+
ival_H_end_of_quarter = Period(freq="H", year=2007, month=3, day=31, hour=23)
|
| 499 |
+
ival_H_end_of_month = Period(freq="H", year=2007, month=1, day=31, hour=23)
|
| 500 |
+
ival_H_end_of_week = Period(freq="H", year=2007, month=1, day=7, hour=23)
|
| 501 |
+
ival_H_end_of_day = Period(freq="H", year=2007, month=1, day=1, hour=23)
|
| 502 |
+
ival_H_end_of_bus = Period(freq="H", year=2007, month=1, day=1, hour=23)
|
| 503 |
+
|
| 504 |
+
ival_H_to_A = Period(freq="A", year=2007)
|
| 505 |
+
ival_H_to_Q = Period(freq="Q", year=2007, quarter=1)
|
| 506 |
+
ival_H_to_M = Period(freq="M", year=2007, month=1)
|
| 507 |
+
ival_H_to_W = Period(freq="W", year=2007, month=1, day=7)
|
| 508 |
+
ival_H_to_D = Period(freq="D", year=2007, month=1, day=1)
|
| 509 |
+
ival_H_to_B = Period(freq="B", year=2007, month=1, day=1)
|
| 510 |
+
|
| 511 |
+
ival_H_to_T_start = Period(
|
| 512 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0
|
| 513 |
+
)
|
| 514 |
+
ival_H_to_T_end = Period(
|
| 515 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=59
|
| 516 |
+
)
|
| 517 |
+
ival_H_to_S_start = Period(
|
| 518 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 519 |
+
)
|
| 520 |
+
ival_H_to_S_end = Period(
|
| 521 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=59, second=59
|
| 522 |
+
)
|
| 523 |
+
|
| 524 |
+
assert ival_H.asfreq("A") == ival_H_to_A
|
| 525 |
+
assert ival_H_end_of_year.asfreq("A") == ival_H_to_A
|
| 526 |
+
assert ival_H.asfreq("Q") == ival_H_to_Q
|
| 527 |
+
assert ival_H_end_of_quarter.asfreq("Q") == ival_H_to_Q
|
| 528 |
+
assert ival_H.asfreq("M") == ival_H_to_M
|
| 529 |
+
assert ival_H_end_of_month.asfreq("M") == ival_H_to_M
|
| 530 |
+
assert ival_H.asfreq("W") == ival_H_to_W
|
| 531 |
+
assert ival_H_end_of_week.asfreq("W") == ival_H_to_W
|
| 532 |
+
assert ival_H.asfreq("D") == ival_H_to_D
|
| 533 |
+
assert ival_H_end_of_day.asfreq("D") == ival_H_to_D
|
| 534 |
+
assert ival_H.asfreq("B") == ival_H_to_B
|
| 535 |
+
assert ival_H_end_of_bus.asfreq("B") == ival_H_to_B
|
| 536 |
+
|
| 537 |
+
assert ival_H.asfreq("Min", "S") == ival_H_to_T_start
|
| 538 |
+
assert ival_H.asfreq("Min", "E") == ival_H_to_T_end
|
| 539 |
+
assert ival_H.asfreq("S", "S") == ival_H_to_S_start
|
| 540 |
+
assert ival_H.asfreq("S", "E") == ival_H_to_S_end
|
| 541 |
+
|
| 542 |
+
assert ival_H.asfreq("H") == ival_H
|
| 543 |
+
|
| 544 |
+
def test_conv_minutely(self):
|
| 545 |
+
# frequency conversion tests: from Minutely Frequency"
|
| 546 |
+
|
| 547 |
+
ival_T = Period(freq="Min", year=2007, month=1, day=1, hour=0, minute=0)
|
| 548 |
+
ival_T_end_of_year = Period(
|
| 549 |
+
freq="Min", year=2007, month=12, day=31, hour=23, minute=59
|
| 550 |
+
)
|
| 551 |
+
ival_T_end_of_quarter = Period(
|
| 552 |
+
freq="Min", year=2007, month=3, day=31, hour=23, minute=59
|
| 553 |
+
)
|
| 554 |
+
ival_T_end_of_month = Period(
|
| 555 |
+
freq="Min", year=2007, month=1, day=31, hour=23, minute=59
|
| 556 |
+
)
|
| 557 |
+
ival_T_end_of_week = Period(
|
| 558 |
+
freq="Min", year=2007, month=1, day=7, hour=23, minute=59
|
| 559 |
+
)
|
| 560 |
+
ival_T_end_of_day = Period(
|
| 561 |
+
freq="Min", year=2007, month=1, day=1, hour=23, minute=59
|
| 562 |
+
)
|
| 563 |
+
ival_T_end_of_bus = Period(
|
| 564 |
+
freq="Min", year=2007, month=1, day=1, hour=23, minute=59
|
| 565 |
+
)
|
| 566 |
+
ival_T_end_of_hour = Period(
|
| 567 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=59
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
ival_T_to_A = Period(freq="A", year=2007)
|
| 571 |
+
ival_T_to_Q = Period(freq="Q", year=2007, quarter=1)
|
| 572 |
+
ival_T_to_M = Period(freq="M", year=2007, month=1)
|
| 573 |
+
ival_T_to_W = Period(freq="W", year=2007, month=1, day=7)
|
| 574 |
+
ival_T_to_D = Period(freq="D", year=2007, month=1, day=1)
|
| 575 |
+
ival_T_to_B = Period(freq="B", year=2007, month=1, day=1)
|
| 576 |
+
ival_T_to_H = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 577 |
+
|
| 578 |
+
ival_T_to_S_start = Period(
|
| 579 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 580 |
+
)
|
| 581 |
+
ival_T_to_S_end = Period(
|
| 582 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=59
|
| 583 |
+
)
|
| 584 |
+
|
| 585 |
+
assert ival_T.asfreq("A") == ival_T_to_A
|
| 586 |
+
assert ival_T_end_of_year.asfreq("A") == ival_T_to_A
|
| 587 |
+
assert ival_T.asfreq("Q") == ival_T_to_Q
|
| 588 |
+
assert ival_T_end_of_quarter.asfreq("Q") == ival_T_to_Q
|
| 589 |
+
assert ival_T.asfreq("M") == ival_T_to_M
|
| 590 |
+
assert ival_T_end_of_month.asfreq("M") == ival_T_to_M
|
| 591 |
+
assert ival_T.asfreq("W") == ival_T_to_W
|
| 592 |
+
assert ival_T_end_of_week.asfreq("W") == ival_T_to_W
|
| 593 |
+
assert ival_T.asfreq("D") == ival_T_to_D
|
| 594 |
+
assert ival_T_end_of_day.asfreq("D") == ival_T_to_D
|
| 595 |
+
assert ival_T.asfreq("B") == ival_T_to_B
|
| 596 |
+
assert ival_T_end_of_bus.asfreq("B") == ival_T_to_B
|
| 597 |
+
assert ival_T.asfreq("H") == ival_T_to_H
|
| 598 |
+
assert ival_T_end_of_hour.asfreq("H") == ival_T_to_H
|
| 599 |
+
|
| 600 |
+
assert ival_T.asfreq("S", "S") == ival_T_to_S_start
|
| 601 |
+
assert ival_T.asfreq("S", "E") == ival_T_to_S_end
|
| 602 |
+
|
| 603 |
+
assert ival_T.asfreq("Min") == ival_T
|
| 604 |
+
|
| 605 |
+
def test_conv_secondly(self):
|
| 606 |
+
# frequency conversion tests: from Secondly Frequency"
|
| 607 |
+
|
| 608 |
+
ival_S = Period(freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=0)
|
| 609 |
+
ival_S_end_of_year = Period(
|
| 610 |
+
freq="S", year=2007, month=12, day=31, hour=23, minute=59, second=59
|
| 611 |
+
)
|
| 612 |
+
ival_S_end_of_quarter = Period(
|
| 613 |
+
freq="S", year=2007, month=3, day=31, hour=23, minute=59, second=59
|
| 614 |
+
)
|
| 615 |
+
ival_S_end_of_month = Period(
|
| 616 |
+
freq="S", year=2007, month=1, day=31, hour=23, minute=59, second=59
|
| 617 |
+
)
|
| 618 |
+
ival_S_end_of_week = Period(
|
| 619 |
+
freq="S", year=2007, month=1, day=7, hour=23, minute=59, second=59
|
| 620 |
+
)
|
| 621 |
+
ival_S_end_of_day = Period(
|
| 622 |
+
freq="S", year=2007, month=1, day=1, hour=23, minute=59, second=59
|
| 623 |
+
)
|
| 624 |
+
ival_S_end_of_bus = Period(
|
| 625 |
+
freq="S", year=2007, month=1, day=1, hour=23, minute=59, second=59
|
| 626 |
+
)
|
| 627 |
+
ival_S_end_of_hour = Period(
|
| 628 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=59, second=59
|
| 629 |
+
)
|
| 630 |
+
ival_S_end_of_minute = Period(
|
| 631 |
+
freq="S", year=2007, month=1, day=1, hour=0, minute=0, second=59
|
| 632 |
+
)
|
| 633 |
+
|
| 634 |
+
ival_S_to_A = Period(freq="A", year=2007)
|
| 635 |
+
ival_S_to_Q = Period(freq="Q", year=2007, quarter=1)
|
| 636 |
+
ival_S_to_M = Period(freq="M", year=2007, month=1)
|
| 637 |
+
ival_S_to_W = Period(freq="W", year=2007, month=1, day=7)
|
| 638 |
+
ival_S_to_D = Period(freq="D", year=2007, month=1, day=1)
|
| 639 |
+
ival_S_to_B = Period(freq="B", year=2007, month=1, day=1)
|
| 640 |
+
ival_S_to_H = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 641 |
+
ival_S_to_T = Period(freq="Min", year=2007, month=1, day=1, hour=0, minute=0)
|
| 642 |
+
|
| 643 |
+
assert ival_S.asfreq("A") == ival_S_to_A
|
| 644 |
+
assert ival_S_end_of_year.asfreq("A") == ival_S_to_A
|
| 645 |
+
assert ival_S.asfreq("Q") == ival_S_to_Q
|
| 646 |
+
assert ival_S_end_of_quarter.asfreq("Q") == ival_S_to_Q
|
| 647 |
+
assert ival_S.asfreq("M") == ival_S_to_M
|
| 648 |
+
assert ival_S_end_of_month.asfreq("M") == ival_S_to_M
|
| 649 |
+
assert ival_S.asfreq("W") == ival_S_to_W
|
| 650 |
+
assert ival_S_end_of_week.asfreq("W") == ival_S_to_W
|
| 651 |
+
assert ival_S.asfreq("D") == ival_S_to_D
|
| 652 |
+
assert ival_S_end_of_day.asfreq("D") == ival_S_to_D
|
| 653 |
+
assert ival_S.asfreq("B") == ival_S_to_B
|
| 654 |
+
assert ival_S_end_of_bus.asfreq("B") == ival_S_to_B
|
| 655 |
+
assert ival_S.asfreq("H") == ival_S_to_H
|
| 656 |
+
assert ival_S_end_of_hour.asfreq("H") == ival_S_to_H
|
| 657 |
+
assert ival_S.asfreq("Min") == ival_S_to_T
|
| 658 |
+
assert ival_S_end_of_minute.asfreq("Min") == ival_S_to_T
|
| 659 |
+
|
| 660 |
+
assert ival_S.asfreq("S") == ival_S
|
| 661 |
+
|
| 662 |
+
def test_conv_microsecond(self):
|
| 663 |
+
# GH#31475 Avoid floating point errors dropping the start_time to
|
| 664 |
+
# before the beginning of the Period
|
| 665 |
+
per = Period("2020-01-30 15:57:27.576166", freq="U")
|
| 666 |
+
assert per.ordinal == 1580399847576166
|
| 667 |
+
|
| 668 |
+
start = per.start_time
|
| 669 |
+
expected = Timestamp("2020-01-30 15:57:27.576166")
|
| 670 |
+
assert start == expected
|
| 671 |
+
assert start._value == per.ordinal * 1000
|
| 672 |
+
|
| 673 |
+
per2 = Period("2300-01-01", "us")
|
| 674 |
+
msg = "2300-01-01"
|
| 675 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 676 |
+
per2.start_time
|
| 677 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 678 |
+
per2.end_time
|
| 679 |
+
|
| 680 |
+
def test_asfreq_mult(self):
|
| 681 |
+
# normal freq to mult freq
|
| 682 |
+
p = Period(freq="A", year=2007)
|
| 683 |
+
# ordinal will not change
|
| 684 |
+
for freq in ["3A", offsets.YearEnd(3)]:
|
| 685 |
+
result = p.asfreq(freq)
|
| 686 |
+
expected = Period("2007", freq="3A")
|
| 687 |
+
|
| 688 |
+
assert result == expected
|
| 689 |
+
assert result.ordinal == expected.ordinal
|
| 690 |
+
assert result.freq == expected.freq
|
| 691 |
+
# ordinal will not change
|
| 692 |
+
for freq in ["3A", offsets.YearEnd(3)]:
|
| 693 |
+
result = p.asfreq(freq, how="S")
|
| 694 |
+
expected = Period("2007", freq="3A")
|
| 695 |
+
|
| 696 |
+
assert result == expected
|
| 697 |
+
assert result.ordinal == expected.ordinal
|
| 698 |
+
assert result.freq == expected.freq
|
| 699 |
+
|
| 700 |
+
# mult freq to normal freq
|
| 701 |
+
p = Period(freq="3A", year=2007)
|
| 702 |
+
# ordinal will change because how=E is the default
|
| 703 |
+
for freq in ["A", offsets.YearEnd()]:
|
| 704 |
+
result = p.asfreq(freq)
|
| 705 |
+
expected = Period("2009", freq="A")
|
| 706 |
+
|
| 707 |
+
assert result == expected
|
| 708 |
+
assert result.ordinal == expected.ordinal
|
| 709 |
+
assert result.freq == expected.freq
|
| 710 |
+
# ordinal will not change
|
| 711 |
+
for freq in ["A", offsets.YearEnd()]:
|
| 712 |
+
result = p.asfreq(freq, how="S")
|
| 713 |
+
expected = Period("2007", freq="A")
|
| 714 |
+
|
| 715 |
+
assert result == expected
|
| 716 |
+
assert result.ordinal == expected.ordinal
|
| 717 |
+
assert result.freq == expected.freq
|
| 718 |
+
|
| 719 |
+
p = Period(freq="A", year=2007)
|
| 720 |
+
for freq in ["2M", offsets.MonthEnd(2)]:
|
| 721 |
+
result = p.asfreq(freq)
|
| 722 |
+
expected = Period("2007-12", freq="2M")
|
| 723 |
+
|
| 724 |
+
assert result == expected
|
| 725 |
+
assert result.ordinal == expected.ordinal
|
| 726 |
+
assert result.freq == expected.freq
|
| 727 |
+
for freq in ["2M", offsets.MonthEnd(2)]:
|
| 728 |
+
result = p.asfreq(freq, how="S")
|
| 729 |
+
expected = Period("2007-01", freq="2M")
|
| 730 |
+
|
| 731 |
+
assert result == expected
|
| 732 |
+
assert result.ordinal == expected.ordinal
|
| 733 |
+
assert result.freq == expected.freq
|
| 734 |
+
|
| 735 |
+
p = Period(freq="3A", year=2007)
|
| 736 |
+
for freq in ["2M", offsets.MonthEnd(2)]:
|
| 737 |
+
result = p.asfreq(freq)
|
| 738 |
+
expected = Period("2009-12", freq="2M")
|
| 739 |
+
|
| 740 |
+
assert result == expected
|
| 741 |
+
assert result.ordinal == expected.ordinal
|
| 742 |
+
assert result.freq == expected.freq
|
| 743 |
+
for freq in ["2M", offsets.MonthEnd(2)]:
|
| 744 |
+
result = p.asfreq(freq, how="S")
|
| 745 |
+
expected = Period("2007-01", freq="2M")
|
| 746 |
+
|
| 747 |
+
assert result == expected
|
| 748 |
+
assert result.ordinal == expected.ordinal
|
| 749 |
+
assert result.freq == expected.freq
|
| 750 |
+
|
| 751 |
+
def test_asfreq_combined(self):
|
| 752 |
+
# normal freq to combined freq
|
| 753 |
+
p = Period("2007", freq="H")
|
| 754 |
+
|
| 755 |
+
# ordinal will not change
|
| 756 |
+
expected = Period("2007", freq="25H")
|
| 757 |
+
for freq, how in zip(["1D1H", "1H1D"], ["E", "S"]):
|
| 758 |
+
result = p.asfreq(freq, how=how)
|
| 759 |
+
assert result == expected
|
| 760 |
+
assert result.ordinal == expected.ordinal
|
| 761 |
+
assert result.freq == expected.freq
|
| 762 |
+
|
| 763 |
+
# combined freq to normal freq
|
| 764 |
+
p1 = Period(freq="1D1H", year=2007)
|
| 765 |
+
p2 = Period(freq="1H1D", year=2007)
|
| 766 |
+
|
| 767 |
+
# ordinal will change because how=E is the default
|
| 768 |
+
result1 = p1.asfreq("H")
|
| 769 |
+
result2 = p2.asfreq("H")
|
| 770 |
+
expected = Period("2007-01-02", freq="H")
|
| 771 |
+
assert result1 == expected
|
| 772 |
+
assert result1.ordinal == expected.ordinal
|
| 773 |
+
assert result1.freq == expected.freq
|
| 774 |
+
assert result2 == expected
|
| 775 |
+
assert result2.ordinal == expected.ordinal
|
| 776 |
+
assert result2.freq == expected.freq
|
| 777 |
+
|
| 778 |
+
# ordinal will not change
|
| 779 |
+
result1 = p1.asfreq("H", how="S")
|
| 780 |
+
result2 = p2.asfreq("H", how="S")
|
| 781 |
+
expected = Period("2007-01-01", freq="H")
|
| 782 |
+
assert result1 == expected
|
| 783 |
+
assert result1.ordinal == expected.ordinal
|
| 784 |
+
assert result1.freq == expected.freq
|
| 785 |
+
assert result2 == expected
|
| 786 |
+
assert result2.ordinal == expected.ordinal
|
| 787 |
+
assert result2.freq == expected.freq
|
| 788 |
+
|
| 789 |
+
def test_asfreq_MS(self):
|
| 790 |
+
initial = Period("2013")
|
| 791 |
+
|
| 792 |
+
assert initial.asfreq(freq="M", how="S") == Period("2013-01", "M")
|
| 793 |
+
|
| 794 |
+
msg = INVALID_FREQ_ERR_MSG
|
| 795 |
+
with pytest.raises(ValueError, match=msg):
|
| 796 |
+
initial.asfreq(freq="MS", how="S")
|
| 797 |
+
|
| 798 |
+
with pytest.raises(ValueError, match=msg):
|
| 799 |
+
Period("2013-01", "MS")
|
| 800 |
+
|
| 801 |
+
assert _period_code_map.get("MS") is None
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/period/test_period.py
ADDED
|
@@ -0,0 +1,1570 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import (
|
| 2 |
+
date,
|
| 3 |
+
datetime,
|
| 4 |
+
timedelta,
|
| 5 |
+
)
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pytest
|
| 9 |
+
|
| 10 |
+
from pandas._libs.tslibs import (
|
| 11 |
+
iNaT,
|
| 12 |
+
period as libperiod,
|
| 13 |
+
)
|
| 14 |
+
from pandas._libs.tslibs.ccalendar import (
|
| 15 |
+
DAYS,
|
| 16 |
+
MONTHS,
|
| 17 |
+
)
|
| 18 |
+
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
|
| 19 |
+
from pandas._libs.tslibs.parsing import DateParseError
|
| 20 |
+
from pandas._libs.tslibs.period import (
|
| 21 |
+
INVALID_FREQ_ERR_MSG,
|
| 22 |
+
IncompatibleFrequency,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
import pandas as pd
|
| 26 |
+
from pandas import (
|
| 27 |
+
NaT,
|
| 28 |
+
Period,
|
| 29 |
+
Timedelta,
|
| 30 |
+
Timestamp,
|
| 31 |
+
offsets,
|
| 32 |
+
)
|
| 33 |
+
import pandas._testing as tm
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class TestPeriodConstruction:
|
| 37 |
+
def test_from_td64nat_raises(self):
|
| 38 |
+
# GH#44507
|
| 39 |
+
td = NaT.to_numpy("m8[ns]")
|
| 40 |
+
|
| 41 |
+
msg = "Value must be Period, string, integer, or datetime"
|
| 42 |
+
with pytest.raises(ValueError, match=msg):
|
| 43 |
+
Period(td)
|
| 44 |
+
|
| 45 |
+
with pytest.raises(ValueError, match=msg):
|
| 46 |
+
Period(td, freq="D")
|
| 47 |
+
|
| 48 |
+
def test_construction(self):
|
| 49 |
+
i1 = Period("1/1/2005", freq="M")
|
| 50 |
+
i2 = Period("Jan 2005")
|
| 51 |
+
|
| 52 |
+
assert i1 == i2
|
| 53 |
+
|
| 54 |
+
i1 = Period("2005", freq="A")
|
| 55 |
+
i2 = Period("2005")
|
| 56 |
+
i3 = Period("2005", freq="a")
|
| 57 |
+
|
| 58 |
+
assert i1 == i2
|
| 59 |
+
assert i1 == i3
|
| 60 |
+
|
| 61 |
+
i4 = Period("2005", freq="M")
|
| 62 |
+
i5 = Period("2005", freq="m")
|
| 63 |
+
|
| 64 |
+
assert i1 != i4
|
| 65 |
+
assert i4 == i5
|
| 66 |
+
|
| 67 |
+
i1 = Period.now("Q")
|
| 68 |
+
i2 = Period(datetime.now(), freq="Q")
|
| 69 |
+
i3 = Period.now("q")
|
| 70 |
+
|
| 71 |
+
assert i1 == i2
|
| 72 |
+
assert i1 == i3
|
| 73 |
+
|
| 74 |
+
i1 = Period.now("D")
|
| 75 |
+
i2 = Period(datetime.now(), freq="D")
|
| 76 |
+
i3 = Period.now(offsets.Day())
|
| 77 |
+
|
| 78 |
+
assert i1 == i2
|
| 79 |
+
assert i1 == i3
|
| 80 |
+
|
| 81 |
+
i1 = Period("1982", freq="min")
|
| 82 |
+
i2 = Period("1982", freq="MIN")
|
| 83 |
+
assert i1 == i2
|
| 84 |
+
|
| 85 |
+
i1 = Period(year=2005, month=3, day=1, freq="D")
|
| 86 |
+
i2 = Period("3/1/2005", freq="D")
|
| 87 |
+
assert i1 == i2
|
| 88 |
+
|
| 89 |
+
i3 = Period(year=2005, month=3, day=1, freq="d")
|
| 90 |
+
assert i1 == i3
|
| 91 |
+
|
| 92 |
+
i1 = Period("2007-01-01 09:00:00.001")
|
| 93 |
+
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq="L")
|
| 94 |
+
assert i1 == expected
|
| 95 |
+
|
| 96 |
+
expected = Period("2007-01-01 09:00:00.001", freq="L")
|
| 97 |
+
assert i1 == expected
|
| 98 |
+
|
| 99 |
+
i1 = Period("2007-01-01 09:00:00.00101")
|
| 100 |
+
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq="U")
|
| 101 |
+
assert i1 == expected
|
| 102 |
+
|
| 103 |
+
expected = Period("2007-01-01 09:00:00.00101", freq="U")
|
| 104 |
+
assert i1 == expected
|
| 105 |
+
|
| 106 |
+
msg = "Must supply freq for ordinal value"
|
| 107 |
+
with pytest.raises(ValueError, match=msg):
|
| 108 |
+
Period(ordinal=200701)
|
| 109 |
+
|
| 110 |
+
msg = "Invalid frequency: X"
|
| 111 |
+
with pytest.raises(ValueError, match=msg):
|
| 112 |
+
Period("2007-1-1", freq="X")
|
| 113 |
+
|
| 114 |
+
# GH#34703 tuple freq disallowed
|
| 115 |
+
with pytest.raises(TypeError, match="pass as a string instead"):
|
| 116 |
+
Period("1982", freq=("Min", 1))
|
| 117 |
+
|
| 118 |
+
def test_construction_from_timestamp_nanos(self):
|
| 119 |
+
# GH#46811 don't drop nanos from Timestamp
|
| 120 |
+
ts = Timestamp("2022-04-20 09:23:24.123456789")
|
| 121 |
+
per = Period(ts, freq="ns")
|
| 122 |
+
|
| 123 |
+
# should losslessly round-trip, not lose the 789
|
| 124 |
+
rt = per.to_timestamp()
|
| 125 |
+
assert rt == ts
|
| 126 |
+
|
| 127 |
+
# same thing but from a datetime64 object
|
| 128 |
+
dt64 = ts.asm8
|
| 129 |
+
per2 = Period(dt64, freq="ns")
|
| 130 |
+
rt2 = per2.to_timestamp()
|
| 131 |
+
assert rt2.asm8 == dt64
|
| 132 |
+
|
| 133 |
+
def test_construction_bday(self):
|
| 134 |
+
# Biz day construction, roll forward if non-weekday
|
| 135 |
+
i1 = Period("3/10/12", freq="B")
|
| 136 |
+
i2 = Period("3/10/12", freq="D")
|
| 137 |
+
assert i1 == i2.asfreq("B")
|
| 138 |
+
i2 = Period("3/11/12", freq="D")
|
| 139 |
+
assert i1 == i2.asfreq("B")
|
| 140 |
+
i2 = Period("3/12/12", freq="D")
|
| 141 |
+
assert i1 == i2.asfreq("B")
|
| 142 |
+
|
| 143 |
+
i3 = Period("3/10/12", freq="b")
|
| 144 |
+
assert i1 == i3
|
| 145 |
+
|
| 146 |
+
i1 = Period(year=2012, month=3, day=10, freq="B")
|
| 147 |
+
i2 = Period("3/12/12", freq="B")
|
| 148 |
+
assert i1 == i2
|
| 149 |
+
|
| 150 |
+
def test_construction_quarter(self):
|
| 151 |
+
i1 = Period(year=2005, quarter=1, freq="Q")
|
| 152 |
+
i2 = Period("1/1/2005", freq="Q")
|
| 153 |
+
assert i1 == i2
|
| 154 |
+
|
| 155 |
+
i1 = Period(year=2005, quarter=3, freq="Q")
|
| 156 |
+
i2 = Period("9/1/2005", freq="Q")
|
| 157 |
+
assert i1 == i2
|
| 158 |
+
|
| 159 |
+
i1 = Period("2005Q1")
|
| 160 |
+
i2 = Period(year=2005, quarter=1, freq="Q")
|
| 161 |
+
i3 = Period("2005q1")
|
| 162 |
+
assert i1 == i2
|
| 163 |
+
assert i1 == i3
|
| 164 |
+
|
| 165 |
+
i1 = Period("05Q1")
|
| 166 |
+
assert i1 == i2
|
| 167 |
+
lower = Period("05q1")
|
| 168 |
+
assert i1 == lower
|
| 169 |
+
|
| 170 |
+
i1 = Period("1Q2005")
|
| 171 |
+
assert i1 == i2
|
| 172 |
+
lower = Period("1q2005")
|
| 173 |
+
assert i1 == lower
|
| 174 |
+
|
| 175 |
+
i1 = Period("1Q05")
|
| 176 |
+
assert i1 == i2
|
| 177 |
+
lower = Period("1q05")
|
| 178 |
+
assert i1 == lower
|
| 179 |
+
|
| 180 |
+
i1 = Period("4Q1984")
|
| 181 |
+
assert i1.year == 1984
|
| 182 |
+
lower = Period("4q1984")
|
| 183 |
+
assert i1 == lower
|
| 184 |
+
|
| 185 |
+
def test_construction_month(self):
|
| 186 |
+
expected = Period("2007-01", freq="M")
|
| 187 |
+
i1 = Period("200701", freq="M")
|
| 188 |
+
assert i1 == expected
|
| 189 |
+
|
| 190 |
+
i1 = Period("200701", freq="M")
|
| 191 |
+
assert i1 == expected
|
| 192 |
+
|
| 193 |
+
i1 = Period(200701, freq="M")
|
| 194 |
+
assert i1 == expected
|
| 195 |
+
|
| 196 |
+
i1 = Period(ordinal=200701, freq="M")
|
| 197 |
+
assert i1.year == 18695
|
| 198 |
+
|
| 199 |
+
i1 = Period(datetime(2007, 1, 1), freq="M")
|
| 200 |
+
i2 = Period("200701", freq="M")
|
| 201 |
+
assert i1 == i2
|
| 202 |
+
|
| 203 |
+
i1 = Period(date(2007, 1, 1), freq="M")
|
| 204 |
+
i2 = Period(datetime(2007, 1, 1), freq="M")
|
| 205 |
+
i3 = Period(np.datetime64("2007-01-01"), freq="M")
|
| 206 |
+
i4 = Period("2007-01-01 00:00:00", freq="M")
|
| 207 |
+
i5 = Period("2007-01-01 00:00:00.000", freq="M")
|
| 208 |
+
assert i1 == i2
|
| 209 |
+
assert i1 == i3
|
| 210 |
+
assert i1 == i4
|
| 211 |
+
assert i1 == i5
|
| 212 |
+
|
| 213 |
+
def test_period_constructor_offsets(self):
|
| 214 |
+
assert Period("1/1/2005", freq=offsets.MonthEnd()) == Period(
|
| 215 |
+
"1/1/2005", freq="M"
|
| 216 |
+
)
|
| 217 |
+
assert Period("2005", freq=offsets.YearEnd()) == Period("2005", freq="A")
|
| 218 |
+
assert Period("2005", freq=offsets.MonthEnd()) == Period("2005", freq="M")
|
| 219 |
+
assert Period("3/10/12", freq=offsets.BusinessDay()) == Period(
|
| 220 |
+
"3/10/12", freq="B"
|
| 221 |
+
)
|
| 222 |
+
assert Period("3/10/12", freq=offsets.Day()) == Period("3/10/12", freq="D")
|
| 223 |
+
|
| 224 |
+
assert Period(
|
| 225 |
+
year=2005, quarter=1, freq=offsets.QuarterEnd(startingMonth=12)
|
| 226 |
+
) == Period(year=2005, quarter=1, freq="Q")
|
| 227 |
+
assert Period(
|
| 228 |
+
year=2005, quarter=2, freq=offsets.QuarterEnd(startingMonth=12)
|
| 229 |
+
) == Period(year=2005, quarter=2, freq="Q")
|
| 230 |
+
|
| 231 |
+
assert Period(year=2005, month=3, day=1, freq=offsets.Day()) == Period(
|
| 232 |
+
year=2005, month=3, day=1, freq="D"
|
| 233 |
+
)
|
| 234 |
+
assert Period(year=2012, month=3, day=10, freq=offsets.BDay()) == Period(
|
| 235 |
+
year=2012, month=3, day=10, freq="B"
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
expected = Period("2005-03-01", freq="3D")
|
| 239 |
+
assert Period(year=2005, month=3, day=1, freq=offsets.Day(3)) == expected
|
| 240 |
+
assert Period(year=2005, month=3, day=1, freq="3D") == expected
|
| 241 |
+
|
| 242 |
+
assert Period(year=2012, month=3, day=10, freq=offsets.BDay(3)) == Period(
|
| 243 |
+
year=2012, month=3, day=10, freq="3B"
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
assert Period(200701, freq=offsets.MonthEnd()) == Period(200701, freq="M")
|
| 247 |
+
|
| 248 |
+
i1 = Period(ordinal=200701, freq=offsets.MonthEnd())
|
| 249 |
+
i2 = Period(ordinal=200701, freq="M")
|
| 250 |
+
assert i1 == i2
|
| 251 |
+
assert i1.year == 18695
|
| 252 |
+
assert i2.year == 18695
|
| 253 |
+
|
| 254 |
+
i1 = Period(datetime(2007, 1, 1), freq="M")
|
| 255 |
+
i2 = Period("200701", freq="M")
|
| 256 |
+
assert i1 == i2
|
| 257 |
+
|
| 258 |
+
i1 = Period(date(2007, 1, 1), freq="M")
|
| 259 |
+
i2 = Period(datetime(2007, 1, 1), freq="M")
|
| 260 |
+
i3 = Period(np.datetime64("2007-01-01"), freq="M")
|
| 261 |
+
i4 = Period("2007-01-01 00:00:00", freq="M")
|
| 262 |
+
i5 = Period("2007-01-01 00:00:00.000", freq="M")
|
| 263 |
+
assert i1 == i2
|
| 264 |
+
assert i1 == i3
|
| 265 |
+
assert i1 == i4
|
| 266 |
+
assert i1 == i5
|
| 267 |
+
|
| 268 |
+
i1 = Period("2007-01-01 09:00:00.001")
|
| 269 |
+
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq="L")
|
| 270 |
+
assert i1 == expected
|
| 271 |
+
|
| 272 |
+
expected = Period("2007-01-01 09:00:00.001", freq="L")
|
| 273 |
+
assert i1 == expected
|
| 274 |
+
|
| 275 |
+
i1 = Period("2007-01-01 09:00:00.00101")
|
| 276 |
+
expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq="U")
|
| 277 |
+
assert i1 == expected
|
| 278 |
+
|
| 279 |
+
expected = Period("2007-01-01 09:00:00.00101", freq="U")
|
| 280 |
+
assert i1 == expected
|
| 281 |
+
|
| 282 |
+
def test_invalid_arguments(self):
|
| 283 |
+
msg = "Must supply freq for datetime value"
|
| 284 |
+
with pytest.raises(ValueError, match=msg):
|
| 285 |
+
Period(datetime.now())
|
| 286 |
+
with pytest.raises(ValueError, match=msg):
|
| 287 |
+
Period(datetime.now().date())
|
| 288 |
+
|
| 289 |
+
msg = "Value must be Period, string, integer, or datetime"
|
| 290 |
+
with pytest.raises(ValueError, match=msg):
|
| 291 |
+
Period(1.6, freq="D")
|
| 292 |
+
msg = "Ordinal must be an integer"
|
| 293 |
+
with pytest.raises(ValueError, match=msg):
|
| 294 |
+
Period(ordinal=1.6, freq="D")
|
| 295 |
+
msg = "Only value or ordinal but not both should be given but not both"
|
| 296 |
+
with pytest.raises(ValueError, match=msg):
|
| 297 |
+
Period(ordinal=2, value=1, freq="D")
|
| 298 |
+
|
| 299 |
+
msg = "If value is None, freq cannot be None"
|
| 300 |
+
with pytest.raises(ValueError, match=msg):
|
| 301 |
+
Period(month=1)
|
| 302 |
+
|
| 303 |
+
msg = '^Given date string "-2000" not likely a datetime$'
|
| 304 |
+
with pytest.raises(ValueError, match=msg):
|
| 305 |
+
Period("-2000", "A")
|
| 306 |
+
msg = "day is out of range for month"
|
| 307 |
+
with pytest.raises(DateParseError, match=msg):
|
| 308 |
+
Period("0", "A")
|
| 309 |
+
msg = "Unknown datetime string format, unable to parse"
|
| 310 |
+
with pytest.raises(DateParseError, match=msg):
|
| 311 |
+
Period("1/1/-2000", "A")
|
| 312 |
+
|
| 313 |
+
def test_constructor_corner(self):
|
| 314 |
+
expected = Period("2007-01", freq="2M")
|
| 315 |
+
assert Period(year=2007, month=1, freq="2M") == expected
|
| 316 |
+
|
| 317 |
+
assert Period(None) is NaT
|
| 318 |
+
|
| 319 |
+
p = Period("2007-01-01", freq="D")
|
| 320 |
+
|
| 321 |
+
result = Period(p, freq="A")
|
| 322 |
+
exp = Period("2007", freq="A")
|
| 323 |
+
assert result == exp
|
| 324 |
+
|
| 325 |
+
def test_constructor_infer_freq(self):
|
| 326 |
+
p = Period("2007-01-01")
|
| 327 |
+
assert p.freq == "D"
|
| 328 |
+
|
| 329 |
+
p = Period("2007-01-01 07")
|
| 330 |
+
assert p.freq == "H"
|
| 331 |
+
|
| 332 |
+
p = Period("2007-01-01 07:10")
|
| 333 |
+
assert p.freq == "T"
|
| 334 |
+
|
| 335 |
+
p = Period("2007-01-01 07:10:15")
|
| 336 |
+
assert p.freq == "S"
|
| 337 |
+
|
| 338 |
+
p = Period("2007-01-01 07:10:15.123")
|
| 339 |
+
assert p.freq == "L"
|
| 340 |
+
|
| 341 |
+
# We see that there are 6 digits after the decimal, so get microsecond
|
| 342 |
+
# even though they are all zeros.
|
| 343 |
+
p = Period("2007-01-01 07:10:15.123000")
|
| 344 |
+
assert p.freq == "U"
|
| 345 |
+
|
| 346 |
+
p = Period("2007-01-01 07:10:15.123400")
|
| 347 |
+
assert p.freq == "U"
|
| 348 |
+
|
| 349 |
+
def test_multiples(self):
|
| 350 |
+
result1 = Period("1989", freq="2A")
|
| 351 |
+
result2 = Period("1989", freq="A")
|
| 352 |
+
assert result1.ordinal == result2.ordinal
|
| 353 |
+
assert result1.freqstr == "2A-DEC"
|
| 354 |
+
assert result2.freqstr == "A-DEC"
|
| 355 |
+
assert result1.freq == offsets.YearEnd(2)
|
| 356 |
+
assert result2.freq == offsets.YearEnd()
|
| 357 |
+
|
| 358 |
+
assert (result1 + 1).ordinal == result1.ordinal + 2
|
| 359 |
+
assert (1 + result1).ordinal == result1.ordinal + 2
|
| 360 |
+
assert (result1 - 1).ordinal == result2.ordinal - 2
|
| 361 |
+
assert (-1 + result1).ordinal == result2.ordinal - 2
|
| 362 |
+
|
| 363 |
+
@pytest.mark.parametrize("month", MONTHS)
|
| 364 |
+
def test_period_cons_quarterly(self, month):
|
| 365 |
+
# bugs in scikits.timeseries
|
| 366 |
+
freq = f"Q-{month}"
|
| 367 |
+
exp = Period("1989Q3", freq=freq)
|
| 368 |
+
assert "1989Q3" in str(exp)
|
| 369 |
+
stamp = exp.to_timestamp("D", how="end")
|
| 370 |
+
p = Period(stamp, freq=freq)
|
| 371 |
+
assert p == exp
|
| 372 |
+
|
| 373 |
+
stamp = exp.to_timestamp("3D", how="end")
|
| 374 |
+
p = Period(stamp, freq=freq)
|
| 375 |
+
assert p == exp
|
| 376 |
+
|
| 377 |
+
@pytest.mark.parametrize("month", MONTHS)
|
| 378 |
+
def test_period_cons_annual(self, month):
|
| 379 |
+
# bugs in scikits.timeseries
|
| 380 |
+
freq = f"A-{month}"
|
| 381 |
+
exp = Period("1989", freq=freq)
|
| 382 |
+
stamp = exp.to_timestamp("D", how="end") + timedelta(days=30)
|
| 383 |
+
p = Period(stamp, freq=freq)
|
| 384 |
+
|
| 385 |
+
assert p == exp + 1
|
| 386 |
+
assert isinstance(p, Period)
|
| 387 |
+
|
| 388 |
+
@pytest.mark.parametrize("day", DAYS)
|
| 389 |
+
@pytest.mark.parametrize("num", range(10, 17))
|
| 390 |
+
def test_period_cons_weekly(self, num, day):
|
| 391 |
+
daystr = f"2011-02-{num}"
|
| 392 |
+
freq = f"W-{day}"
|
| 393 |
+
|
| 394 |
+
result = Period(daystr, freq=freq)
|
| 395 |
+
expected = Period(daystr, freq="D").asfreq(freq)
|
| 396 |
+
assert result == expected
|
| 397 |
+
assert isinstance(result, Period)
|
| 398 |
+
|
| 399 |
+
def test_parse_week_str_roundstrip(self):
|
| 400 |
+
# GH#50803
|
| 401 |
+
per = Period("2017-01-23/2017-01-29")
|
| 402 |
+
assert per.freq.freqstr == "W-SUN"
|
| 403 |
+
|
| 404 |
+
per = Period("2017-01-24/2017-01-30")
|
| 405 |
+
assert per.freq.freqstr == "W-MON"
|
| 406 |
+
|
| 407 |
+
msg = "Could not parse as weekly-freq Period"
|
| 408 |
+
with pytest.raises(ValueError, match=msg):
|
| 409 |
+
# not 6 days apart
|
| 410 |
+
Period("2016-01-23/2017-01-29")
|
| 411 |
+
|
| 412 |
+
def test_period_from_ordinal(self):
|
| 413 |
+
p = Period("2011-01", freq="M")
|
| 414 |
+
res = Period._from_ordinal(p.ordinal, freq="M")
|
| 415 |
+
assert p == res
|
| 416 |
+
assert isinstance(res, Period)
|
| 417 |
+
|
| 418 |
+
@pytest.mark.parametrize("freq", ["A", "M", "D", "H"])
|
| 419 |
+
def test_construct_from_nat_string_and_freq(self, freq):
|
| 420 |
+
per = Period("NaT", freq=freq)
|
| 421 |
+
assert per is NaT
|
| 422 |
+
|
| 423 |
+
per = Period("NaT", freq="2" + freq)
|
| 424 |
+
assert per is NaT
|
| 425 |
+
|
| 426 |
+
per = Period("NaT", freq="3" + freq)
|
| 427 |
+
assert per is NaT
|
| 428 |
+
|
| 429 |
+
def test_period_cons_nat(self):
|
| 430 |
+
p = Period("nat", freq="W-SUN")
|
| 431 |
+
assert p is NaT
|
| 432 |
+
|
| 433 |
+
p = Period(iNaT, freq="D")
|
| 434 |
+
assert p is NaT
|
| 435 |
+
|
| 436 |
+
p = Period(iNaT, freq="3D")
|
| 437 |
+
assert p is NaT
|
| 438 |
+
|
| 439 |
+
p = Period(iNaT, freq="1D1H")
|
| 440 |
+
assert p is NaT
|
| 441 |
+
|
| 442 |
+
p = Period("NaT")
|
| 443 |
+
assert p is NaT
|
| 444 |
+
|
| 445 |
+
p = Period(iNaT)
|
| 446 |
+
assert p is NaT
|
| 447 |
+
|
| 448 |
+
def test_period_cons_mult(self):
|
| 449 |
+
p1 = Period("2011-01", freq="3M")
|
| 450 |
+
p2 = Period("2011-01", freq="M")
|
| 451 |
+
assert p1.ordinal == p2.ordinal
|
| 452 |
+
|
| 453 |
+
assert p1.freq == offsets.MonthEnd(3)
|
| 454 |
+
assert p1.freqstr == "3M"
|
| 455 |
+
|
| 456 |
+
assert p2.freq == offsets.MonthEnd()
|
| 457 |
+
assert p2.freqstr == "M"
|
| 458 |
+
|
| 459 |
+
result = p1 + 1
|
| 460 |
+
assert result.ordinal == (p2 + 3).ordinal
|
| 461 |
+
|
| 462 |
+
assert result.freq == p1.freq
|
| 463 |
+
assert result.freqstr == "3M"
|
| 464 |
+
|
| 465 |
+
result = p1 - 1
|
| 466 |
+
assert result.ordinal == (p2 - 3).ordinal
|
| 467 |
+
assert result.freq == p1.freq
|
| 468 |
+
assert result.freqstr == "3M"
|
| 469 |
+
|
| 470 |
+
msg = "Frequency must be positive, because it represents span: -3M"
|
| 471 |
+
with pytest.raises(ValueError, match=msg):
|
| 472 |
+
Period("2011-01", freq="-3M")
|
| 473 |
+
|
| 474 |
+
msg = "Frequency must be positive, because it represents span: 0M"
|
| 475 |
+
with pytest.raises(ValueError, match=msg):
|
| 476 |
+
Period("2011-01", freq="0M")
|
| 477 |
+
|
| 478 |
+
def test_period_cons_combined(self):
|
| 479 |
+
p = [
|
| 480 |
+
(
|
| 481 |
+
Period("2011-01", freq="1D1H"),
|
| 482 |
+
Period("2011-01", freq="1H1D"),
|
| 483 |
+
Period("2011-01", freq="H"),
|
| 484 |
+
),
|
| 485 |
+
(
|
| 486 |
+
Period(ordinal=1, freq="1D1H"),
|
| 487 |
+
Period(ordinal=1, freq="1H1D"),
|
| 488 |
+
Period(ordinal=1, freq="H"),
|
| 489 |
+
),
|
| 490 |
+
]
|
| 491 |
+
|
| 492 |
+
for p1, p2, p3 in p:
|
| 493 |
+
assert p1.ordinal == p3.ordinal
|
| 494 |
+
assert p2.ordinal == p3.ordinal
|
| 495 |
+
|
| 496 |
+
assert p1.freq == offsets.Hour(25)
|
| 497 |
+
assert p1.freqstr == "25H"
|
| 498 |
+
|
| 499 |
+
assert p2.freq == offsets.Hour(25)
|
| 500 |
+
assert p2.freqstr == "25H"
|
| 501 |
+
|
| 502 |
+
assert p3.freq == offsets.Hour()
|
| 503 |
+
assert p3.freqstr == "H"
|
| 504 |
+
|
| 505 |
+
result = p1 + 1
|
| 506 |
+
assert result.ordinal == (p3 + 25).ordinal
|
| 507 |
+
assert result.freq == p1.freq
|
| 508 |
+
assert result.freqstr == "25H"
|
| 509 |
+
|
| 510 |
+
result = p2 + 1
|
| 511 |
+
assert result.ordinal == (p3 + 25).ordinal
|
| 512 |
+
assert result.freq == p2.freq
|
| 513 |
+
assert result.freqstr == "25H"
|
| 514 |
+
|
| 515 |
+
result = p1 - 1
|
| 516 |
+
assert result.ordinal == (p3 - 25).ordinal
|
| 517 |
+
assert result.freq == p1.freq
|
| 518 |
+
assert result.freqstr == "25H"
|
| 519 |
+
|
| 520 |
+
result = p2 - 1
|
| 521 |
+
assert result.ordinal == (p3 - 25).ordinal
|
| 522 |
+
assert result.freq == p2.freq
|
| 523 |
+
assert result.freqstr == "25H"
|
| 524 |
+
|
| 525 |
+
msg = "Frequency must be positive, because it represents span: -25H"
|
| 526 |
+
with pytest.raises(ValueError, match=msg):
|
| 527 |
+
Period("2011-01", freq="-1D1H")
|
| 528 |
+
with pytest.raises(ValueError, match=msg):
|
| 529 |
+
Period("2011-01", freq="-1H1D")
|
| 530 |
+
with pytest.raises(ValueError, match=msg):
|
| 531 |
+
Period(ordinal=1, freq="-1D1H")
|
| 532 |
+
with pytest.raises(ValueError, match=msg):
|
| 533 |
+
Period(ordinal=1, freq="-1H1D")
|
| 534 |
+
|
| 535 |
+
msg = "Frequency must be positive, because it represents span: 0D"
|
| 536 |
+
with pytest.raises(ValueError, match=msg):
|
| 537 |
+
Period("2011-01", freq="0D0H")
|
| 538 |
+
with pytest.raises(ValueError, match=msg):
|
| 539 |
+
Period(ordinal=1, freq="0D0H")
|
| 540 |
+
|
| 541 |
+
# You can only combine together day and intraday offsets
|
| 542 |
+
msg = "Invalid frequency: 1W1D"
|
| 543 |
+
with pytest.raises(ValueError, match=msg):
|
| 544 |
+
Period("2011-01", freq="1W1D")
|
| 545 |
+
msg = "Invalid frequency: 1D1W"
|
| 546 |
+
with pytest.raises(ValueError, match=msg):
|
| 547 |
+
Period("2011-01", freq="1D1W")
|
| 548 |
+
|
| 549 |
+
@pytest.mark.parametrize("day", ["1970/01/01 ", "2020-12-31 ", "1981/09/13 "])
|
| 550 |
+
@pytest.mark.parametrize("hour", ["00:00:00", "00:00:01", "23:59:59", "12:00:59"])
|
| 551 |
+
@pytest.mark.parametrize(
|
| 552 |
+
"sec_float, expected",
|
| 553 |
+
[
|
| 554 |
+
(".000000001", 1),
|
| 555 |
+
(".000000999", 999),
|
| 556 |
+
(".123456789", 789),
|
| 557 |
+
(".999999999", 999),
|
| 558 |
+
(".999999000", 0),
|
| 559 |
+
# Test femtoseconds, attoseconds, picoseconds are dropped like Timestamp
|
| 560 |
+
(".999999001123", 1),
|
| 561 |
+
(".999999001123456", 1),
|
| 562 |
+
(".999999001123456789", 1),
|
| 563 |
+
],
|
| 564 |
+
)
|
| 565 |
+
def test_period_constructor_nanosecond(self, day, hour, sec_float, expected):
|
| 566 |
+
# GH 34621
|
| 567 |
+
|
| 568 |
+
assert Period(day + hour + sec_float).start_time.nanosecond == expected
|
| 569 |
+
|
| 570 |
+
@pytest.mark.parametrize("hour", range(24))
|
| 571 |
+
def test_period_large_ordinal(self, hour):
|
| 572 |
+
# Issue #36430
|
| 573 |
+
# Integer overflow for Period over the maximum timestamp
|
| 574 |
+
p = Period(ordinal=2562048 + hour, freq="1H")
|
| 575 |
+
assert p.hour == hour
|
| 576 |
+
|
| 577 |
+
|
| 578 |
+
class TestPeriodMethods:
|
| 579 |
+
def test_round_trip(self):
|
| 580 |
+
p = Period("2000Q1")
|
| 581 |
+
new_p = tm.round_trip_pickle(p)
|
| 582 |
+
assert new_p == p
|
| 583 |
+
|
| 584 |
+
def test_hash(self):
|
| 585 |
+
assert hash(Period("2011-01", freq="M")) == hash(Period("2011-01", freq="M"))
|
| 586 |
+
|
| 587 |
+
assert hash(Period("2011-01-01", freq="D")) != hash(Period("2011-01", freq="M"))
|
| 588 |
+
|
| 589 |
+
assert hash(Period("2011-01", freq="3M")) != hash(Period("2011-01", freq="2M"))
|
| 590 |
+
|
| 591 |
+
assert hash(Period("2011-01", freq="M")) != hash(Period("2011-02", freq="M"))
|
| 592 |
+
|
| 593 |
+
# --------------------------------------------------------------
|
| 594 |
+
# to_timestamp
|
| 595 |
+
|
| 596 |
+
def test_to_timestamp_mult(self):
|
| 597 |
+
p = Period("2011-01", freq="M")
|
| 598 |
+
assert p.to_timestamp(how="S") == Timestamp("2011-01-01")
|
| 599 |
+
expected = Timestamp("2011-02-01") - Timedelta(1, "ns")
|
| 600 |
+
assert p.to_timestamp(how="E") == expected
|
| 601 |
+
|
| 602 |
+
p = Period("2011-01", freq="3M")
|
| 603 |
+
assert p.to_timestamp(how="S") == Timestamp("2011-01-01")
|
| 604 |
+
expected = Timestamp("2011-04-01") - Timedelta(1, "ns")
|
| 605 |
+
assert p.to_timestamp(how="E") == expected
|
| 606 |
+
|
| 607 |
+
def test_to_timestamp(self):
|
| 608 |
+
p = Period("1982", freq="A")
|
| 609 |
+
start_ts = p.to_timestamp(how="S")
|
| 610 |
+
aliases = ["s", "StarT", "BEGIn"]
|
| 611 |
+
for a in aliases:
|
| 612 |
+
assert start_ts == p.to_timestamp("D", how=a)
|
| 613 |
+
# freq with mult should not affect to the result
|
| 614 |
+
assert start_ts == p.to_timestamp("3D", how=a)
|
| 615 |
+
|
| 616 |
+
end_ts = p.to_timestamp(how="E")
|
| 617 |
+
aliases = ["e", "end", "FINIsH"]
|
| 618 |
+
for a in aliases:
|
| 619 |
+
assert end_ts == p.to_timestamp("D", how=a)
|
| 620 |
+
assert end_ts == p.to_timestamp("3D", how=a)
|
| 621 |
+
|
| 622 |
+
from_lst = ["A", "Q", "M", "W", "B", "D", "H", "Min", "S"]
|
| 623 |
+
|
| 624 |
+
def _ex(p):
|
| 625 |
+
if p.freq == "B":
|
| 626 |
+
return p.start_time + Timedelta(days=1, nanoseconds=-1)
|
| 627 |
+
return Timestamp((p + p.freq).start_time._value - 1)
|
| 628 |
+
|
| 629 |
+
for fcode in from_lst:
|
| 630 |
+
p = Period("1982", freq=fcode)
|
| 631 |
+
result = p.to_timestamp().to_period(fcode)
|
| 632 |
+
assert result == p
|
| 633 |
+
|
| 634 |
+
assert p.start_time == p.to_timestamp(how="S")
|
| 635 |
+
|
| 636 |
+
assert p.end_time == _ex(p)
|
| 637 |
+
|
| 638 |
+
# Frequency other than daily
|
| 639 |
+
|
| 640 |
+
p = Period("1985", freq="A")
|
| 641 |
+
|
| 642 |
+
result = p.to_timestamp("H", how="end")
|
| 643 |
+
expected = Timestamp(1986, 1, 1) - Timedelta(1, "ns")
|
| 644 |
+
assert result == expected
|
| 645 |
+
result = p.to_timestamp("3H", how="end")
|
| 646 |
+
assert result == expected
|
| 647 |
+
|
| 648 |
+
result = p.to_timestamp("T", how="end")
|
| 649 |
+
expected = Timestamp(1986, 1, 1) - Timedelta(1, "ns")
|
| 650 |
+
assert result == expected
|
| 651 |
+
result = p.to_timestamp("2T", how="end")
|
| 652 |
+
assert result == expected
|
| 653 |
+
|
| 654 |
+
result = p.to_timestamp(how="end")
|
| 655 |
+
expected = Timestamp(1986, 1, 1) - Timedelta(1, "ns")
|
| 656 |
+
assert result == expected
|
| 657 |
+
|
| 658 |
+
expected = datetime(1985, 1, 1)
|
| 659 |
+
result = p.to_timestamp("H", how="start")
|
| 660 |
+
assert result == expected
|
| 661 |
+
result = p.to_timestamp("T", how="start")
|
| 662 |
+
assert result == expected
|
| 663 |
+
result = p.to_timestamp("S", how="start")
|
| 664 |
+
assert result == expected
|
| 665 |
+
result = p.to_timestamp("3H", how="start")
|
| 666 |
+
assert result == expected
|
| 667 |
+
result = p.to_timestamp("5S", how="start")
|
| 668 |
+
assert result == expected
|
| 669 |
+
|
| 670 |
+
def test_to_timestamp_business_end(self):
|
| 671 |
+
per = Period("1990-01-05", "B") # Friday
|
| 672 |
+
result = per.to_timestamp("B", how="E")
|
| 673 |
+
|
| 674 |
+
expected = Timestamp("1990-01-06") - Timedelta(nanoseconds=1)
|
| 675 |
+
assert result == expected
|
| 676 |
+
|
| 677 |
+
@pytest.mark.parametrize(
|
| 678 |
+
"ts, expected",
|
| 679 |
+
[
|
| 680 |
+
("1970-01-01 00:00:00", 0),
|
| 681 |
+
("1970-01-01 00:00:00.000001", 1),
|
| 682 |
+
("1970-01-01 00:00:00.00001", 10),
|
| 683 |
+
("1970-01-01 00:00:00.499", 499000),
|
| 684 |
+
("1999-12-31 23:59:59.999", 999000),
|
| 685 |
+
("1999-12-31 23:59:59.999999", 999999),
|
| 686 |
+
("2050-12-31 23:59:59.5", 500000),
|
| 687 |
+
("2050-12-31 23:59:59.500001", 500001),
|
| 688 |
+
("2050-12-31 23:59:59.123456", 123456),
|
| 689 |
+
],
|
| 690 |
+
)
|
| 691 |
+
@pytest.mark.parametrize("freq", [None, "us", "ns"])
|
| 692 |
+
def test_to_timestamp_microsecond(self, ts, expected, freq):
|
| 693 |
+
# GH 24444
|
| 694 |
+
result = Period(ts).to_timestamp(freq=freq).microsecond
|
| 695 |
+
assert result == expected
|
| 696 |
+
|
| 697 |
+
# --------------------------------------------------------------
|
| 698 |
+
# Rendering: __repr__, strftime, etc
|
| 699 |
+
|
| 700 |
+
def test_repr(self):
|
| 701 |
+
p = Period("Jan-2000")
|
| 702 |
+
assert "2000-01" in repr(p)
|
| 703 |
+
|
| 704 |
+
p = Period("2000-12-15")
|
| 705 |
+
assert "2000-12-15" in repr(p)
|
| 706 |
+
|
| 707 |
+
def test_repr_nat(self):
|
| 708 |
+
p = Period("nat", freq="M")
|
| 709 |
+
assert repr(NaT) in repr(p)
|
| 710 |
+
|
| 711 |
+
def test_millisecond_repr(self):
|
| 712 |
+
p = Period("2000-01-01 12:15:02.123")
|
| 713 |
+
|
| 714 |
+
assert repr(p) == "Period('2000-01-01 12:15:02.123', 'L')"
|
| 715 |
+
|
| 716 |
+
def test_microsecond_repr(self):
|
| 717 |
+
p = Period("2000-01-01 12:15:02.123567")
|
| 718 |
+
|
| 719 |
+
assert repr(p) == "Period('2000-01-01 12:15:02.123567', 'U')"
|
| 720 |
+
|
| 721 |
+
def test_strftime(self):
|
| 722 |
+
# GH#3363
|
| 723 |
+
p = Period("2000-1-1 12:34:12", freq="S")
|
| 724 |
+
res = p.strftime("%Y-%m-%d %H:%M:%S")
|
| 725 |
+
assert res == "2000-01-01 12:34:12"
|
| 726 |
+
assert isinstance(res, str)
|
| 727 |
+
|
| 728 |
+
|
| 729 |
+
class TestPeriodProperties:
|
| 730 |
+
"""Test properties such as year, month, weekday, etc...."""
|
| 731 |
+
|
| 732 |
+
@pytest.mark.parametrize("freq", ["A", "M", "D", "H"])
|
| 733 |
+
def test_is_leap_year(self, freq):
|
| 734 |
+
# GH 13727
|
| 735 |
+
p = Period("2000-01-01 00:00:00", freq=freq)
|
| 736 |
+
assert p.is_leap_year
|
| 737 |
+
assert isinstance(p.is_leap_year, bool)
|
| 738 |
+
|
| 739 |
+
p = Period("1999-01-01 00:00:00", freq=freq)
|
| 740 |
+
assert not p.is_leap_year
|
| 741 |
+
|
| 742 |
+
p = Period("2004-01-01 00:00:00", freq=freq)
|
| 743 |
+
assert p.is_leap_year
|
| 744 |
+
|
| 745 |
+
p = Period("2100-01-01 00:00:00", freq=freq)
|
| 746 |
+
assert not p.is_leap_year
|
| 747 |
+
|
| 748 |
+
def test_quarterly_negative_ordinals(self):
|
| 749 |
+
p = Period(ordinal=-1, freq="Q-DEC")
|
| 750 |
+
assert p.year == 1969
|
| 751 |
+
assert p.quarter == 4
|
| 752 |
+
assert isinstance(p, Period)
|
| 753 |
+
|
| 754 |
+
p = Period(ordinal=-2, freq="Q-DEC")
|
| 755 |
+
assert p.year == 1969
|
| 756 |
+
assert p.quarter == 3
|
| 757 |
+
assert isinstance(p, Period)
|
| 758 |
+
|
| 759 |
+
p = Period(ordinal=-2, freq="M")
|
| 760 |
+
assert p.year == 1969
|
| 761 |
+
assert p.month == 11
|
| 762 |
+
assert isinstance(p, Period)
|
| 763 |
+
|
| 764 |
+
def test_freq_str(self):
|
| 765 |
+
i1 = Period("1982", freq="Min")
|
| 766 |
+
assert i1.freq == offsets.Minute()
|
| 767 |
+
assert i1.freqstr == "T"
|
| 768 |
+
|
| 769 |
+
def test_period_deprecated_freq(self):
|
| 770 |
+
cases = {
|
| 771 |
+
"M": ["MTH", "MONTH", "MONTHLY", "Mth", "month", "monthly"],
|
| 772 |
+
"B": ["BUS", "BUSINESS", "BUSINESSLY", "WEEKDAY", "bus"],
|
| 773 |
+
"D": ["DAY", "DLY", "DAILY", "Day", "Dly", "Daily"],
|
| 774 |
+
"H": ["HR", "HOUR", "HRLY", "HOURLY", "hr", "Hour", "HRly"],
|
| 775 |
+
"T": ["minute", "MINUTE", "MINUTELY", "minutely"],
|
| 776 |
+
"S": ["sec", "SEC", "SECOND", "SECONDLY", "second"],
|
| 777 |
+
"L": ["MILLISECOND", "MILLISECONDLY", "millisecond"],
|
| 778 |
+
"U": ["MICROSECOND", "MICROSECONDLY", "microsecond"],
|
| 779 |
+
"N": ["NANOSECOND", "NANOSECONDLY", "nanosecond"],
|
| 780 |
+
}
|
| 781 |
+
|
| 782 |
+
msg = INVALID_FREQ_ERR_MSG
|
| 783 |
+
for exp, freqs in cases.items():
|
| 784 |
+
for freq in freqs:
|
| 785 |
+
with pytest.raises(ValueError, match=msg):
|
| 786 |
+
Period("2016-03-01 09:00", freq=freq)
|
| 787 |
+
with pytest.raises(ValueError, match=msg):
|
| 788 |
+
Period(ordinal=1, freq=freq)
|
| 789 |
+
|
| 790 |
+
# check supported freq-aliases still works
|
| 791 |
+
p1 = Period("2016-03-01 09:00", freq=exp)
|
| 792 |
+
p2 = Period(ordinal=1, freq=exp)
|
| 793 |
+
assert isinstance(p1, Period)
|
| 794 |
+
assert isinstance(p2, Period)
|
| 795 |
+
|
| 796 |
+
@staticmethod
|
| 797 |
+
def _period_constructor(bound, offset):
|
| 798 |
+
return Period(
|
| 799 |
+
year=bound.year,
|
| 800 |
+
month=bound.month,
|
| 801 |
+
day=bound.day,
|
| 802 |
+
hour=bound.hour,
|
| 803 |
+
minute=bound.minute,
|
| 804 |
+
second=bound.second + offset,
|
| 805 |
+
freq="us",
|
| 806 |
+
)
|
| 807 |
+
|
| 808 |
+
@pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)])
|
| 809 |
+
@pytest.mark.parametrize("period_property", ["start_time", "end_time"])
|
| 810 |
+
def test_outer_bounds_start_and_end_time(self, bound, offset, period_property):
|
| 811 |
+
# GH #13346
|
| 812 |
+
period = TestPeriodProperties._period_constructor(bound, offset)
|
| 813 |
+
with pytest.raises(OutOfBoundsDatetime, match="Out of bounds nanosecond"):
|
| 814 |
+
getattr(period, period_property)
|
| 815 |
+
|
| 816 |
+
@pytest.mark.parametrize("bound, offset", [(Timestamp.min, -1), (Timestamp.max, 1)])
|
| 817 |
+
@pytest.mark.parametrize("period_property", ["start_time", "end_time"])
|
| 818 |
+
def test_inner_bounds_start_and_end_time(self, bound, offset, period_property):
|
| 819 |
+
# GH #13346
|
| 820 |
+
period = TestPeriodProperties._period_constructor(bound, -offset)
|
| 821 |
+
expected = period.to_timestamp().round(freq="S")
|
| 822 |
+
assert getattr(period, period_property).round(freq="S") == expected
|
| 823 |
+
expected = (bound - offset * Timedelta(1, unit="S")).floor("S")
|
| 824 |
+
assert getattr(period, period_property).floor("S") == expected
|
| 825 |
+
|
| 826 |
+
def test_start_time(self):
|
| 827 |
+
freq_lst = ["A", "Q", "M", "D", "H", "T", "S"]
|
| 828 |
+
xp = datetime(2012, 1, 1)
|
| 829 |
+
for f in freq_lst:
|
| 830 |
+
p = Period("2012", freq=f)
|
| 831 |
+
assert p.start_time == xp
|
| 832 |
+
assert Period("2012", freq="B").start_time == datetime(2012, 1, 2)
|
| 833 |
+
assert Period("2012", freq="W").start_time == datetime(2011, 12, 26)
|
| 834 |
+
|
| 835 |
+
def test_end_time(self):
|
| 836 |
+
p = Period("2012", freq="A")
|
| 837 |
+
|
| 838 |
+
def _ex(*args):
|
| 839 |
+
return Timestamp(Timestamp(datetime(*args)).as_unit("ns")._value - 1)
|
| 840 |
+
|
| 841 |
+
xp = _ex(2013, 1, 1)
|
| 842 |
+
assert xp == p.end_time
|
| 843 |
+
|
| 844 |
+
p = Period("2012", freq="Q")
|
| 845 |
+
xp = _ex(2012, 4, 1)
|
| 846 |
+
assert xp == p.end_time
|
| 847 |
+
|
| 848 |
+
p = Period("2012", freq="M")
|
| 849 |
+
xp = _ex(2012, 2, 1)
|
| 850 |
+
assert xp == p.end_time
|
| 851 |
+
|
| 852 |
+
p = Period("2012", freq="D")
|
| 853 |
+
xp = _ex(2012, 1, 2)
|
| 854 |
+
assert xp == p.end_time
|
| 855 |
+
|
| 856 |
+
p = Period("2012", freq="H")
|
| 857 |
+
xp = _ex(2012, 1, 1, 1)
|
| 858 |
+
assert xp == p.end_time
|
| 859 |
+
|
| 860 |
+
p = Period("2012", freq="B")
|
| 861 |
+
xp = _ex(2012, 1, 3)
|
| 862 |
+
assert xp == p.end_time
|
| 863 |
+
|
| 864 |
+
p = Period("2012", freq="W")
|
| 865 |
+
xp = _ex(2012, 1, 2)
|
| 866 |
+
assert xp == p.end_time
|
| 867 |
+
|
| 868 |
+
# Test for GH 11738
|
| 869 |
+
p = Period("2012", freq="15D")
|
| 870 |
+
xp = _ex(2012, 1, 16)
|
| 871 |
+
assert xp == p.end_time
|
| 872 |
+
|
| 873 |
+
p = Period("2012", freq="1D1H")
|
| 874 |
+
xp = _ex(2012, 1, 2, 1)
|
| 875 |
+
assert xp == p.end_time
|
| 876 |
+
|
| 877 |
+
p = Period("2012", freq="1H1D")
|
| 878 |
+
xp = _ex(2012, 1, 2, 1)
|
| 879 |
+
assert xp == p.end_time
|
| 880 |
+
|
| 881 |
+
def test_end_time_business_friday(self):
|
| 882 |
+
# GH#34449
|
| 883 |
+
per = Period("1990-01-05", "B")
|
| 884 |
+
result = per.end_time
|
| 885 |
+
|
| 886 |
+
expected = Timestamp("1990-01-06") - Timedelta(nanoseconds=1)
|
| 887 |
+
assert result == expected
|
| 888 |
+
|
| 889 |
+
def test_anchor_week_end_time(self):
|
| 890 |
+
def _ex(*args):
|
| 891 |
+
return Timestamp(Timestamp(datetime(*args)).as_unit("ns")._value - 1)
|
| 892 |
+
|
| 893 |
+
p = Period("2013-1-1", "W-SAT")
|
| 894 |
+
xp = _ex(2013, 1, 6)
|
| 895 |
+
assert p.end_time == xp
|
| 896 |
+
|
| 897 |
+
def test_properties_annually(self):
|
| 898 |
+
# Test properties on Periods with annually frequency.
|
| 899 |
+
a_date = Period(freq="A", year=2007)
|
| 900 |
+
assert a_date.year == 2007
|
| 901 |
+
|
| 902 |
+
def test_properties_quarterly(self):
|
| 903 |
+
# Test properties on Periods with daily frequency.
|
| 904 |
+
qedec_date = Period(freq="Q-DEC", year=2007, quarter=1)
|
| 905 |
+
qejan_date = Period(freq="Q-JAN", year=2007, quarter=1)
|
| 906 |
+
qejun_date = Period(freq="Q-JUN", year=2007, quarter=1)
|
| 907 |
+
#
|
| 908 |
+
for x in range(3):
|
| 909 |
+
for qd in (qedec_date, qejan_date, qejun_date):
|
| 910 |
+
assert (qd + x).qyear == 2007
|
| 911 |
+
assert (qd + x).quarter == x + 1
|
| 912 |
+
|
| 913 |
+
def test_properties_monthly(self):
|
| 914 |
+
# Test properties on Periods with daily frequency.
|
| 915 |
+
m_date = Period(freq="M", year=2007, month=1)
|
| 916 |
+
for x in range(11):
|
| 917 |
+
m_ival_x = m_date + x
|
| 918 |
+
assert m_ival_x.year == 2007
|
| 919 |
+
if 1 <= x + 1 <= 3:
|
| 920 |
+
assert m_ival_x.quarter == 1
|
| 921 |
+
elif 4 <= x + 1 <= 6:
|
| 922 |
+
assert m_ival_x.quarter == 2
|
| 923 |
+
elif 7 <= x + 1 <= 9:
|
| 924 |
+
assert m_ival_x.quarter == 3
|
| 925 |
+
elif 10 <= x + 1 <= 12:
|
| 926 |
+
assert m_ival_x.quarter == 4
|
| 927 |
+
assert m_ival_x.month == x + 1
|
| 928 |
+
|
| 929 |
+
def test_properties_weekly(self):
|
| 930 |
+
# Test properties on Periods with daily frequency.
|
| 931 |
+
w_date = Period(freq="W", year=2007, month=1, day=7)
|
| 932 |
+
#
|
| 933 |
+
assert w_date.year == 2007
|
| 934 |
+
assert w_date.quarter == 1
|
| 935 |
+
assert w_date.month == 1
|
| 936 |
+
assert w_date.week == 1
|
| 937 |
+
assert (w_date - 1).week == 52
|
| 938 |
+
assert w_date.days_in_month == 31
|
| 939 |
+
assert Period(freq="W", year=2012, month=2, day=1).days_in_month == 29
|
| 940 |
+
|
| 941 |
+
def test_properties_weekly_legacy(self):
|
| 942 |
+
# Test properties on Periods with daily frequency.
|
| 943 |
+
w_date = Period(freq="W", year=2007, month=1, day=7)
|
| 944 |
+
assert w_date.year == 2007
|
| 945 |
+
assert w_date.quarter == 1
|
| 946 |
+
assert w_date.month == 1
|
| 947 |
+
assert w_date.week == 1
|
| 948 |
+
assert (w_date - 1).week == 52
|
| 949 |
+
assert w_date.days_in_month == 31
|
| 950 |
+
|
| 951 |
+
exp = Period(freq="W", year=2012, month=2, day=1)
|
| 952 |
+
assert exp.days_in_month == 29
|
| 953 |
+
|
| 954 |
+
msg = INVALID_FREQ_ERR_MSG
|
| 955 |
+
with pytest.raises(ValueError, match=msg):
|
| 956 |
+
Period(freq="WK", year=2007, month=1, day=7)
|
| 957 |
+
|
| 958 |
+
def test_properties_daily(self):
|
| 959 |
+
# Test properties on Periods with daily frequency.
|
| 960 |
+
b_date = Period(freq="B", year=2007, month=1, day=1)
|
| 961 |
+
#
|
| 962 |
+
assert b_date.year == 2007
|
| 963 |
+
assert b_date.quarter == 1
|
| 964 |
+
assert b_date.month == 1
|
| 965 |
+
assert b_date.day == 1
|
| 966 |
+
assert b_date.weekday == 0
|
| 967 |
+
assert b_date.dayofyear == 1
|
| 968 |
+
assert b_date.days_in_month == 31
|
| 969 |
+
assert Period(freq="B", year=2012, month=2, day=1).days_in_month == 29
|
| 970 |
+
|
| 971 |
+
d_date = Period(freq="D", year=2007, month=1, day=1)
|
| 972 |
+
|
| 973 |
+
assert d_date.year == 2007
|
| 974 |
+
assert d_date.quarter == 1
|
| 975 |
+
assert d_date.month == 1
|
| 976 |
+
assert d_date.day == 1
|
| 977 |
+
assert d_date.weekday == 0
|
| 978 |
+
assert d_date.dayofyear == 1
|
| 979 |
+
assert d_date.days_in_month == 31
|
| 980 |
+
assert Period(freq="D", year=2012, month=2, day=1).days_in_month == 29
|
| 981 |
+
|
| 982 |
+
def test_properties_hourly(self):
|
| 983 |
+
# Test properties on Periods with hourly frequency.
|
| 984 |
+
h_date1 = Period(freq="H", year=2007, month=1, day=1, hour=0)
|
| 985 |
+
h_date2 = Period(freq="2H", year=2007, month=1, day=1, hour=0)
|
| 986 |
+
|
| 987 |
+
for h_date in [h_date1, h_date2]:
|
| 988 |
+
assert h_date.year == 2007
|
| 989 |
+
assert h_date.quarter == 1
|
| 990 |
+
assert h_date.month == 1
|
| 991 |
+
assert h_date.day == 1
|
| 992 |
+
assert h_date.weekday == 0
|
| 993 |
+
assert h_date.dayofyear == 1
|
| 994 |
+
assert h_date.hour == 0
|
| 995 |
+
assert h_date.days_in_month == 31
|
| 996 |
+
assert (
|
| 997 |
+
Period(freq="H", year=2012, month=2, day=1, hour=0).days_in_month == 29
|
| 998 |
+
)
|
| 999 |
+
|
| 1000 |
+
def test_properties_minutely(self):
|
| 1001 |
+
# Test properties on Periods with minutely frequency.
|
| 1002 |
+
t_date = Period(freq="Min", year=2007, month=1, day=1, hour=0, minute=0)
|
| 1003 |
+
#
|
| 1004 |
+
assert t_date.quarter == 1
|
| 1005 |
+
assert t_date.month == 1
|
| 1006 |
+
assert t_date.day == 1
|
| 1007 |
+
assert t_date.weekday == 0
|
| 1008 |
+
assert t_date.dayofyear == 1
|
| 1009 |
+
assert t_date.hour == 0
|
| 1010 |
+
assert t_date.minute == 0
|
| 1011 |
+
assert t_date.days_in_month == 31
|
| 1012 |
+
assert (
|
| 1013 |
+
Period(freq="D", year=2012, month=2, day=1, hour=0, minute=0).days_in_month
|
| 1014 |
+
== 29
|
| 1015 |
+
)
|
| 1016 |
+
|
| 1017 |
+
def test_properties_secondly(self):
|
| 1018 |
+
# Test properties on Periods with secondly frequency.
|
| 1019 |
+
s_date = Period(
|
| 1020 |
+
freq="Min", year=2007, month=1, day=1, hour=0, minute=0, second=0
|
| 1021 |
+
)
|
| 1022 |
+
#
|
| 1023 |
+
assert s_date.year == 2007
|
| 1024 |
+
assert s_date.quarter == 1
|
| 1025 |
+
assert s_date.month == 1
|
| 1026 |
+
assert s_date.day == 1
|
| 1027 |
+
assert s_date.weekday == 0
|
| 1028 |
+
assert s_date.dayofyear == 1
|
| 1029 |
+
assert s_date.hour == 0
|
| 1030 |
+
assert s_date.minute == 0
|
| 1031 |
+
assert s_date.second == 0
|
| 1032 |
+
assert s_date.days_in_month == 31
|
| 1033 |
+
assert (
|
| 1034 |
+
Period(
|
| 1035 |
+
freq="Min", year=2012, month=2, day=1, hour=0, minute=0, second=0
|
| 1036 |
+
).days_in_month
|
| 1037 |
+
== 29
|
| 1038 |
+
)
|
| 1039 |
+
|
| 1040 |
+
|
| 1041 |
+
class TestPeriodField:
|
| 1042 |
+
def test_get_period_field_array_raises_on_out_of_range(self):
|
| 1043 |
+
msg = "Buffer dtype mismatch, expected 'const int64_t' but got 'double'"
|
| 1044 |
+
with pytest.raises(ValueError, match=msg):
|
| 1045 |
+
libperiod.get_period_field_arr(-1, np.empty(1), 0)
|
| 1046 |
+
|
| 1047 |
+
|
| 1048 |
+
class TestPeriodComparisons:
|
| 1049 |
+
def test_comparison_same_period_different_object(self):
|
| 1050 |
+
# Separate Period objects for the same period
|
| 1051 |
+
left = Period("2000-01", "M")
|
| 1052 |
+
right = Period("2000-01", "M")
|
| 1053 |
+
|
| 1054 |
+
assert left == right
|
| 1055 |
+
assert left >= right
|
| 1056 |
+
assert left <= right
|
| 1057 |
+
assert not left < right
|
| 1058 |
+
assert not left > right
|
| 1059 |
+
|
| 1060 |
+
def test_comparison_same_freq(self):
|
| 1061 |
+
jan = Period("2000-01", "M")
|
| 1062 |
+
feb = Period("2000-02", "M")
|
| 1063 |
+
|
| 1064 |
+
assert not jan == feb
|
| 1065 |
+
assert jan != feb
|
| 1066 |
+
assert jan < feb
|
| 1067 |
+
assert jan <= feb
|
| 1068 |
+
assert not jan > feb
|
| 1069 |
+
assert not jan >= feb
|
| 1070 |
+
|
| 1071 |
+
def test_comparison_mismatched_freq(self):
|
| 1072 |
+
jan = Period("2000-01", "M")
|
| 1073 |
+
day = Period("2012-01-01", "D")
|
| 1074 |
+
|
| 1075 |
+
assert not jan == day
|
| 1076 |
+
assert jan != day
|
| 1077 |
+
msg = r"Input has different freq=D from Period\(freq=M\)"
|
| 1078 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1079 |
+
jan < day
|
| 1080 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1081 |
+
jan <= day
|
| 1082 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1083 |
+
jan > day
|
| 1084 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1085 |
+
jan >= day
|
| 1086 |
+
|
| 1087 |
+
def test_comparison_invalid_type(self):
|
| 1088 |
+
jan = Period("2000-01", "M")
|
| 1089 |
+
|
| 1090 |
+
assert not jan == 1
|
| 1091 |
+
assert jan != 1
|
| 1092 |
+
|
| 1093 |
+
int_or_per = "'(Period|int)'"
|
| 1094 |
+
msg = f"not supported between instances of {int_or_per} and {int_or_per}"
|
| 1095 |
+
for left, right in [(jan, 1), (1, jan)]:
|
| 1096 |
+
with pytest.raises(TypeError, match=msg):
|
| 1097 |
+
left > right
|
| 1098 |
+
with pytest.raises(TypeError, match=msg):
|
| 1099 |
+
left >= right
|
| 1100 |
+
with pytest.raises(TypeError, match=msg):
|
| 1101 |
+
left < right
|
| 1102 |
+
with pytest.raises(TypeError, match=msg):
|
| 1103 |
+
left <= right
|
| 1104 |
+
|
| 1105 |
+
def test_sort_periods(self):
|
| 1106 |
+
jan = Period("2000-01", "M")
|
| 1107 |
+
feb = Period("2000-02", "M")
|
| 1108 |
+
mar = Period("2000-03", "M")
|
| 1109 |
+
periods = [mar, jan, feb]
|
| 1110 |
+
correctPeriods = [jan, feb, mar]
|
| 1111 |
+
assert sorted(periods) == correctPeriods
|
| 1112 |
+
|
| 1113 |
+
def test_period_cmp_nat(self):
|
| 1114 |
+
p = Period("2011-01-01", freq="D")
|
| 1115 |
+
|
| 1116 |
+
t = Timestamp("2011-01-01")
|
| 1117 |
+
# confirm Period('NaT') work identical with Timestamp('NaT')
|
| 1118 |
+
for left, right in [
|
| 1119 |
+
(NaT, p),
|
| 1120 |
+
(p, NaT),
|
| 1121 |
+
(NaT, t),
|
| 1122 |
+
(t, NaT),
|
| 1123 |
+
]:
|
| 1124 |
+
assert not left < right
|
| 1125 |
+
assert not left > right
|
| 1126 |
+
assert not left == right
|
| 1127 |
+
assert left != right
|
| 1128 |
+
assert not left <= right
|
| 1129 |
+
assert not left >= right
|
| 1130 |
+
|
| 1131 |
+
@pytest.mark.parametrize(
|
| 1132 |
+
"zerodim_arr, expected",
|
| 1133 |
+
((np.array(0), False), (np.array(Period("2000-01", "M")), True)),
|
| 1134 |
+
)
|
| 1135 |
+
def test_comparison_numpy_zerodim_arr(self, zerodim_arr, expected):
|
| 1136 |
+
p = Period("2000-01", "M")
|
| 1137 |
+
|
| 1138 |
+
assert (p == zerodim_arr) is expected
|
| 1139 |
+
assert (zerodim_arr == p) is expected
|
| 1140 |
+
|
| 1141 |
+
|
| 1142 |
+
class TestArithmetic:
|
| 1143 |
+
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s", "m"])
|
| 1144 |
+
def test_add_sub_td64_nat(self, unit):
|
| 1145 |
+
# GH#47196
|
| 1146 |
+
per = Period("2022-06-01", "D")
|
| 1147 |
+
nat = np.timedelta64("NaT", unit)
|
| 1148 |
+
|
| 1149 |
+
assert per + nat is NaT
|
| 1150 |
+
assert nat + per is NaT
|
| 1151 |
+
assert per - nat is NaT
|
| 1152 |
+
|
| 1153 |
+
with pytest.raises(TypeError, match="unsupported operand"):
|
| 1154 |
+
nat - per
|
| 1155 |
+
|
| 1156 |
+
def test_sub_delta(self):
|
| 1157 |
+
left, right = Period("2011", freq="A"), Period("2007", freq="A")
|
| 1158 |
+
result = left - right
|
| 1159 |
+
assert result == 4 * right.freq
|
| 1160 |
+
|
| 1161 |
+
msg = r"Input has different freq=M from Period\(freq=A-DEC\)"
|
| 1162 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1163 |
+
left - Period("2007-01", freq="M")
|
| 1164 |
+
|
| 1165 |
+
def test_add_integer(self):
|
| 1166 |
+
per1 = Period(freq="D", year=2008, month=1, day=1)
|
| 1167 |
+
per2 = Period(freq="D", year=2008, month=1, day=2)
|
| 1168 |
+
assert per1 + 1 == per2
|
| 1169 |
+
assert 1 + per1 == per2
|
| 1170 |
+
|
| 1171 |
+
def test_add_sub_nat(self):
|
| 1172 |
+
# GH#13071
|
| 1173 |
+
p = Period("2011-01", freq="M")
|
| 1174 |
+
assert p + NaT is NaT
|
| 1175 |
+
assert NaT + p is NaT
|
| 1176 |
+
assert p - NaT is NaT
|
| 1177 |
+
assert NaT - p is NaT
|
| 1178 |
+
|
| 1179 |
+
def test_add_invalid(self):
|
| 1180 |
+
# GH#4731
|
| 1181 |
+
per1 = Period(freq="D", year=2008, month=1, day=1)
|
| 1182 |
+
per2 = Period(freq="D", year=2008, month=1, day=2)
|
| 1183 |
+
|
| 1184 |
+
msg = "|".join(
|
| 1185 |
+
[
|
| 1186 |
+
r"unsupported operand type\(s\)",
|
| 1187 |
+
"can only concatenate str",
|
| 1188 |
+
"must be str, not Period",
|
| 1189 |
+
]
|
| 1190 |
+
)
|
| 1191 |
+
with pytest.raises(TypeError, match=msg):
|
| 1192 |
+
per1 + "str"
|
| 1193 |
+
with pytest.raises(TypeError, match=msg):
|
| 1194 |
+
"str" + per1
|
| 1195 |
+
with pytest.raises(TypeError, match=msg):
|
| 1196 |
+
per1 + per2
|
| 1197 |
+
|
| 1198 |
+
boxes = [lambda x: x, lambda x: pd.Series([x]), lambda x: pd.Index([x])]
|
| 1199 |
+
ids = ["identity", "Series", "Index"]
|
| 1200 |
+
|
| 1201 |
+
@pytest.mark.parametrize("lbox", boxes, ids=ids)
|
| 1202 |
+
@pytest.mark.parametrize("rbox", boxes, ids=ids)
|
| 1203 |
+
def test_add_timestamp_raises(self, rbox, lbox):
|
| 1204 |
+
# GH#17983
|
| 1205 |
+
ts = Timestamp("2017")
|
| 1206 |
+
per = Period("2017", freq="M")
|
| 1207 |
+
|
| 1208 |
+
# We may get a different message depending on which class raises
|
| 1209 |
+
# the error.
|
| 1210 |
+
msg = "|".join(
|
| 1211 |
+
[
|
| 1212 |
+
"cannot add",
|
| 1213 |
+
"unsupported operand",
|
| 1214 |
+
"can only operate on a",
|
| 1215 |
+
"incompatible type",
|
| 1216 |
+
"ufunc add cannot use operands",
|
| 1217 |
+
]
|
| 1218 |
+
)
|
| 1219 |
+
with pytest.raises(TypeError, match=msg):
|
| 1220 |
+
lbox(ts) + rbox(per)
|
| 1221 |
+
|
| 1222 |
+
with pytest.raises(TypeError, match=msg):
|
| 1223 |
+
lbox(per) + rbox(ts)
|
| 1224 |
+
|
| 1225 |
+
with pytest.raises(TypeError, match=msg):
|
| 1226 |
+
lbox(per) + rbox(per)
|
| 1227 |
+
|
| 1228 |
+
def test_sub(self):
|
| 1229 |
+
per1 = Period("2011-01-01", freq="D")
|
| 1230 |
+
per2 = Period("2011-01-15", freq="D")
|
| 1231 |
+
|
| 1232 |
+
off = per1.freq
|
| 1233 |
+
assert per1 - per2 == -14 * off
|
| 1234 |
+
assert per2 - per1 == 14 * off
|
| 1235 |
+
|
| 1236 |
+
msg = r"Input has different freq=M from Period\(freq=D\)"
|
| 1237 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1238 |
+
per1 - Period("2011-02", freq="M")
|
| 1239 |
+
|
| 1240 |
+
@pytest.mark.parametrize("n", [1, 2, 3, 4])
|
| 1241 |
+
def test_sub_n_gt_1_ticks(self, tick_classes, n):
|
| 1242 |
+
# GH 23878
|
| 1243 |
+
p1 = Period("19910905", freq=tick_classes(n))
|
| 1244 |
+
p2 = Period("19920406", freq=tick_classes(n))
|
| 1245 |
+
|
| 1246 |
+
expected = Period(str(p2), freq=p2.freq.base) - Period(
|
| 1247 |
+
str(p1), freq=p1.freq.base
|
| 1248 |
+
)
|
| 1249 |
+
|
| 1250 |
+
assert (p2 - p1) == expected
|
| 1251 |
+
|
| 1252 |
+
@pytest.mark.parametrize("normalize", [True, False])
|
| 1253 |
+
@pytest.mark.parametrize("n", [1, 2, 3, 4])
|
| 1254 |
+
@pytest.mark.parametrize(
|
| 1255 |
+
"offset, kwd_name",
|
| 1256 |
+
[
|
| 1257 |
+
(offsets.YearEnd, "month"),
|
| 1258 |
+
(offsets.QuarterEnd, "startingMonth"),
|
| 1259 |
+
(offsets.MonthEnd, None),
|
| 1260 |
+
(offsets.Week, "weekday"),
|
| 1261 |
+
],
|
| 1262 |
+
)
|
| 1263 |
+
def test_sub_n_gt_1_offsets(self, offset, kwd_name, n, normalize):
|
| 1264 |
+
# GH 23878
|
| 1265 |
+
kwds = {kwd_name: 3} if kwd_name is not None else {}
|
| 1266 |
+
p1_d = "19910905"
|
| 1267 |
+
p2_d = "19920406"
|
| 1268 |
+
p1 = Period(p1_d, freq=offset(n, normalize, **kwds))
|
| 1269 |
+
p2 = Period(p2_d, freq=offset(n, normalize, **kwds))
|
| 1270 |
+
|
| 1271 |
+
expected = Period(p2_d, freq=p2.freq.base) - Period(p1_d, freq=p1.freq.base)
|
| 1272 |
+
|
| 1273 |
+
assert (p2 - p1) == expected
|
| 1274 |
+
|
| 1275 |
+
def test_add_offset(self):
|
| 1276 |
+
# freq is DateOffset
|
| 1277 |
+
for freq in ["A", "2A", "3A"]:
|
| 1278 |
+
p = Period("2011", freq=freq)
|
| 1279 |
+
exp = Period("2013", freq=freq)
|
| 1280 |
+
assert p + offsets.YearEnd(2) == exp
|
| 1281 |
+
assert offsets.YearEnd(2) + p == exp
|
| 1282 |
+
|
| 1283 |
+
for o in [
|
| 1284 |
+
offsets.YearBegin(2),
|
| 1285 |
+
offsets.MonthBegin(1),
|
| 1286 |
+
offsets.Minute(),
|
| 1287 |
+
np.timedelta64(365, "D"),
|
| 1288 |
+
timedelta(365),
|
| 1289 |
+
]:
|
| 1290 |
+
msg = "Input has different freq|Input cannot be converted to Period"
|
| 1291 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1292 |
+
p + o
|
| 1293 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1294 |
+
o + p
|
| 1295 |
+
|
| 1296 |
+
for freq in ["M", "2M", "3M"]:
|
| 1297 |
+
p = Period("2011-03", freq=freq)
|
| 1298 |
+
exp = Period("2011-05", freq=freq)
|
| 1299 |
+
assert p + offsets.MonthEnd(2) == exp
|
| 1300 |
+
assert offsets.MonthEnd(2) + p == exp
|
| 1301 |
+
|
| 1302 |
+
exp = Period("2012-03", freq=freq)
|
| 1303 |
+
assert p + offsets.MonthEnd(12) == exp
|
| 1304 |
+
assert offsets.MonthEnd(12) + p == exp
|
| 1305 |
+
|
| 1306 |
+
msg = "|".join(
|
| 1307 |
+
[
|
| 1308 |
+
"Input has different freq",
|
| 1309 |
+
"Input cannot be converted to Period",
|
| 1310 |
+
]
|
| 1311 |
+
)
|
| 1312 |
+
|
| 1313 |
+
for o in [
|
| 1314 |
+
offsets.YearBegin(2),
|
| 1315 |
+
offsets.MonthBegin(1),
|
| 1316 |
+
offsets.Minute(),
|
| 1317 |
+
np.timedelta64(365, "D"),
|
| 1318 |
+
timedelta(365),
|
| 1319 |
+
]:
|
| 1320 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1321 |
+
p + o
|
| 1322 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1323 |
+
o + p
|
| 1324 |
+
|
| 1325 |
+
# freq is Tick
|
| 1326 |
+
for freq in ["D", "2D", "3D"]:
|
| 1327 |
+
p = Period("2011-04-01", freq=freq)
|
| 1328 |
+
|
| 1329 |
+
exp = Period("2011-04-06", freq=freq)
|
| 1330 |
+
assert p + offsets.Day(5) == exp
|
| 1331 |
+
assert offsets.Day(5) + p == exp
|
| 1332 |
+
|
| 1333 |
+
exp = Period("2011-04-02", freq=freq)
|
| 1334 |
+
assert p + offsets.Hour(24) == exp
|
| 1335 |
+
assert offsets.Hour(24) + p == exp
|
| 1336 |
+
|
| 1337 |
+
exp = Period("2011-04-03", freq=freq)
|
| 1338 |
+
assert p + np.timedelta64(2, "D") == exp
|
| 1339 |
+
assert np.timedelta64(2, "D") + p == exp
|
| 1340 |
+
|
| 1341 |
+
exp = Period("2011-04-02", freq=freq)
|
| 1342 |
+
assert p + np.timedelta64(3600 * 24, "s") == exp
|
| 1343 |
+
assert np.timedelta64(3600 * 24, "s") + p == exp
|
| 1344 |
+
|
| 1345 |
+
exp = Period("2011-03-30", freq=freq)
|
| 1346 |
+
assert p + timedelta(-2) == exp
|
| 1347 |
+
assert timedelta(-2) + p == exp
|
| 1348 |
+
|
| 1349 |
+
exp = Period("2011-04-03", freq=freq)
|
| 1350 |
+
assert p + timedelta(hours=48) == exp
|
| 1351 |
+
assert timedelta(hours=48) + p == exp
|
| 1352 |
+
|
| 1353 |
+
msg = "|".join(
|
| 1354 |
+
[
|
| 1355 |
+
"Input has different freq",
|
| 1356 |
+
"Input cannot be converted to Period",
|
| 1357 |
+
]
|
| 1358 |
+
)
|
| 1359 |
+
|
| 1360 |
+
for o in [
|
| 1361 |
+
offsets.YearBegin(2),
|
| 1362 |
+
offsets.MonthBegin(1),
|
| 1363 |
+
offsets.Minute(),
|
| 1364 |
+
np.timedelta64(4, "h"),
|
| 1365 |
+
timedelta(hours=23),
|
| 1366 |
+
]:
|
| 1367 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1368 |
+
p + o
|
| 1369 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1370 |
+
o + p
|
| 1371 |
+
|
| 1372 |
+
for freq in ["H", "2H", "3H"]:
|
| 1373 |
+
p = Period("2011-04-01 09:00", freq=freq)
|
| 1374 |
+
|
| 1375 |
+
exp = Period("2011-04-03 09:00", freq=freq)
|
| 1376 |
+
assert p + offsets.Day(2) == exp
|
| 1377 |
+
assert offsets.Day(2) + p == exp
|
| 1378 |
+
|
| 1379 |
+
exp = Period("2011-04-01 12:00", freq=freq)
|
| 1380 |
+
assert p + offsets.Hour(3) == exp
|
| 1381 |
+
assert offsets.Hour(3) + p == exp
|
| 1382 |
+
|
| 1383 |
+
msg = "cannot use operands with types"
|
| 1384 |
+
exp = Period("2011-04-01 12:00", freq=freq)
|
| 1385 |
+
assert p + np.timedelta64(3, "h") == exp
|
| 1386 |
+
assert np.timedelta64(3, "h") + p == exp
|
| 1387 |
+
|
| 1388 |
+
exp = Period("2011-04-01 10:00", freq=freq)
|
| 1389 |
+
assert p + np.timedelta64(3600, "s") == exp
|
| 1390 |
+
assert np.timedelta64(3600, "s") + p == exp
|
| 1391 |
+
|
| 1392 |
+
exp = Period("2011-04-01 11:00", freq=freq)
|
| 1393 |
+
assert p + timedelta(minutes=120) == exp
|
| 1394 |
+
assert timedelta(minutes=120) + p == exp
|
| 1395 |
+
|
| 1396 |
+
exp = Period("2011-04-05 12:00", freq=freq)
|
| 1397 |
+
assert p + timedelta(days=4, minutes=180) == exp
|
| 1398 |
+
assert timedelta(days=4, minutes=180) + p == exp
|
| 1399 |
+
|
| 1400 |
+
msg = "|".join(
|
| 1401 |
+
[
|
| 1402 |
+
"Input has different freq",
|
| 1403 |
+
"Input cannot be converted to Period",
|
| 1404 |
+
]
|
| 1405 |
+
)
|
| 1406 |
+
|
| 1407 |
+
for o in [
|
| 1408 |
+
offsets.YearBegin(2),
|
| 1409 |
+
offsets.MonthBegin(1),
|
| 1410 |
+
offsets.Minute(),
|
| 1411 |
+
np.timedelta64(3200, "s"),
|
| 1412 |
+
timedelta(hours=23, minutes=30),
|
| 1413 |
+
]:
|
| 1414 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1415 |
+
p + o
|
| 1416 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1417 |
+
o + p
|
| 1418 |
+
|
| 1419 |
+
def test_sub_offset(self):
|
| 1420 |
+
# freq is DateOffset
|
| 1421 |
+
msg = "|".join(
|
| 1422 |
+
[
|
| 1423 |
+
"Input has different freq",
|
| 1424 |
+
"Input cannot be converted to Period",
|
| 1425 |
+
]
|
| 1426 |
+
)
|
| 1427 |
+
|
| 1428 |
+
for freq in ["A", "2A", "3A"]:
|
| 1429 |
+
p = Period("2011", freq=freq)
|
| 1430 |
+
assert p - offsets.YearEnd(2) == Period("2009", freq=freq)
|
| 1431 |
+
|
| 1432 |
+
for o in [
|
| 1433 |
+
offsets.YearBegin(2),
|
| 1434 |
+
offsets.MonthBegin(1),
|
| 1435 |
+
offsets.Minute(),
|
| 1436 |
+
np.timedelta64(365, "D"),
|
| 1437 |
+
timedelta(365),
|
| 1438 |
+
]:
|
| 1439 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1440 |
+
p - o
|
| 1441 |
+
|
| 1442 |
+
for freq in ["M", "2M", "3M"]:
|
| 1443 |
+
p = Period("2011-03", freq=freq)
|
| 1444 |
+
assert p - offsets.MonthEnd(2) == Period("2011-01", freq=freq)
|
| 1445 |
+
assert p - offsets.MonthEnd(12) == Period("2010-03", freq=freq)
|
| 1446 |
+
|
| 1447 |
+
for o in [
|
| 1448 |
+
offsets.YearBegin(2),
|
| 1449 |
+
offsets.MonthBegin(1),
|
| 1450 |
+
offsets.Minute(),
|
| 1451 |
+
np.timedelta64(365, "D"),
|
| 1452 |
+
timedelta(365),
|
| 1453 |
+
]:
|
| 1454 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1455 |
+
p - o
|
| 1456 |
+
|
| 1457 |
+
# freq is Tick
|
| 1458 |
+
for freq in ["D", "2D", "3D"]:
|
| 1459 |
+
p = Period("2011-04-01", freq=freq)
|
| 1460 |
+
assert p - offsets.Day(5) == Period("2011-03-27", freq=freq)
|
| 1461 |
+
assert p - offsets.Hour(24) == Period("2011-03-31", freq=freq)
|
| 1462 |
+
assert p - np.timedelta64(2, "D") == Period("2011-03-30", freq=freq)
|
| 1463 |
+
assert p - np.timedelta64(3600 * 24, "s") == Period("2011-03-31", freq=freq)
|
| 1464 |
+
assert p - timedelta(-2) == Period("2011-04-03", freq=freq)
|
| 1465 |
+
assert p - timedelta(hours=48) == Period("2011-03-30", freq=freq)
|
| 1466 |
+
|
| 1467 |
+
for o in [
|
| 1468 |
+
offsets.YearBegin(2),
|
| 1469 |
+
offsets.MonthBegin(1),
|
| 1470 |
+
offsets.Minute(),
|
| 1471 |
+
np.timedelta64(4, "h"),
|
| 1472 |
+
timedelta(hours=23),
|
| 1473 |
+
]:
|
| 1474 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1475 |
+
p - o
|
| 1476 |
+
|
| 1477 |
+
for freq in ["H", "2H", "3H"]:
|
| 1478 |
+
p = Period("2011-04-01 09:00", freq=freq)
|
| 1479 |
+
assert p - offsets.Day(2) == Period("2011-03-30 09:00", freq=freq)
|
| 1480 |
+
assert p - offsets.Hour(3) == Period("2011-04-01 06:00", freq=freq)
|
| 1481 |
+
assert p - np.timedelta64(3, "h") == Period("2011-04-01 06:00", freq=freq)
|
| 1482 |
+
assert p - np.timedelta64(3600, "s") == Period(
|
| 1483 |
+
"2011-04-01 08:00", freq=freq
|
| 1484 |
+
)
|
| 1485 |
+
assert p - timedelta(minutes=120) == Period("2011-04-01 07:00", freq=freq)
|
| 1486 |
+
assert p - timedelta(days=4, minutes=180) == Period(
|
| 1487 |
+
"2011-03-28 06:00", freq=freq
|
| 1488 |
+
)
|
| 1489 |
+
|
| 1490 |
+
for o in [
|
| 1491 |
+
offsets.YearBegin(2),
|
| 1492 |
+
offsets.MonthBegin(1),
|
| 1493 |
+
offsets.Minute(),
|
| 1494 |
+
np.timedelta64(3200, "s"),
|
| 1495 |
+
timedelta(hours=23, minutes=30),
|
| 1496 |
+
]:
|
| 1497 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1498 |
+
p - o
|
| 1499 |
+
|
| 1500 |
+
@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
|
| 1501 |
+
def test_period_addsub_nat(self, freq):
|
| 1502 |
+
per = Period("2011-01", freq=freq)
|
| 1503 |
+
|
| 1504 |
+
# For subtraction, NaT is treated as another Period object
|
| 1505 |
+
assert NaT - per is NaT
|
| 1506 |
+
assert per - NaT is NaT
|
| 1507 |
+
|
| 1508 |
+
# For addition, NaT is treated as offset-like
|
| 1509 |
+
assert NaT + per is NaT
|
| 1510 |
+
assert per + NaT is NaT
|
| 1511 |
+
|
| 1512 |
+
def test_period_ops_offset(self):
|
| 1513 |
+
p = Period("2011-04-01", freq="D")
|
| 1514 |
+
result = p + offsets.Day()
|
| 1515 |
+
exp = Period("2011-04-02", freq="D")
|
| 1516 |
+
assert result == exp
|
| 1517 |
+
|
| 1518 |
+
result = p - offsets.Day(2)
|
| 1519 |
+
exp = Period("2011-03-30", freq="D")
|
| 1520 |
+
assert result == exp
|
| 1521 |
+
|
| 1522 |
+
msg = r"Input cannot be converted to Period\(freq=D\)"
|
| 1523 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1524 |
+
p + offsets.Hour(2)
|
| 1525 |
+
|
| 1526 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
| 1527 |
+
p - offsets.Hour(2)
|
| 1528 |
+
|
| 1529 |
+
|
| 1530 |
+
def test_period_immutable():
|
| 1531 |
+
# see gh-17116
|
| 1532 |
+
msg = "not writable"
|
| 1533 |
+
|
| 1534 |
+
per = Period("2014Q1")
|
| 1535 |
+
with pytest.raises(AttributeError, match=msg):
|
| 1536 |
+
per.ordinal = 14
|
| 1537 |
+
|
| 1538 |
+
freq = per.freq
|
| 1539 |
+
with pytest.raises(AttributeError, match=msg):
|
| 1540 |
+
per.freq = 2 * freq
|
| 1541 |
+
|
| 1542 |
+
|
| 1543 |
+
def test_small_year_parsing():
|
| 1544 |
+
per1 = Period("0001-01-07", "D")
|
| 1545 |
+
assert per1.year == 1
|
| 1546 |
+
assert per1.day == 7
|
| 1547 |
+
|
| 1548 |
+
|
| 1549 |
+
def test_negone_ordinals():
|
| 1550 |
+
freqs = ["A", "M", "Q", "D", "H", "T", "S"]
|
| 1551 |
+
|
| 1552 |
+
period = Period(ordinal=-1, freq="D")
|
| 1553 |
+
for freq in freqs:
|
| 1554 |
+
repr(period.asfreq(freq))
|
| 1555 |
+
|
| 1556 |
+
for freq in freqs:
|
| 1557 |
+
period = Period(ordinal=-1, freq=freq)
|
| 1558 |
+
repr(period)
|
| 1559 |
+
assert period.year == 1969
|
| 1560 |
+
|
| 1561 |
+
period = Period(ordinal=-1, freq="B")
|
| 1562 |
+
repr(period)
|
| 1563 |
+
period = Period(ordinal=-1, freq="W")
|
| 1564 |
+
repr(period)
|
| 1565 |
+
|
| 1566 |
+
|
| 1567 |
+
def test_invalid_frequency_error_message():
|
| 1568 |
+
msg = "Invalid frequency: <WeekOfMonth: week=0, weekday=0>"
|
| 1569 |
+
with pytest.raises(ValueError, match=msg):
|
| 1570 |
+
Period("2012-01-02", freq="WOM-1MON")
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/test_na_scalar.py
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import (
|
| 2 |
+
date,
|
| 3 |
+
time,
|
| 4 |
+
timedelta,
|
| 5 |
+
)
|
| 6 |
+
import pickle
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
from pandas._libs.missing import NA
|
| 12 |
+
|
| 13 |
+
from pandas.core.dtypes.common import is_scalar
|
| 14 |
+
|
| 15 |
+
import pandas as pd
|
| 16 |
+
import pandas._testing as tm
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def test_singleton():
|
| 20 |
+
assert NA is NA
|
| 21 |
+
new_NA = type(NA)()
|
| 22 |
+
assert new_NA is NA
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_repr():
|
| 26 |
+
assert repr(NA) == "<NA>"
|
| 27 |
+
assert str(NA) == "<NA>"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_format():
|
| 31 |
+
# GH-34740
|
| 32 |
+
assert format(NA) == "<NA>"
|
| 33 |
+
assert format(NA, ">10") == " <NA>"
|
| 34 |
+
assert format(NA, "xxx") == "<NA>" # NA is flexible, accept any format spec
|
| 35 |
+
|
| 36 |
+
assert f"{NA}" == "<NA>"
|
| 37 |
+
assert f"{NA:>10}" == " <NA>"
|
| 38 |
+
assert f"{NA:xxx}" == "<NA>"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_truthiness():
|
| 42 |
+
msg = "boolean value of NA is ambiguous"
|
| 43 |
+
|
| 44 |
+
with pytest.raises(TypeError, match=msg):
|
| 45 |
+
bool(NA)
|
| 46 |
+
|
| 47 |
+
with pytest.raises(TypeError, match=msg):
|
| 48 |
+
not NA
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_hashable():
|
| 52 |
+
assert hash(NA) == hash(NA)
|
| 53 |
+
d = {NA: "test"}
|
| 54 |
+
assert d[NA] == "test"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@pytest.mark.parametrize(
|
| 58 |
+
"other", [NA, 1, 1.0, "a", b"a", np.int64(1), np.nan], ids=repr
|
| 59 |
+
)
|
| 60 |
+
def test_arithmetic_ops(all_arithmetic_functions, other):
|
| 61 |
+
op = all_arithmetic_functions
|
| 62 |
+
|
| 63 |
+
if op.__name__ in ("pow", "rpow", "rmod") and isinstance(other, (str, bytes)):
|
| 64 |
+
pytest.skip(reason=f"{op.__name__} with NA and {other} not defined.")
|
| 65 |
+
if op.__name__ in ("divmod", "rdivmod"):
|
| 66 |
+
assert op(NA, other) is (NA, NA)
|
| 67 |
+
else:
|
| 68 |
+
if op.__name__ == "rpow":
|
| 69 |
+
# avoid special case
|
| 70 |
+
other += 1
|
| 71 |
+
assert op(NA, other) is NA
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@pytest.mark.parametrize(
|
| 75 |
+
"other",
|
| 76 |
+
[
|
| 77 |
+
NA,
|
| 78 |
+
1,
|
| 79 |
+
1.0,
|
| 80 |
+
"a",
|
| 81 |
+
b"a",
|
| 82 |
+
np.int64(1),
|
| 83 |
+
np.nan,
|
| 84 |
+
np.bool_(True),
|
| 85 |
+
time(0),
|
| 86 |
+
date(1, 2, 3),
|
| 87 |
+
timedelta(1),
|
| 88 |
+
pd.NaT,
|
| 89 |
+
],
|
| 90 |
+
)
|
| 91 |
+
def test_comparison_ops(comparison_op, other):
|
| 92 |
+
assert comparison_op(NA, other) is NA
|
| 93 |
+
assert comparison_op(other, NA) is NA
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@pytest.mark.parametrize(
|
| 97 |
+
"value",
|
| 98 |
+
[
|
| 99 |
+
0,
|
| 100 |
+
0.0,
|
| 101 |
+
-0,
|
| 102 |
+
-0.0,
|
| 103 |
+
False,
|
| 104 |
+
np.bool_(False),
|
| 105 |
+
np.int_(0),
|
| 106 |
+
np.float_(0),
|
| 107 |
+
np.int_(-0),
|
| 108 |
+
np.float_(-0),
|
| 109 |
+
],
|
| 110 |
+
)
|
| 111 |
+
@pytest.mark.parametrize("asarray", [True, False])
|
| 112 |
+
def test_pow_special(value, asarray):
|
| 113 |
+
if asarray:
|
| 114 |
+
value = np.array([value])
|
| 115 |
+
result = NA**value
|
| 116 |
+
|
| 117 |
+
if asarray:
|
| 118 |
+
result = result[0]
|
| 119 |
+
else:
|
| 120 |
+
# this assertion isn't possible for ndarray.
|
| 121 |
+
assert isinstance(result, type(value))
|
| 122 |
+
assert result == 1
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
@pytest.mark.parametrize(
|
| 126 |
+
"value", [1, 1.0, True, np.bool_(True), np.int_(1), np.float_(1)]
|
| 127 |
+
)
|
| 128 |
+
@pytest.mark.parametrize("asarray", [True, False])
|
| 129 |
+
def test_rpow_special(value, asarray):
|
| 130 |
+
if asarray:
|
| 131 |
+
value = np.array([value])
|
| 132 |
+
result = value**NA
|
| 133 |
+
|
| 134 |
+
if asarray:
|
| 135 |
+
result = result[0]
|
| 136 |
+
elif not isinstance(value, (np.float_, np.bool_, np.int_)):
|
| 137 |
+
# this assertion isn't possible with asarray=True
|
| 138 |
+
assert isinstance(result, type(value))
|
| 139 |
+
|
| 140 |
+
assert result == value
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
@pytest.mark.parametrize("value", [-1, -1.0, np.int_(-1), np.float_(-1)])
|
| 144 |
+
@pytest.mark.parametrize("asarray", [True, False])
|
| 145 |
+
def test_rpow_minus_one(value, asarray):
|
| 146 |
+
if asarray:
|
| 147 |
+
value = np.array([value])
|
| 148 |
+
result = value**NA
|
| 149 |
+
|
| 150 |
+
if asarray:
|
| 151 |
+
result = result[0]
|
| 152 |
+
|
| 153 |
+
assert pd.isna(result)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def test_unary_ops():
|
| 157 |
+
assert +NA is NA
|
| 158 |
+
assert -NA is NA
|
| 159 |
+
assert abs(NA) is NA
|
| 160 |
+
assert ~NA is NA
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def test_logical_and():
|
| 164 |
+
assert NA & True is NA
|
| 165 |
+
assert True & NA is NA
|
| 166 |
+
assert NA & False is False
|
| 167 |
+
assert False & NA is False
|
| 168 |
+
assert NA & NA is NA
|
| 169 |
+
|
| 170 |
+
msg = "unsupported operand type"
|
| 171 |
+
with pytest.raises(TypeError, match=msg):
|
| 172 |
+
NA & 5
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def test_logical_or():
|
| 176 |
+
assert NA | True is True
|
| 177 |
+
assert True | NA is True
|
| 178 |
+
assert NA | False is NA
|
| 179 |
+
assert False | NA is NA
|
| 180 |
+
assert NA | NA is NA
|
| 181 |
+
|
| 182 |
+
msg = "unsupported operand type"
|
| 183 |
+
with pytest.raises(TypeError, match=msg):
|
| 184 |
+
NA | 5
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def test_logical_xor():
|
| 188 |
+
assert NA ^ True is NA
|
| 189 |
+
assert True ^ NA is NA
|
| 190 |
+
assert NA ^ False is NA
|
| 191 |
+
assert False ^ NA is NA
|
| 192 |
+
assert NA ^ NA is NA
|
| 193 |
+
|
| 194 |
+
msg = "unsupported operand type"
|
| 195 |
+
with pytest.raises(TypeError, match=msg):
|
| 196 |
+
NA ^ 5
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def test_logical_not():
|
| 200 |
+
assert ~NA is NA
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
@pytest.mark.parametrize("shape", [(3,), (3, 3), (1, 2, 3)])
|
| 204 |
+
def test_arithmetic_ndarray(shape, all_arithmetic_functions):
|
| 205 |
+
op = all_arithmetic_functions
|
| 206 |
+
a = np.zeros(shape)
|
| 207 |
+
if op.__name__ == "pow":
|
| 208 |
+
a += 5
|
| 209 |
+
result = op(NA, a)
|
| 210 |
+
expected = np.full(a.shape, NA, dtype=object)
|
| 211 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def test_is_scalar():
|
| 215 |
+
assert is_scalar(NA) is True
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
def test_isna():
|
| 219 |
+
assert pd.isna(NA) is True
|
| 220 |
+
assert pd.notna(NA) is False
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def test_series_isna():
|
| 224 |
+
s = pd.Series([1, NA], dtype=object)
|
| 225 |
+
expected = pd.Series([False, True])
|
| 226 |
+
tm.assert_series_equal(s.isna(), expected)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def test_ufunc():
|
| 230 |
+
assert np.log(NA) is NA
|
| 231 |
+
assert np.add(NA, 1) is NA
|
| 232 |
+
result = np.divmod(NA, 1)
|
| 233 |
+
assert result[0] is NA and result[1] is NA
|
| 234 |
+
|
| 235 |
+
result = np.frexp(NA)
|
| 236 |
+
assert result[0] is NA and result[1] is NA
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def test_ufunc_raises():
|
| 240 |
+
msg = "ufunc method 'at'"
|
| 241 |
+
with pytest.raises(ValueError, match=msg):
|
| 242 |
+
np.log.at(NA, 0)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def test_binary_input_not_dunder():
|
| 246 |
+
a = np.array([1, 2, 3])
|
| 247 |
+
expected = np.array([NA, NA, NA], dtype=object)
|
| 248 |
+
result = np.logaddexp(a, NA)
|
| 249 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 250 |
+
|
| 251 |
+
result = np.logaddexp(NA, a)
|
| 252 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 253 |
+
|
| 254 |
+
# all NA, multiple inputs
|
| 255 |
+
assert np.logaddexp(NA, NA) is NA
|
| 256 |
+
|
| 257 |
+
result = np.modf(NA, NA)
|
| 258 |
+
assert len(result) == 2
|
| 259 |
+
assert all(x is NA for x in result)
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def test_divmod_ufunc():
|
| 263 |
+
# binary in, binary out.
|
| 264 |
+
a = np.array([1, 2, 3])
|
| 265 |
+
expected = np.array([NA, NA, NA], dtype=object)
|
| 266 |
+
|
| 267 |
+
result = np.divmod(a, NA)
|
| 268 |
+
assert isinstance(result, tuple)
|
| 269 |
+
for arr in result:
|
| 270 |
+
tm.assert_numpy_array_equal(arr, expected)
|
| 271 |
+
tm.assert_numpy_array_equal(arr, expected)
|
| 272 |
+
|
| 273 |
+
result = np.divmod(NA, a)
|
| 274 |
+
for arr in result:
|
| 275 |
+
tm.assert_numpy_array_equal(arr, expected)
|
| 276 |
+
tm.assert_numpy_array_equal(arr, expected)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def test_integer_hash_collision_dict():
|
| 280 |
+
# GH 30013
|
| 281 |
+
result = {NA: "foo", hash(NA): "bar"}
|
| 282 |
+
|
| 283 |
+
assert result[NA] == "foo"
|
| 284 |
+
assert result[hash(NA)] == "bar"
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def test_integer_hash_collision_set():
|
| 288 |
+
# GH 30013
|
| 289 |
+
result = {NA, hash(NA)}
|
| 290 |
+
|
| 291 |
+
assert len(result) == 2
|
| 292 |
+
assert NA in result
|
| 293 |
+
assert hash(NA) in result
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def test_pickle_roundtrip():
|
| 297 |
+
# https://github.com/pandas-dev/pandas/issues/31847
|
| 298 |
+
result = pickle.loads(pickle.dumps(NA))
|
| 299 |
+
assert result is NA
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
def test_pickle_roundtrip_pandas():
|
| 303 |
+
result = tm.round_trip_pickle(NA)
|
| 304 |
+
assert result is NA
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
@pytest.mark.parametrize(
|
| 308 |
+
"values, dtype", [([1, 2, NA], "Int64"), (["A", "B", NA], "string")]
|
| 309 |
+
)
|
| 310 |
+
@pytest.mark.parametrize("as_frame", [True, False])
|
| 311 |
+
def test_pickle_roundtrip_containers(as_frame, values, dtype):
|
| 312 |
+
s = pd.Series(pd.array(values, dtype=dtype))
|
| 313 |
+
if as_frame:
|
| 314 |
+
s = s.to_frame(name="A")
|
| 315 |
+
result = tm.round_trip_pickle(s)
|
| 316 |
+
tm.assert_equal(result, s)
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/test_nat.py
ADDED
|
@@ -0,0 +1,696 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import (
|
| 2 |
+
datetime,
|
| 3 |
+
timedelta,
|
| 4 |
+
)
|
| 5 |
+
import operator
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pytest
|
| 9 |
+
import pytz
|
| 10 |
+
|
| 11 |
+
from pandas._libs.tslibs import iNaT
|
| 12 |
+
from pandas.compat.numpy import np_version_gte1p24p3
|
| 13 |
+
|
| 14 |
+
from pandas.core.dtypes.common import is_datetime64_any_dtype
|
| 15 |
+
|
| 16 |
+
from pandas import (
|
| 17 |
+
DatetimeIndex,
|
| 18 |
+
DatetimeTZDtype,
|
| 19 |
+
Index,
|
| 20 |
+
NaT,
|
| 21 |
+
Period,
|
| 22 |
+
Series,
|
| 23 |
+
Timedelta,
|
| 24 |
+
TimedeltaIndex,
|
| 25 |
+
Timestamp,
|
| 26 |
+
isna,
|
| 27 |
+
offsets,
|
| 28 |
+
)
|
| 29 |
+
import pandas._testing as tm
|
| 30 |
+
from pandas.core.arrays import (
|
| 31 |
+
DatetimeArray,
|
| 32 |
+
PeriodArray,
|
| 33 |
+
TimedeltaArray,
|
| 34 |
+
)
|
| 35 |
+
from pandas.core.ops import roperator
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@pytest.mark.parametrize(
|
| 39 |
+
"nat,idx",
|
| 40 |
+
[
|
| 41 |
+
(Timestamp("NaT"), DatetimeArray),
|
| 42 |
+
(Timedelta("NaT"), TimedeltaArray),
|
| 43 |
+
(Period("NaT", freq="M"), PeriodArray),
|
| 44 |
+
],
|
| 45 |
+
)
|
| 46 |
+
def test_nat_fields(nat, idx):
|
| 47 |
+
for field in idx._field_ops:
|
| 48 |
+
# weekday is a property of DTI, but a method
|
| 49 |
+
# on NaT/Timestamp for compat with datetime
|
| 50 |
+
if field == "weekday":
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
result = getattr(NaT, field)
|
| 54 |
+
assert np.isnan(result)
|
| 55 |
+
|
| 56 |
+
result = getattr(nat, field)
|
| 57 |
+
assert np.isnan(result)
|
| 58 |
+
|
| 59 |
+
for field in idx._bool_ops:
|
| 60 |
+
result = getattr(NaT, field)
|
| 61 |
+
assert result is False
|
| 62 |
+
|
| 63 |
+
result = getattr(nat, field)
|
| 64 |
+
assert result is False
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_nat_vector_field_access():
|
| 68 |
+
idx = DatetimeIndex(["1/1/2000", None, None, "1/4/2000"])
|
| 69 |
+
|
| 70 |
+
for field in DatetimeArray._field_ops:
|
| 71 |
+
# weekday is a property of DTI, but a method
|
| 72 |
+
# on NaT/Timestamp for compat with datetime
|
| 73 |
+
if field == "weekday":
|
| 74 |
+
continue
|
| 75 |
+
|
| 76 |
+
result = getattr(idx, field)
|
| 77 |
+
expected = Index([getattr(x, field) for x in idx])
|
| 78 |
+
tm.assert_index_equal(result, expected)
|
| 79 |
+
|
| 80 |
+
ser = Series(idx)
|
| 81 |
+
|
| 82 |
+
for field in DatetimeArray._field_ops:
|
| 83 |
+
# weekday is a property of DTI, but a method
|
| 84 |
+
# on NaT/Timestamp for compat with datetime
|
| 85 |
+
if field == "weekday":
|
| 86 |
+
continue
|
| 87 |
+
|
| 88 |
+
result = getattr(ser.dt, field)
|
| 89 |
+
expected = [getattr(x, field) for x in idx]
|
| 90 |
+
tm.assert_series_equal(result, Series(expected))
|
| 91 |
+
|
| 92 |
+
for field in DatetimeArray._bool_ops:
|
| 93 |
+
result = getattr(ser.dt, field)
|
| 94 |
+
expected = [getattr(x, field) for x in idx]
|
| 95 |
+
tm.assert_series_equal(result, Series(expected))
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
@pytest.mark.parametrize("klass", [Timestamp, Timedelta, Period])
|
| 99 |
+
@pytest.mark.parametrize(
|
| 100 |
+
"value", [None, np.nan, iNaT, float("nan"), NaT, "NaT", "nat", "", "NAT"]
|
| 101 |
+
)
|
| 102 |
+
def test_identity(klass, value):
|
| 103 |
+
assert klass(value) is NaT
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@pytest.mark.parametrize("klass", [Timestamp, Timedelta])
|
| 107 |
+
@pytest.mark.parametrize("method", ["round", "floor", "ceil"])
|
| 108 |
+
@pytest.mark.parametrize("freq", ["s", "5s", "min", "5min", "h", "5h"])
|
| 109 |
+
def test_round_nat(klass, method, freq):
|
| 110 |
+
# see gh-14940
|
| 111 |
+
ts = klass("nat")
|
| 112 |
+
|
| 113 |
+
round_method = getattr(ts, method)
|
| 114 |
+
assert round_method(freq) is ts
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
@pytest.mark.parametrize(
|
| 118 |
+
"method",
|
| 119 |
+
[
|
| 120 |
+
"astimezone",
|
| 121 |
+
"combine",
|
| 122 |
+
"ctime",
|
| 123 |
+
"dst",
|
| 124 |
+
"fromordinal",
|
| 125 |
+
"fromtimestamp",
|
| 126 |
+
"fromisocalendar",
|
| 127 |
+
"isocalendar",
|
| 128 |
+
"strftime",
|
| 129 |
+
"strptime",
|
| 130 |
+
"time",
|
| 131 |
+
"timestamp",
|
| 132 |
+
"timetuple",
|
| 133 |
+
"timetz",
|
| 134 |
+
"toordinal",
|
| 135 |
+
"tzname",
|
| 136 |
+
"utcfromtimestamp",
|
| 137 |
+
"utcnow",
|
| 138 |
+
"utcoffset",
|
| 139 |
+
"utctimetuple",
|
| 140 |
+
"timestamp",
|
| 141 |
+
],
|
| 142 |
+
)
|
| 143 |
+
def test_nat_methods_raise(method):
|
| 144 |
+
# see gh-9513, gh-17329
|
| 145 |
+
msg = f"NaTType does not support {method}"
|
| 146 |
+
|
| 147 |
+
with pytest.raises(ValueError, match=msg):
|
| 148 |
+
getattr(NaT, method)()
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
@pytest.mark.parametrize("method", ["weekday", "isoweekday"])
|
| 152 |
+
def test_nat_methods_nan(method):
|
| 153 |
+
# see gh-9513, gh-17329
|
| 154 |
+
assert np.isnan(getattr(NaT, method)())
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
@pytest.mark.parametrize(
|
| 158 |
+
"method", ["date", "now", "replace", "today", "tz_convert", "tz_localize"]
|
| 159 |
+
)
|
| 160 |
+
def test_nat_methods_nat(method):
|
| 161 |
+
# see gh-8254, gh-9513, gh-17329
|
| 162 |
+
assert getattr(NaT, method)() is NaT
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
@pytest.mark.parametrize(
|
| 166 |
+
"get_nat", [lambda x: NaT, lambda x: Timedelta(x), lambda x: Timestamp(x)]
|
| 167 |
+
)
|
| 168 |
+
def test_nat_iso_format(get_nat):
|
| 169 |
+
# see gh-12300
|
| 170 |
+
assert get_nat("NaT").isoformat() == "NaT"
|
| 171 |
+
assert get_nat("NaT").isoformat(timespec="nanoseconds") == "NaT"
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
@pytest.mark.parametrize(
|
| 175 |
+
"klass,expected",
|
| 176 |
+
[
|
| 177 |
+
(Timestamp, ["normalize", "to_julian_date", "to_period", "unit"]),
|
| 178 |
+
(
|
| 179 |
+
Timedelta,
|
| 180 |
+
[
|
| 181 |
+
"components",
|
| 182 |
+
"resolution_string",
|
| 183 |
+
"to_pytimedelta",
|
| 184 |
+
"to_timedelta64",
|
| 185 |
+
"unit",
|
| 186 |
+
"view",
|
| 187 |
+
],
|
| 188 |
+
),
|
| 189 |
+
],
|
| 190 |
+
)
|
| 191 |
+
def test_missing_public_nat_methods(klass, expected):
|
| 192 |
+
# see gh-17327
|
| 193 |
+
#
|
| 194 |
+
# NaT should have *most* of the Timestamp and Timedelta methods.
|
| 195 |
+
# Here, we check which public methods NaT does not have. We
|
| 196 |
+
# ignore any missing private methods.
|
| 197 |
+
nat_names = dir(NaT)
|
| 198 |
+
klass_names = dir(klass)
|
| 199 |
+
|
| 200 |
+
missing = [x for x in klass_names if x not in nat_names and not x.startswith("_")]
|
| 201 |
+
missing.sort()
|
| 202 |
+
|
| 203 |
+
assert missing == expected
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def _get_overlap_public_nat_methods(klass, as_tuple=False):
|
| 207 |
+
"""
|
| 208 |
+
Get overlapping public methods between NaT and another class.
|
| 209 |
+
|
| 210 |
+
Parameters
|
| 211 |
+
----------
|
| 212 |
+
klass : type
|
| 213 |
+
The class to compare with NaT
|
| 214 |
+
as_tuple : bool, default False
|
| 215 |
+
Whether to return a list of tuples of the form (klass, method).
|
| 216 |
+
|
| 217 |
+
Returns
|
| 218 |
+
-------
|
| 219 |
+
overlap : list
|
| 220 |
+
"""
|
| 221 |
+
nat_names = dir(NaT)
|
| 222 |
+
klass_names = dir(klass)
|
| 223 |
+
|
| 224 |
+
overlap = [
|
| 225 |
+
x
|
| 226 |
+
for x in nat_names
|
| 227 |
+
if x in klass_names and not x.startswith("_") and callable(getattr(klass, x))
|
| 228 |
+
]
|
| 229 |
+
|
| 230 |
+
# Timestamp takes precedence over Timedelta in terms of overlap.
|
| 231 |
+
if klass is Timedelta:
|
| 232 |
+
ts_names = dir(Timestamp)
|
| 233 |
+
overlap = [x for x in overlap if x not in ts_names]
|
| 234 |
+
|
| 235 |
+
if as_tuple:
|
| 236 |
+
overlap = [(klass, method) for method in overlap]
|
| 237 |
+
|
| 238 |
+
overlap.sort()
|
| 239 |
+
return overlap
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
@pytest.mark.parametrize(
|
| 243 |
+
"klass,expected",
|
| 244 |
+
[
|
| 245 |
+
(
|
| 246 |
+
Timestamp,
|
| 247 |
+
[
|
| 248 |
+
"as_unit",
|
| 249 |
+
"astimezone",
|
| 250 |
+
"ceil",
|
| 251 |
+
"combine",
|
| 252 |
+
"ctime",
|
| 253 |
+
"date",
|
| 254 |
+
"day_name",
|
| 255 |
+
"dst",
|
| 256 |
+
"floor",
|
| 257 |
+
"fromisocalendar",
|
| 258 |
+
"fromisoformat",
|
| 259 |
+
"fromordinal",
|
| 260 |
+
"fromtimestamp",
|
| 261 |
+
"isocalendar",
|
| 262 |
+
"isoformat",
|
| 263 |
+
"isoweekday",
|
| 264 |
+
"month_name",
|
| 265 |
+
"now",
|
| 266 |
+
"replace",
|
| 267 |
+
"round",
|
| 268 |
+
"strftime",
|
| 269 |
+
"strptime",
|
| 270 |
+
"time",
|
| 271 |
+
"timestamp",
|
| 272 |
+
"timetuple",
|
| 273 |
+
"timetz",
|
| 274 |
+
"to_datetime64",
|
| 275 |
+
"to_numpy",
|
| 276 |
+
"to_pydatetime",
|
| 277 |
+
"today",
|
| 278 |
+
"toordinal",
|
| 279 |
+
"tz_convert",
|
| 280 |
+
"tz_localize",
|
| 281 |
+
"tzname",
|
| 282 |
+
"utcfromtimestamp",
|
| 283 |
+
"utcnow",
|
| 284 |
+
"utcoffset",
|
| 285 |
+
"utctimetuple",
|
| 286 |
+
"weekday",
|
| 287 |
+
],
|
| 288 |
+
),
|
| 289 |
+
(Timedelta, ["total_seconds"]),
|
| 290 |
+
],
|
| 291 |
+
)
|
| 292 |
+
def test_overlap_public_nat_methods(klass, expected):
|
| 293 |
+
# see gh-17327
|
| 294 |
+
#
|
| 295 |
+
# NaT should have *most* of the Timestamp and Timedelta methods.
|
| 296 |
+
# In case when Timestamp, Timedelta, and NaT are overlap, the overlap
|
| 297 |
+
# is considered to be with Timestamp and NaT, not Timedelta.
|
| 298 |
+
assert _get_overlap_public_nat_methods(klass) == expected
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
@pytest.mark.parametrize(
|
| 302 |
+
"compare",
|
| 303 |
+
(
|
| 304 |
+
_get_overlap_public_nat_methods(Timestamp, True)
|
| 305 |
+
+ _get_overlap_public_nat_methods(Timedelta, True)
|
| 306 |
+
),
|
| 307 |
+
ids=lambda x: f"{x[0].__name__}.{x[1]}",
|
| 308 |
+
)
|
| 309 |
+
def test_nat_doc_strings(compare):
|
| 310 |
+
# see gh-17327
|
| 311 |
+
#
|
| 312 |
+
# The docstrings for overlapping methods should match.
|
| 313 |
+
klass, method = compare
|
| 314 |
+
klass_doc = getattr(klass, method).__doc__
|
| 315 |
+
|
| 316 |
+
# Ignore differences with Timestamp.isoformat() as they're intentional
|
| 317 |
+
if klass == Timestamp and method == "isoformat":
|
| 318 |
+
return
|
| 319 |
+
|
| 320 |
+
if method == "to_numpy":
|
| 321 |
+
# GH#44460 can return either dt64 or td64 depending on dtype,
|
| 322 |
+
# different docstring is intentional
|
| 323 |
+
return
|
| 324 |
+
|
| 325 |
+
nat_doc = getattr(NaT, method).__doc__
|
| 326 |
+
assert klass_doc == nat_doc
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
_ops = {
|
| 330 |
+
"left_plus_right": lambda a, b: a + b,
|
| 331 |
+
"right_plus_left": lambda a, b: b + a,
|
| 332 |
+
"left_minus_right": lambda a, b: a - b,
|
| 333 |
+
"right_minus_left": lambda a, b: b - a,
|
| 334 |
+
"left_times_right": lambda a, b: a * b,
|
| 335 |
+
"right_times_left": lambda a, b: b * a,
|
| 336 |
+
"left_div_right": lambda a, b: a / b,
|
| 337 |
+
"right_div_left": lambda a, b: b / a,
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
@pytest.mark.parametrize("op_name", list(_ops.keys()))
|
| 342 |
+
@pytest.mark.parametrize(
|
| 343 |
+
"value,val_type",
|
| 344 |
+
[
|
| 345 |
+
(2, "scalar"),
|
| 346 |
+
(1.5, "floating"),
|
| 347 |
+
(np.nan, "floating"),
|
| 348 |
+
("foo", "str"),
|
| 349 |
+
(timedelta(3600), "timedelta"),
|
| 350 |
+
(Timedelta("5s"), "timedelta"),
|
| 351 |
+
(datetime(2014, 1, 1), "timestamp"),
|
| 352 |
+
(Timestamp("2014-01-01"), "timestamp"),
|
| 353 |
+
(Timestamp("2014-01-01", tz="UTC"), "timestamp"),
|
| 354 |
+
(Timestamp("2014-01-01", tz="US/Eastern"), "timestamp"),
|
| 355 |
+
(pytz.timezone("Asia/Tokyo").localize(datetime(2014, 1, 1)), "timestamp"),
|
| 356 |
+
],
|
| 357 |
+
)
|
| 358 |
+
def test_nat_arithmetic_scalar(op_name, value, val_type):
|
| 359 |
+
# see gh-6873
|
| 360 |
+
invalid_ops = {
|
| 361 |
+
"scalar": {"right_div_left"},
|
| 362 |
+
"floating": {
|
| 363 |
+
"right_div_left",
|
| 364 |
+
"left_minus_right",
|
| 365 |
+
"right_minus_left",
|
| 366 |
+
"left_plus_right",
|
| 367 |
+
"right_plus_left",
|
| 368 |
+
},
|
| 369 |
+
"str": set(_ops.keys()),
|
| 370 |
+
"timedelta": {"left_times_right", "right_times_left"},
|
| 371 |
+
"timestamp": {
|
| 372 |
+
"left_times_right",
|
| 373 |
+
"right_times_left",
|
| 374 |
+
"left_div_right",
|
| 375 |
+
"right_div_left",
|
| 376 |
+
},
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
op = _ops[op_name]
|
| 380 |
+
|
| 381 |
+
if op_name in invalid_ops.get(val_type, set()):
|
| 382 |
+
if (
|
| 383 |
+
val_type == "timedelta"
|
| 384 |
+
and "times" in op_name
|
| 385 |
+
and isinstance(value, Timedelta)
|
| 386 |
+
):
|
| 387 |
+
typs = "(Timedelta|NaTType)"
|
| 388 |
+
msg = rf"unsupported operand type\(s\) for \*: '{typs}' and '{typs}'"
|
| 389 |
+
elif val_type == "str":
|
| 390 |
+
# un-specific check here because the message comes from str
|
| 391 |
+
# and varies by method
|
| 392 |
+
msg = "|".join(
|
| 393 |
+
[
|
| 394 |
+
"can only concatenate str",
|
| 395 |
+
"unsupported operand type",
|
| 396 |
+
"can't multiply sequence",
|
| 397 |
+
"Can't convert 'NaTType'",
|
| 398 |
+
"must be str, not NaTType",
|
| 399 |
+
]
|
| 400 |
+
)
|
| 401 |
+
else:
|
| 402 |
+
msg = "unsupported operand type"
|
| 403 |
+
|
| 404 |
+
with pytest.raises(TypeError, match=msg):
|
| 405 |
+
op(NaT, value)
|
| 406 |
+
else:
|
| 407 |
+
if val_type == "timedelta" and "div" in op_name:
|
| 408 |
+
expected = np.nan
|
| 409 |
+
else:
|
| 410 |
+
expected = NaT
|
| 411 |
+
|
| 412 |
+
assert op(NaT, value) is expected
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
@pytest.mark.parametrize(
|
| 416 |
+
"val,expected", [(np.nan, NaT), (NaT, np.nan), (np.timedelta64("NaT"), np.nan)]
|
| 417 |
+
)
|
| 418 |
+
def test_nat_rfloordiv_timedelta(val, expected):
|
| 419 |
+
# see gh-#18846
|
| 420 |
+
#
|
| 421 |
+
# See also test_timedelta.TestTimedeltaArithmetic.test_floordiv
|
| 422 |
+
td = Timedelta(hours=3, minutes=4)
|
| 423 |
+
assert td // val is expected
|
| 424 |
+
|
| 425 |
+
|
| 426 |
+
@pytest.mark.parametrize(
|
| 427 |
+
"op_name",
|
| 428 |
+
["left_plus_right", "right_plus_left", "left_minus_right", "right_minus_left"],
|
| 429 |
+
)
|
| 430 |
+
@pytest.mark.parametrize(
|
| 431 |
+
"value",
|
| 432 |
+
[
|
| 433 |
+
DatetimeIndex(["2011-01-01", "2011-01-02"], name="x"),
|
| 434 |
+
DatetimeIndex(["2011-01-01", "2011-01-02"], tz="US/Eastern", name="x"),
|
| 435 |
+
DatetimeArray._from_sequence(["2011-01-01", "2011-01-02"]),
|
| 436 |
+
DatetimeArray._from_sequence(
|
| 437 |
+
["2011-01-01", "2011-01-02"], dtype=DatetimeTZDtype(tz="US/Pacific")
|
| 438 |
+
),
|
| 439 |
+
TimedeltaIndex(["1 day", "2 day"], name="x"),
|
| 440 |
+
],
|
| 441 |
+
)
|
| 442 |
+
def test_nat_arithmetic_index(op_name, value):
|
| 443 |
+
# see gh-11718
|
| 444 |
+
exp_name = "x"
|
| 445 |
+
exp_data = [NaT] * 2
|
| 446 |
+
|
| 447 |
+
if is_datetime64_any_dtype(value.dtype) and "plus" in op_name:
|
| 448 |
+
expected = DatetimeIndex(exp_data, tz=value.tz, name=exp_name)
|
| 449 |
+
else:
|
| 450 |
+
expected = TimedeltaIndex(exp_data, name=exp_name)
|
| 451 |
+
|
| 452 |
+
if not isinstance(value, Index):
|
| 453 |
+
expected = expected.array
|
| 454 |
+
|
| 455 |
+
op = _ops[op_name]
|
| 456 |
+
result = op(NaT, value)
|
| 457 |
+
tm.assert_equal(result, expected)
|
| 458 |
+
|
| 459 |
+
|
| 460 |
+
@pytest.mark.parametrize(
|
| 461 |
+
"op_name",
|
| 462 |
+
["left_plus_right", "right_plus_left", "left_minus_right", "right_minus_left"],
|
| 463 |
+
)
|
| 464 |
+
@pytest.mark.parametrize("box", [TimedeltaIndex, Series, TimedeltaArray._from_sequence])
|
| 465 |
+
def test_nat_arithmetic_td64_vector(op_name, box):
|
| 466 |
+
# see gh-19124
|
| 467 |
+
vec = box(["1 day", "2 day"], dtype="timedelta64[ns]")
|
| 468 |
+
box_nat = box([NaT, NaT], dtype="timedelta64[ns]")
|
| 469 |
+
tm.assert_equal(_ops[op_name](vec, NaT), box_nat)
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
@pytest.mark.parametrize(
|
| 473 |
+
"dtype,op,out_dtype",
|
| 474 |
+
[
|
| 475 |
+
("datetime64[ns]", operator.add, "datetime64[ns]"),
|
| 476 |
+
("datetime64[ns]", roperator.radd, "datetime64[ns]"),
|
| 477 |
+
("datetime64[ns]", operator.sub, "timedelta64[ns]"),
|
| 478 |
+
("datetime64[ns]", roperator.rsub, "timedelta64[ns]"),
|
| 479 |
+
("timedelta64[ns]", operator.add, "datetime64[ns]"),
|
| 480 |
+
("timedelta64[ns]", roperator.radd, "datetime64[ns]"),
|
| 481 |
+
("timedelta64[ns]", operator.sub, "datetime64[ns]"),
|
| 482 |
+
("timedelta64[ns]", roperator.rsub, "timedelta64[ns]"),
|
| 483 |
+
],
|
| 484 |
+
)
|
| 485 |
+
def test_nat_arithmetic_ndarray(dtype, op, out_dtype):
|
| 486 |
+
other = np.arange(10).astype(dtype)
|
| 487 |
+
result = op(NaT, other)
|
| 488 |
+
|
| 489 |
+
expected = np.empty(other.shape, dtype=out_dtype)
|
| 490 |
+
expected.fill("NaT")
|
| 491 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
def test_nat_pinned_docstrings():
|
| 495 |
+
# see gh-17327
|
| 496 |
+
assert NaT.ctime.__doc__ == datetime.ctime.__doc__
|
| 497 |
+
|
| 498 |
+
|
| 499 |
+
def test_to_numpy_alias():
|
| 500 |
+
# GH 24653: alias .to_numpy() for scalars
|
| 501 |
+
expected = NaT.to_datetime64()
|
| 502 |
+
result = NaT.to_numpy()
|
| 503 |
+
|
| 504 |
+
assert isna(expected) and isna(result)
|
| 505 |
+
|
| 506 |
+
# GH#44460
|
| 507 |
+
result = NaT.to_numpy("M8[s]")
|
| 508 |
+
assert isinstance(result, np.datetime64)
|
| 509 |
+
assert result.dtype == "M8[s]"
|
| 510 |
+
|
| 511 |
+
result = NaT.to_numpy("m8[ns]")
|
| 512 |
+
assert isinstance(result, np.timedelta64)
|
| 513 |
+
assert result.dtype == "m8[ns]"
|
| 514 |
+
|
| 515 |
+
result = NaT.to_numpy("m8[s]")
|
| 516 |
+
assert isinstance(result, np.timedelta64)
|
| 517 |
+
assert result.dtype == "m8[s]"
|
| 518 |
+
|
| 519 |
+
with pytest.raises(ValueError, match="NaT.to_numpy dtype must be a "):
|
| 520 |
+
NaT.to_numpy(np.int64)
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
@pytest.mark.parametrize(
|
| 524 |
+
"other",
|
| 525 |
+
[
|
| 526 |
+
Timedelta(0),
|
| 527 |
+
Timedelta(0).to_pytimedelta(),
|
| 528 |
+
pytest.param(
|
| 529 |
+
Timedelta(0).to_timedelta64(),
|
| 530 |
+
marks=pytest.mark.xfail(
|
| 531 |
+
not np_version_gte1p24p3,
|
| 532 |
+
reason="td64 doesn't return NotImplemented, see numpy#17017",
|
| 533 |
+
),
|
| 534 |
+
),
|
| 535 |
+
Timestamp(0),
|
| 536 |
+
Timestamp(0).to_pydatetime(),
|
| 537 |
+
pytest.param(
|
| 538 |
+
Timestamp(0).to_datetime64(),
|
| 539 |
+
marks=pytest.mark.xfail(
|
| 540 |
+
not np_version_gte1p24p3,
|
| 541 |
+
reason="dt64 doesn't return NotImplemented, see numpy#17017",
|
| 542 |
+
),
|
| 543 |
+
),
|
| 544 |
+
Timestamp(0).tz_localize("UTC"),
|
| 545 |
+
NaT,
|
| 546 |
+
],
|
| 547 |
+
)
|
| 548 |
+
def test_nat_comparisons(compare_operators_no_eq_ne, other):
|
| 549 |
+
# GH 26039
|
| 550 |
+
opname = compare_operators_no_eq_ne
|
| 551 |
+
|
| 552 |
+
assert getattr(NaT, opname)(other) is False
|
| 553 |
+
|
| 554 |
+
op = getattr(operator, opname.strip("_"))
|
| 555 |
+
assert op(NaT, other) is False
|
| 556 |
+
assert op(other, NaT) is False
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
@pytest.mark.parametrize("other", [np.timedelta64(0, "ns"), np.datetime64("now", "ns")])
|
| 560 |
+
def test_nat_comparisons_numpy(other):
|
| 561 |
+
# Once numpy#17017 is fixed and the xfailed cases in test_nat_comparisons
|
| 562 |
+
# pass, this test can be removed
|
| 563 |
+
assert not NaT == other
|
| 564 |
+
assert NaT != other
|
| 565 |
+
assert not NaT < other
|
| 566 |
+
assert not NaT > other
|
| 567 |
+
assert not NaT <= other
|
| 568 |
+
assert not NaT >= other
|
| 569 |
+
|
| 570 |
+
|
| 571 |
+
@pytest.mark.parametrize("other_and_type", [("foo", "str"), (2, "int"), (2.0, "float")])
|
| 572 |
+
@pytest.mark.parametrize(
|
| 573 |
+
"symbol_and_op",
|
| 574 |
+
[("<=", operator.le), ("<", operator.lt), (">=", operator.ge), (">", operator.gt)],
|
| 575 |
+
)
|
| 576 |
+
def test_nat_comparisons_invalid(other_and_type, symbol_and_op):
|
| 577 |
+
# GH#35585
|
| 578 |
+
other, other_type = other_and_type
|
| 579 |
+
symbol, op = symbol_and_op
|
| 580 |
+
|
| 581 |
+
assert not NaT == other
|
| 582 |
+
assert not other == NaT
|
| 583 |
+
|
| 584 |
+
assert NaT != other
|
| 585 |
+
assert other != NaT
|
| 586 |
+
|
| 587 |
+
msg = f"'{symbol}' not supported between instances of 'NaTType' and '{other_type}'"
|
| 588 |
+
with pytest.raises(TypeError, match=msg):
|
| 589 |
+
op(NaT, other)
|
| 590 |
+
|
| 591 |
+
msg = f"'{symbol}' not supported between instances of '{other_type}' and 'NaTType'"
|
| 592 |
+
with pytest.raises(TypeError, match=msg):
|
| 593 |
+
op(other, NaT)
|
| 594 |
+
|
| 595 |
+
|
| 596 |
+
@pytest.mark.parametrize(
|
| 597 |
+
"other",
|
| 598 |
+
[
|
| 599 |
+
np.array(["foo"] * 2, dtype=object),
|
| 600 |
+
np.array([2, 3], dtype="int64"),
|
| 601 |
+
np.array([2.0, 3.5], dtype="float64"),
|
| 602 |
+
],
|
| 603 |
+
ids=["str", "int", "float"],
|
| 604 |
+
)
|
| 605 |
+
def test_nat_comparisons_invalid_ndarray(other):
|
| 606 |
+
# GH#40722
|
| 607 |
+
expected = np.array([False, False])
|
| 608 |
+
result = NaT == other
|
| 609 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 610 |
+
result = other == NaT
|
| 611 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 612 |
+
|
| 613 |
+
expected = np.array([True, True])
|
| 614 |
+
result = NaT != other
|
| 615 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 616 |
+
result = other != NaT
|
| 617 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 618 |
+
|
| 619 |
+
for symbol, op in [
|
| 620 |
+
("<=", operator.le),
|
| 621 |
+
("<", operator.lt),
|
| 622 |
+
(">=", operator.ge),
|
| 623 |
+
(">", operator.gt),
|
| 624 |
+
]:
|
| 625 |
+
msg = f"'{symbol}' not supported between"
|
| 626 |
+
|
| 627 |
+
with pytest.raises(TypeError, match=msg):
|
| 628 |
+
op(NaT, other)
|
| 629 |
+
|
| 630 |
+
if other.dtype == np.dtype("object"):
|
| 631 |
+
# uses the reverse operator, so symbol changes
|
| 632 |
+
msg = None
|
| 633 |
+
with pytest.raises(TypeError, match=msg):
|
| 634 |
+
op(other, NaT)
|
| 635 |
+
|
| 636 |
+
|
| 637 |
+
def test_compare_date(fixed_now_ts):
|
| 638 |
+
# GH#39151 comparing NaT with date object is deprecated
|
| 639 |
+
# See also: tests.scalar.timestamps.test_comparisons::test_compare_date
|
| 640 |
+
|
| 641 |
+
dt = fixed_now_ts.to_pydatetime().date()
|
| 642 |
+
|
| 643 |
+
msg = "Cannot compare NaT with datetime.date object"
|
| 644 |
+
for left, right in [(NaT, dt), (dt, NaT)]:
|
| 645 |
+
assert not left == right
|
| 646 |
+
assert left != right
|
| 647 |
+
|
| 648 |
+
with pytest.raises(TypeError, match=msg):
|
| 649 |
+
left < right
|
| 650 |
+
with pytest.raises(TypeError, match=msg):
|
| 651 |
+
left <= right
|
| 652 |
+
with pytest.raises(TypeError, match=msg):
|
| 653 |
+
left > right
|
| 654 |
+
with pytest.raises(TypeError, match=msg):
|
| 655 |
+
left >= right
|
| 656 |
+
|
| 657 |
+
|
| 658 |
+
@pytest.mark.parametrize(
|
| 659 |
+
"obj",
|
| 660 |
+
[
|
| 661 |
+
offsets.YearEnd(2),
|
| 662 |
+
offsets.YearBegin(2),
|
| 663 |
+
offsets.MonthBegin(1),
|
| 664 |
+
offsets.MonthEnd(2),
|
| 665 |
+
offsets.MonthEnd(12),
|
| 666 |
+
offsets.Day(2),
|
| 667 |
+
offsets.Day(5),
|
| 668 |
+
offsets.Hour(24),
|
| 669 |
+
offsets.Hour(3),
|
| 670 |
+
offsets.Minute(),
|
| 671 |
+
np.timedelta64(3, "h"),
|
| 672 |
+
np.timedelta64(4, "h"),
|
| 673 |
+
np.timedelta64(3200, "s"),
|
| 674 |
+
np.timedelta64(3600, "s"),
|
| 675 |
+
np.timedelta64(3600 * 24, "s"),
|
| 676 |
+
np.timedelta64(2, "D"),
|
| 677 |
+
np.timedelta64(365, "D"),
|
| 678 |
+
timedelta(-2),
|
| 679 |
+
timedelta(365),
|
| 680 |
+
timedelta(minutes=120),
|
| 681 |
+
timedelta(days=4, minutes=180),
|
| 682 |
+
timedelta(hours=23),
|
| 683 |
+
timedelta(hours=23, minutes=30),
|
| 684 |
+
timedelta(hours=48),
|
| 685 |
+
],
|
| 686 |
+
)
|
| 687 |
+
def test_nat_addsub_tdlike_scalar(obj):
|
| 688 |
+
assert NaT + obj is NaT
|
| 689 |
+
assert obj + NaT is NaT
|
| 690 |
+
assert NaT - obj is NaT
|
| 691 |
+
|
| 692 |
+
|
| 693 |
+
def test_pickle():
|
| 694 |
+
# GH#4606
|
| 695 |
+
p = tm.round_trip_pickle(NaT)
|
| 696 |
+
assert p is NaT
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (185 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
|
Binary file (36.6 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (13.2 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_formats.cpython-310.pyc
ADDED
|
Binary file (1.25 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/__pycache__/test_timedelta.cpython-310.pyc
ADDED
|
Binary file (27.4 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/test_arithmetic.py
ADDED
|
@@ -0,0 +1,1181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for scalar Timedelta arithmetic ops
|
| 3 |
+
"""
|
| 4 |
+
from datetime import (
|
| 5 |
+
datetime,
|
| 6 |
+
timedelta,
|
| 7 |
+
)
|
| 8 |
+
import operator
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pytest
|
| 12 |
+
|
| 13 |
+
from pandas.errors import OutOfBoundsTimedelta
|
| 14 |
+
|
| 15 |
+
import pandas as pd
|
| 16 |
+
from pandas import (
|
| 17 |
+
NaT,
|
| 18 |
+
Timedelta,
|
| 19 |
+
Timestamp,
|
| 20 |
+
offsets,
|
| 21 |
+
)
|
| 22 |
+
import pandas._testing as tm
|
| 23 |
+
from pandas.core import ops
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class TestTimedeltaAdditionSubtraction:
|
| 27 |
+
"""
|
| 28 |
+
Tests for Timedelta methods:
|
| 29 |
+
|
| 30 |
+
__add__, __radd__,
|
| 31 |
+
__sub__, __rsub__
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
@pytest.mark.parametrize(
|
| 35 |
+
"ten_seconds",
|
| 36 |
+
[
|
| 37 |
+
Timedelta(10, unit="s"),
|
| 38 |
+
timedelta(seconds=10),
|
| 39 |
+
np.timedelta64(10, "s"),
|
| 40 |
+
np.timedelta64(10000000000, "ns"),
|
| 41 |
+
offsets.Second(10),
|
| 42 |
+
],
|
| 43 |
+
)
|
| 44 |
+
def test_td_add_sub_ten_seconds(self, ten_seconds):
|
| 45 |
+
# GH#6808
|
| 46 |
+
base = Timestamp("20130101 09:01:12.123456")
|
| 47 |
+
expected_add = Timestamp("20130101 09:01:22.123456")
|
| 48 |
+
expected_sub = Timestamp("20130101 09:01:02.123456")
|
| 49 |
+
|
| 50 |
+
result = base + ten_seconds
|
| 51 |
+
assert result == expected_add
|
| 52 |
+
|
| 53 |
+
result = base - ten_seconds
|
| 54 |
+
assert result == expected_sub
|
| 55 |
+
|
| 56 |
+
@pytest.mark.parametrize(
|
| 57 |
+
"one_day_ten_secs",
|
| 58 |
+
[
|
| 59 |
+
Timedelta("1 day, 00:00:10"),
|
| 60 |
+
Timedelta("1 days, 00:00:10"),
|
| 61 |
+
timedelta(days=1, seconds=10),
|
| 62 |
+
np.timedelta64(1, "D") + np.timedelta64(10, "s"),
|
| 63 |
+
offsets.Day() + offsets.Second(10),
|
| 64 |
+
],
|
| 65 |
+
)
|
| 66 |
+
def test_td_add_sub_one_day_ten_seconds(self, one_day_ten_secs):
|
| 67 |
+
# GH#6808
|
| 68 |
+
base = Timestamp("20130102 09:01:12.123456")
|
| 69 |
+
expected_add = Timestamp("20130103 09:01:22.123456")
|
| 70 |
+
expected_sub = Timestamp("20130101 09:01:02.123456")
|
| 71 |
+
|
| 72 |
+
result = base + one_day_ten_secs
|
| 73 |
+
assert result == expected_add
|
| 74 |
+
|
| 75 |
+
result = base - one_day_ten_secs
|
| 76 |
+
assert result == expected_sub
|
| 77 |
+
|
| 78 |
+
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
| 79 |
+
def test_td_add_datetimelike_scalar(self, op):
|
| 80 |
+
# GH#19738
|
| 81 |
+
td = Timedelta(10, unit="d")
|
| 82 |
+
|
| 83 |
+
result = op(td, datetime(2016, 1, 1))
|
| 84 |
+
if op is operator.add:
|
| 85 |
+
# datetime + Timedelta does _not_ call Timedelta.__radd__,
|
| 86 |
+
# so we get a datetime back instead of a Timestamp
|
| 87 |
+
assert isinstance(result, Timestamp)
|
| 88 |
+
assert result == Timestamp(2016, 1, 11)
|
| 89 |
+
|
| 90 |
+
result = op(td, Timestamp("2018-01-12 18:09"))
|
| 91 |
+
assert isinstance(result, Timestamp)
|
| 92 |
+
assert result == Timestamp("2018-01-22 18:09")
|
| 93 |
+
|
| 94 |
+
result = op(td, np.datetime64("2018-01-12"))
|
| 95 |
+
assert isinstance(result, Timestamp)
|
| 96 |
+
assert result == Timestamp("2018-01-22")
|
| 97 |
+
|
| 98 |
+
result = op(td, NaT)
|
| 99 |
+
assert result is NaT
|
| 100 |
+
|
| 101 |
+
def test_td_add_timestamp_overflow(self):
|
| 102 |
+
ts = Timestamp("1700-01-01").as_unit("ns")
|
| 103 |
+
msg = "Cannot cast 259987 from D to 'ns' without overflow."
|
| 104 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 105 |
+
ts + Timedelta(13 * 19999, unit="D")
|
| 106 |
+
|
| 107 |
+
msg = "Cannot cast 259987 days 00:00:00 to unit='ns' without overflow"
|
| 108 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 109 |
+
ts + timedelta(days=13 * 19999)
|
| 110 |
+
|
| 111 |
+
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
| 112 |
+
def test_td_add_td(self, op):
|
| 113 |
+
td = Timedelta(10, unit="d")
|
| 114 |
+
|
| 115 |
+
result = op(td, Timedelta(days=10))
|
| 116 |
+
assert isinstance(result, Timedelta)
|
| 117 |
+
assert result == Timedelta(days=20)
|
| 118 |
+
|
| 119 |
+
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
| 120 |
+
def test_td_add_pytimedelta(self, op):
|
| 121 |
+
td = Timedelta(10, unit="d")
|
| 122 |
+
result = op(td, timedelta(days=9))
|
| 123 |
+
assert isinstance(result, Timedelta)
|
| 124 |
+
assert result == Timedelta(days=19)
|
| 125 |
+
|
| 126 |
+
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
| 127 |
+
def test_td_add_timedelta64(self, op):
|
| 128 |
+
td = Timedelta(10, unit="d")
|
| 129 |
+
result = op(td, np.timedelta64(-4, "D"))
|
| 130 |
+
assert isinstance(result, Timedelta)
|
| 131 |
+
assert result == Timedelta(days=6)
|
| 132 |
+
|
| 133 |
+
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
| 134 |
+
def test_td_add_offset(self, op):
|
| 135 |
+
td = Timedelta(10, unit="d")
|
| 136 |
+
|
| 137 |
+
result = op(td, offsets.Hour(6))
|
| 138 |
+
assert isinstance(result, Timedelta)
|
| 139 |
+
assert result == Timedelta(days=10, hours=6)
|
| 140 |
+
|
| 141 |
+
def test_td_sub_td(self):
|
| 142 |
+
td = Timedelta(10, unit="d")
|
| 143 |
+
expected = Timedelta(0, unit="ns")
|
| 144 |
+
result = td - td
|
| 145 |
+
assert isinstance(result, Timedelta)
|
| 146 |
+
assert result == expected
|
| 147 |
+
|
| 148 |
+
def test_td_sub_pytimedelta(self):
|
| 149 |
+
td = Timedelta(10, unit="d")
|
| 150 |
+
expected = Timedelta(0, unit="ns")
|
| 151 |
+
|
| 152 |
+
result = td - td.to_pytimedelta()
|
| 153 |
+
assert isinstance(result, Timedelta)
|
| 154 |
+
assert result == expected
|
| 155 |
+
|
| 156 |
+
result = td.to_pytimedelta() - td
|
| 157 |
+
assert isinstance(result, Timedelta)
|
| 158 |
+
assert result == expected
|
| 159 |
+
|
| 160 |
+
def test_td_sub_timedelta64(self):
|
| 161 |
+
td = Timedelta(10, unit="d")
|
| 162 |
+
expected = Timedelta(0, unit="ns")
|
| 163 |
+
|
| 164 |
+
result = td - td.to_timedelta64()
|
| 165 |
+
assert isinstance(result, Timedelta)
|
| 166 |
+
assert result == expected
|
| 167 |
+
|
| 168 |
+
result = td.to_timedelta64() - td
|
| 169 |
+
assert isinstance(result, Timedelta)
|
| 170 |
+
assert result == expected
|
| 171 |
+
|
| 172 |
+
def test_td_sub_nat(self):
|
| 173 |
+
# In this context pd.NaT is treated as timedelta-like
|
| 174 |
+
td = Timedelta(10, unit="d")
|
| 175 |
+
result = td - NaT
|
| 176 |
+
assert result is NaT
|
| 177 |
+
|
| 178 |
+
def test_td_sub_td64_nat(self):
|
| 179 |
+
td = Timedelta(10, unit="d")
|
| 180 |
+
td_nat = np.timedelta64("NaT")
|
| 181 |
+
|
| 182 |
+
result = td - td_nat
|
| 183 |
+
assert result is NaT
|
| 184 |
+
|
| 185 |
+
result = td_nat - td
|
| 186 |
+
assert result is NaT
|
| 187 |
+
|
| 188 |
+
def test_td_sub_offset(self):
|
| 189 |
+
td = Timedelta(10, unit="d")
|
| 190 |
+
result = td - offsets.Hour(1)
|
| 191 |
+
assert isinstance(result, Timedelta)
|
| 192 |
+
assert result == Timedelta(239, unit="h")
|
| 193 |
+
|
| 194 |
+
def test_td_add_sub_numeric_raises(self):
|
| 195 |
+
td = Timedelta(10, unit="d")
|
| 196 |
+
msg = "unsupported operand type"
|
| 197 |
+
for other in [2, 2.0, np.int64(2), np.float64(2)]:
|
| 198 |
+
with pytest.raises(TypeError, match=msg):
|
| 199 |
+
td + other
|
| 200 |
+
with pytest.raises(TypeError, match=msg):
|
| 201 |
+
other + td
|
| 202 |
+
with pytest.raises(TypeError, match=msg):
|
| 203 |
+
td - other
|
| 204 |
+
with pytest.raises(TypeError, match=msg):
|
| 205 |
+
other - td
|
| 206 |
+
|
| 207 |
+
def test_td_add_sub_int_ndarray(self):
|
| 208 |
+
td = Timedelta("1 day")
|
| 209 |
+
other = np.array([1])
|
| 210 |
+
|
| 211 |
+
msg = r"unsupported operand type\(s\) for \+: 'Timedelta' and 'int'"
|
| 212 |
+
with pytest.raises(TypeError, match=msg):
|
| 213 |
+
td + np.array([1])
|
| 214 |
+
|
| 215 |
+
msg = "|".join(
|
| 216 |
+
[
|
| 217 |
+
(
|
| 218 |
+
r"unsupported operand type\(s\) for \+: 'numpy.ndarray' "
|
| 219 |
+
"and 'Timedelta'"
|
| 220 |
+
),
|
| 221 |
+
# This message goes on to say "Please do not rely on this error;
|
| 222 |
+
# it may not be given on all Python implementations"
|
| 223 |
+
"Concatenation operation is not implemented for NumPy arrays",
|
| 224 |
+
]
|
| 225 |
+
)
|
| 226 |
+
with pytest.raises(TypeError, match=msg):
|
| 227 |
+
other + td
|
| 228 |
+
msg = r"unsupported operand type\(s\) for -: 'Timedelta' and 'int'"
|
| 229 |
+
with pytest.raises(TypeError, match=msg):
|
| 230 |
+
td - other
|
| 231 |
+
msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timedelta'"
|
| 232 |
+
with pytest.raises(TypeError, match=msg):
|
| 233 |
+
other - td
|
| 234 |
+
|
| 235 |
+
def test_td_rsub_nat(self):
|
| 236 |
+
td = Timedelta(10, unit="d")
|
| 237 |
+
result = NaT - td
|
| 238 |
+
assert result is NaT
|
| 239 |
+
|
| 240 |
+
result = np.datetime64("NaT") - td
|
| 241 |
+
assert result is NaT
|
| 242 |
+
|
| 243 |
+
def test_td_rsub_offset(self):
|
| 244 |
+
result = offsets.Hour(1) - Timedelta(10, unit="d")
|
| 245 |
+
assert isinstance(result, Timedelta)
|
| 246 |
+
assert result == Timedelta(-239, unit="h")
|
| 247 |
+
|
| 248 |
+
def test_td_sub_timedeltalike_object_dtype_array(self):
|
| 249 |
+
# GH#21980
|
| 250 |
+
arr = np.array([Timestamp("20130101 9:01"), Timestamp("20121230 9:02")])
|
| 251 |
+
exp = np.array([Timestamp("20121231 9:01"), Timestamp("20121229 9:02")])
|
| 252 |
+
res = arr - Timedelta("1D")
|
| 253 |
+
tm.assert_numpy_array_equal(res, exp)
|
| 254 |
+
|
| 255 |
+
def test_td_sub_mixed_most_timedeltalike_object_dtype_array(self):
|
| 256 |
+
# GH#21980
|
| 257 |
+
now = Timestamp("2021-11-09 09:54:00")
|
| 258 |
+
arr = np.array([now, Timedelta("1D"), np.timedelta64(2, "h")])
|
| 259 |
+
exp = np.array(
|
| 260 |
+
[
|
| 261 |
+
now - Timedelta("1D"),
|
| 262 |
+
Timedelta("0D"),
|
| 263 |
+
np.timedelta64(2, "h") - Timedelta("1D"),
|
| 264 |
+
]
|
| 265 |
+
)
|
| 266 |
+
res = arr - Timedelta("1D")
|
| 267 |
+
tm.assert_numpy_array_equal(res, exp)
|
| 268 |
+
|
| 269 |
+
def test_td_rsub_mixed_most_timedeltalike_object_dtype_array(self):
|
| 270 |
+
# GH#21980
|
| 271 |
+
now = Timestamp("2021-11-09 09:54:00")
|
| 272 |
+
arr = np.array([now, Timedelta("1D"), np.timedelta64(2, "h")])
|
| 273 |
+
msg = r"unsupported operand type\(s\) for \-: 'Timedelta' and 'Timestamp'"
|
| 274 |
+
with pytest.raises(TypeError, match=msg):
|
| 275 |
+
Timedelta("1D") - arr
|
| 276 |
+
|
| 277 |
+
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
| 278 |
+
def test_td_add_timedeltalike_object_dtype_array(self, op):
|
| 279 |
+
# GH#21980
|
| 280 |
+
arr = np.array([Timestamp("20130101 9:01"), Timestamp("20121230 9:02")])
|
| 281 |
+
exp = np.array([Timestamp("20130102 9:01"), Timestamp("20121231 9:02")])
|
| 282 |
+
res = op(arr, Timedelta("1D"))
|
| 283 |
+
tm.assert_numpy_array_equal(res, exp)
|
| 284 |
+
|
| 285 |
+
@pytest.mark.parametrize("op", [operator.add, ops.radd])
|
| 286 |
+
def test_td_add_mixed_timedeltalike_object_dtype_array(self, op):
|
| 287 |
+
# GH#21980
|
| 288 |
+
now = Timestamp("2021-11-09 09:54:00")
|
| 289 |
+
arr = np.array([now, Timedelta("1D")])
|
| 290 |
+
exp = np.array([now + Timedelta("1D"), Timedelta("2D")])
|
| 291 |
+
res = op(arr, Timedelta("1D"))
|
| 292 |
+
tm.assert_numpy_array_equal(res, exp)
|
| 293 |
+
|
| 294 |
+
def test_td_add_sub_td64_ndarray(self):
|
| 295 |
+
td = Timedelta("1 day")
|
| 296 |
+
|
| 297 |
+
other = np.array([td.to_timedelta64()])
|
| 298 |
+
expected = np.array([Timedelta("2 Days").to_timedelta64()])
|
| 299 |
+
|
| 300 |
+
result = td + other
|
| 301 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 302 |
+
result = other + td
|
| 303 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 304 |
+
|
| 305 |
+
result = td - other
|
| 306 |
+
tm.assert_numpy_array_equal(result, expected * 0)
|
| 307 |
+
result = other - td
|
| 308 |
+
tm.assert_numpy_array_equal(result, expected * 0)
|
| 309 |
+
|
| 310 |
+
def test_td_add_sub_dt64_ndarray(self):
|
| 311 |
+
td = Timedelta("1 day")
|
| 312 |
+
other = pd.to_datetime(["2000-01-01"]).values
|
| 313 |
+
|
| 314 |
+
expected = pd.to_datetime(["2000-01-02"]).values
|
| 315 |
+
tm.assert_numpy_array_equal(td + other, expected)
|
| 316 |
+
tm.assert_numpy_array_equal(other + td, expected)
|
| 317 |
+
|
| 318 |
+
expected = pd.to_datetime(["1999-12-31"]).values
|
| 319 |
+
tm.assert_numpy_array_equal(-td + other, expected)
|
| 320 |
+
tm.assert_numpy_array_equal(other - td, expected)
|
| 321 |
+
|
| 322 |
+
def test_td_add_sub_ndarray_0d(self):
|
| 323 |
+
td = Timedelta("1 day")
|
| 324 |
+
other = np.array(td.asm8)
|
| 325 |
+
|
| 326 |
+
result = td + other
|
| 327 |
+
assert isinstance(result, Timedelta)
|
| 328 |
+
assert result == 2 * td
|
| 329 |
+
|
| 330 |
+
result = other + td
|
| 331 |
+
assert isinstance(result, Timedelta)
|
| 332 |
+
assert result == 2 * td
|
| 333 |
+
|
| 334 |
+
result = other - td
|
| 335 |
+
assert isinstance(result, Timedelta)
|
| 336 |
+
assert result == 0 * td
|
| 337 |
+
|
| 338 |
+
result = td - other
|
| 339 |
+
assert isinstance(result, Timedelta)
|
| 340 |
+
assert result == 0 * td
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
class TestTimedeltaMultiplicationDivision:
|
| 344 |
+
"""
|
| 345 |
+
Tests for Timedelta methods:
|
| 346 |
+
|
| 347 |
+
__mul__, __rmul__,
|
| 348 |
+
__div__, __rdiv__,
|
| 349 |
+
__truediv__, __rtruediv__,
|
| 350 |
+
__floordiv__, __rfloordiv__,
|
| 351 |
+
__mod__, __rmod__,
|
| 352 |
+
__divmod__, __rdivmod__
|
| 353 |
+
"""
|
| 354 |
+
|
| 355 |
+
# ---------------------------------------------------------------
|
| 356 |
+
# Timedelta.__mul__, __rmul__
|
| 357 |
+
|
| 358 |
+
@pytest.mark.parametrize(
|
| 359 |
+
"td_nat", [NaT, np.timedelta64("NaT", "ns"), np.timedelta64("NaT")]
|
| 360 |
+
)
|
| 361 |
+
@pytest.mark.parametrize("op", [operator.mul, ops.rmul])
|
| 362 |
+
def test_td_mul_nat(self, op, td_nat):
|
| 363 |
+
# GH#19819
|
| 364 |
+
td = Timedelta(10, unit="d")
|
| 365 |
+
typs = "|".join(["numpy.timedelta64", "NaTType", "Timedelta"])
|
| 366 |
+
msg = "|".join(
|
| 367 |
+
[
|
| 368 |
+
rf"unsupported operand type\(s\) for \*: '{typs}' and '{typs}'",
|
| 369 |
+
r"ufunc '?multiply'? cannot use operands with types",
|
| 370 |
+
]
|
| 371 |
+
)
|
| 372 |
+
with pytest.raises(TypeError, match=msg):
|
| 373 |
+
op(td, td_nat)
|
| 374 |
+
|
| 375 |
+
@pytest.mark.parametrize("nan", [np.nan, np.float64("NaN"), float("nan")])
|
| 376 |
+
@pytest.mark.parametrize("op", [operator.mul, ops.rmul])
|
| 377 |
+
def test_td_mul_nan(self, op, nan):
|
| 378 |
+
# np.float64('NaN') has a 'dtype' attr, avoid treating as array
|
| 379 |
+
td = Timedelta(10, unit="d")
|
| 380 |
+
result = op(td, nan)
|
| 381 |
+
assert result is NaT
|
| 382 |
+
|
| 383 |
+
@pytest.mark.parametrize("op", [operator.mul, ops.rmul])
|
| 384 |
+
def test_td_mul_scalar(self, op):
|
| 385 |
+
# GH#19738
|
| 386 |
+
td = Timedelta(minutes=3)
|
| 387 |
+
|
| 388 |
+
result = op(td, 2)
|
| 389 |
+
assert result == Timedelta(minutes=6)
|
| 390 |
+
|
| 391 |
+
result = op(td, 1.5)
|
| 392 |
+
assert result == Timedelta(minutes=4, seconds=30)
|
| 393 |
+
|
| 394 |
+
assert op(td, np.nan) is NaT
|
| 395 |
+
|
| 396 |
+
assert op(-1, td)._value == -1 * td._value
|
| 397 |
+
assert op(-1.0, td)._value == -1.0 * td._value
|
| 398 |
+
|
| 399 |
+
msg = "unsupported operand type"
|
| 400 |
+
with pytest.raises(TypeError, match=msg):
|
| 401 |
+
# timedelta * datetime is gibberish
|
| 402 |
+
op(td, Timestamp(2016, 1, 2))
|
| 403 |
+
|
| 404 |
+
with pytest.raises(TypeError, match=msg):
|
| 405 |
+
# invalid multiply with another timedelta
|
| 406 |
+
op(td, td)
|
| 407 |
+
|
| 408 |
+
def test_td_mul_numeric_ndarray(self):
|
| 409 |
+
td = Timedelta("1 day")
|
| 410 |
+
other = np.array([2])
|
| 411 |
+
expected = np.array([Timedelta("2 Days").to_timedelta64()])
|
| 412 |
+
|
| 413 |
+
result = td * other
|
| 414 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 415 |
+
|
| 416 |
+
result = other * td
|
| 417 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 418 |
+
|
| 419 |
+
def test_td_mul_numeric_ndarray_0d(self):
|
| 420 |
+
td = Timedelta("1 day")
|
| 421 |
+
other = np.array(2)
|
| 422 |
+
assert other.ndim == 0
|
| 423 |
+
expected = Timedelta("2 days")
|
| 424 |
+
|
| 425 |
+
res = td * other
|
| 426 |
+
assert type(res) is Timedelta
|
| 427 |
+
assert res == expected
|
| 428 |
+
|
| 429 |
+
res = other * td
|
| 430 |
+
assert type(res) is Timedelta
|
| 431 |
+
assert res == expected
|
| 432 |
+
|
| 433 |
+
def test_td_mul_td64_ndarray_invalid(self):
|
| 434 |
+
td = Timedelta("1 day")
|
| 435 |
+
other = np.array([Timedelta("2 Days").to_timedelta64()])
|
| 436 |
+
|
| 437 |
+
msg = (
|
| 438 |
+
"ufunc '?multiply'? cannot use operands with types "
|
| 439 |
+
rf"dtype\('{tm.ENDIAN}m8\[ns\]'\) and dtype\('{tm.ENDIAN}m8\[ns\]'\)"
|
| 440 |
+
)
|
| 441 |
+
with pytest.raises(TypeError, match=msg):
|
| 442 |
+
td * other
|
| 443 |
+
with pytest.raises(TypeError, match=msg):
|
| 444 |
+
other * td
|
| 445 |
+
|
| 446 |
+
# ---------------------------------------------------------------
|
| 447 |
+
# Timedelta.__div__, __truediv__
|
| 448 |
+
|
| 449 |
+
def test_td_div_timedeltalike_scalar(self):
|
| 450 |
+
# GH#19738
|
| 451 |
+
td = Timedelta(10, unit="d")
|
| 452 |
+
|
| 453 |
+
result = td / offsets.Hour(1)
|
| 454 |
+
assert result == 240
|
| 455 |
+
|
| 456 |
+
assert td / td == 1
|
| 457 |
+
assert td / np.timedelta64(60, "h") == 4
|
| 458 |
+
|
| 459 |
+
assert np.isnan(td / NaT)
|
| 460 |
+
|
| 461 |
+
def test_td_div_td64_non_nano(self):
|
| 462 |
+
# truediv
|
| 463 |
+
td = Timedelta("1 days 2 hours 3 ns")
|
| 464 |
+
result = td / np.timedelta64(1, "D")
|
| 465 |
+
assert result == td._value / (86400 * 10**9)
|
| 466 |
+
result = td / np.timedelta64(1, "s")
|
| 467 |
+
assert result == td._value / 10**9
|
| 468 |
+
result = td / np.timedelta64(1, "ns")
|
| 469 |
+
assert result == td._value
|
| 470 |
+
|
| 471 |
+
# floordiv
|
| 472 |
+
td = Timedelta("1 days 2 hours 3 ns")
|
| 473 |
+
result = td // np.timedelta64(1, "D")
|
| 474 |
+
assert result == 1
|
| 475 |
+
result = td // np.timedelta64(1, "s")
|
| 476 |
+
assert result == 93600
|
| 477 |
+
result = td // np.timedelta64(1, "ns")
|
| 478 |
+
assert result == td._value
|
| 479 |
+
|
| 480 |
+
def test_td_div_numeric_scalar(self):
|
| 481 |
+
# GH#19738
|
| 482 |
+
td = Timedelta(10, unit="d")
|
| 483 |
+
|
| 484 |
+
result = td / 2
|
| 485 |
+
assert isinstance(result, Timedelta)
|
| 486 |
+
assert result == Timedelta(days=5)
|
| 487 |
+
|
| 488 |
+
result = td / 5
|
| 489 |
+
assert isinstance(result, Timedelta)
|
| 490 |
+
assert result == Timedelta(days=2)
|
| 491 |
+
|
| 492 |
+
@pytest.mark.parametrize(
|
| 493 |
+
"nan",
|
| 494 |
+
[
|
| 495 |
+
np.nan,
|
| 496 |
+
np.float64("NaN"),
|
| 497 |
+
float("nan"),
|
| 498 |
+
],
|
| 499 |
+
)
|
| 500 |
+
def test_td_div_nan(self, nan):
|
| 501 |
+
# np.float64('NaN') has a 'dtype' attr, avoid treating as array
|
| 502 |
+
td = Timedelta(10, unit="d")
|
| 503 |
+
result = td / nan
|
| 504 |
+
assert result is NaT
|
| 505 |
+
|
| 506 |
+
result = td // nan
|
| 507 |
+
assert result is NaT
|
| 508 |
+
|
| 509 |
+
def test_td_div_td64_ndarray(self):
|
| 510 |
+
td = Timedelta("1 day")
|
| 511 |
+
|
| 512 |
+
other = np.array([Timedelta("2 Days").to_timedelta64()])
|
| 513 |
+
expected = np.array([0.5])
|
| 514 |
+
|
| 515 |
+
result = td / other
|
| 516 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 517 |
+
|
| 518 |
+
result = other / td
|
| 519 |
+
tm.assert_numpy_array_equal(result, expected * 4)
|
| 520 |
+
|
| 521 |
+
def test_td_div_ndarray_0d(self):
|
| 522 |
+
td = Timedelta("1 day")
|
| 523 |
+
|
| 524 |
+
other = np.array(1)
|
| 525 |
+
res = td / other
|
| 526 |
+
assert isinstance(res, Timedelta)
|
| 527 |
+
assert res == td
|
| 528 |
+
|
| 529 |
+
# ---------------------------------------------------------------
|
| 530 |
+
# Timedelta.__rdiv__
|
| 531 |
+
|
| 532 |
+
def test_td_rdiv_timedeltalike_scalar(self):
|
| 533 |
+
# GH#19738
|
| 534 |
+
td = Timedelta(10, unit="d")
|
| 535 |
+
result = offsets.Hour(1) / td
|
| 536 |
+
assert result == 1 / 240.0
|
| 537 |
+
|
| 538 |
+
assert np.timedelta64(60, "h") / td == 0.25
|
| 539 |
+
|
| 540 |
+
def test_td_rdiv_na_scalar(self):
|
| 541 |
+
# GH#31869 None gets cast to NaT
|
| 542 |
+
td = Timedelta(10, unit="d")
|
| 543 |
+
|
| 544 |
+
result = NaT / td
|
| 545 |
+
assert np.isnan(result)
|
| 546 |
+
|
| 547 |
+
result = None / td
|
| 548 |
+
assert np.isnan(result)
|
| 549 |
+
|
| 550 |
+
result = np.timedelta64("NaT") / td
|
| 551 |
+
assert np.isnan(result)
|
| 552 |
+
|
| 553 |
+
msg = r"unsupported operand type\(s\) for /: 'numpy.datetime64' and 'Timedelta'"
|
| 554 |
+
with pytest.raises(TypeError, match=msg):
|
| 555 |
+
np.datetime64("NaT") / td
|
| 556 |
+
|
| 557 |
+
msg = r"unsupported operand type\(s\) for /: 'float' and 'Timedelta'"
|
| 558 |
+
with pytest.raises(TypeError, match=msg):
|
| 559 |
+
np.nan / td
|
| 560 |
+
|
| 561 |
+
def test_td_rdiv_ndarray(self):
|
| 562 |
+
td = Timedelta(10, unit="d")
|
| 563 |
+
|
| 564 |
+
arr = np.array([td], dtype=object)
|
| 565 |
+
result = arr / td
|
| 566 |
+
expected = np.array([1], dtype=np.float64)
|
| 567 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 568 |
+
|
| 569 |
+
arr = np.array([None])
|
| 570 |
+
result = arr / td
|
| 571 |
+
expected = np.array([np.nan])
|
| 572 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 573 |
+
|
| 574 |
+
arr = np.array([np.nan], dtype=object)
|
| 575 |
+
msg = r"unsupported operand type\(s\) for /: 'float' and 'Timedelta'"
|
| 576 |
+
with pytest.raises(TypeError, match=msg):
|
| 577 |
+
arr / td
|
| 578 |
+
|
| 579 |
+
arr = np.array([np.nan], dtype=np.float64)
|
| 580 |
+
msg = "cannot use operands with types dtype"
|
| 581 |
+
with pytest.raises(TypeError, match=msg):
|
| 582 |
+
arr / td
|
| 583 |
+
|
| 584 |
+
def test_td_rdiv_ndarray_0d(self):
|
| 585 |
+
td = Timedelta(10, unit="d")
|
| 586 |
+
|
| 587 |
+
arr = np.array(td.asm8)
|
| 588 |
+
|
| 589 |
+
assert arr / td == 1
|
| 590 |
+
|
| 591 |
+
# ---------------------------------------------------------------
|
| 592 |
+
# Timedelta.__floordiv__
|
| 593 |
+
|
| 594 |
+
def test_td_floordiv_timedeltalike_scalar(self):
|
| 595 |
+
# GH#18846
|
| 596 |
+
td = Timedelta(hours=3, minutes=4)
|
| 597 |
+
scalar = Timedelta(hours=3, minutes=3)
|
| 598 |
+
|
| 599 |
+
assert td // scalar == 1
|
| 600 |
+
assert -td // scalar.to_pytimedelta() == -2
|
| 601 |
+
assert (2 * td) // scalar.to_timedelta64() == 2
|
| 602 |
+
|
| 603 |
+
def test_td_floordiv_null_scalar(self):
|
| 604 |
+
# GH#18846
|
| 605 |
+
td = Timedelta(hours=3, minutes=4)
|
| 606 |
+
|
| 607 |
+
assert td // np.nan is NaT
|
| 608 |
+
assert np.isnan(td // NaT)
|
| 609 |
+
assert np.isnan(td // np.timedelta64("NaT"))
|
| 610 |
+
|
| 611 |
+
def test_td_floordiv_offsets(self):
|
| 612 |
+
# GH#19738
|
| 613 |
+
td = Timedelta(hours=3, minutes=4)
|
| 614 |
+
assert td // offsets.Hour(1) == 3
|
| 615 |
+
assert td // offsets.Minute(2) == 92
|
| 616 |
+
|
| 617 |
+
def test_td_floordiv_invalid_scalar(self):
|
| 618 |
+
# GH#18846
|
| 619 |
+
td = Timedelta(hours=3, minutes=4)
|
| 620 |
+
|
| 621 |
+
msg = "|".join(
|
| 622 |
+
[
|
| 623 |
+
r"Invalid dtype datetime64\[D\] for __floordiv__",
|
| 624 |
+
"'dtype' is an invalid keyword argument for this function",
|
| 625 |
+
r"ufunc '?floor_divide'? cannot use operands with types",
|
| 626 |
+
]
|
| 627 |
+
)
|
| 628 |
+
with pytest.raises(TypeError, match=msg):
|
| 629 |
+
td // np.datetime64("2016-01-01", dtype="datetime64[us]")
|
| 630 |
+
|
| 631 |
+
def test_td_floordiv_numeric_scalar(self):
|
| 632 |
+
# GH#18846
|
| 633 |
+
td = Timedelta(hours=3, minutes=4)
|
| 634 |
+
|
| 635 |
+
expected = Timedelta(hours=1, minutes=32)
|
| 636 |
+
assert td // 2 == expected
|
| 637 |
+
assert td // 2.0 == expected
|
| 638 |
+
assert td // np.float64(2.0) == expected
|
| 639 |
+
assert td // np.int32(2.0) == expected
|
| 640 |
+
assert td // np.uint8(2.0) == expected
|
| 641 |
+
|
| 642 |
+
def test_td_floordiv_timedeltalike_array(self):
|
| 643 |
+
# GH#18846
|
| 644 |
+
td = Timedelta(hours=3, minutes=4)
|
| 645 |
+
scalar = Timedelta(hours=3, minutes=3)
|
| 646 |
+
|
| 647 |
+
# Array-like others
|
| 648 |
+
assert td // np.array(scalar.to_timedelta64()) == 1
|
| 649 |
+
|
| 650 |
+
res = (3 * td) // np.array([scalar.to_timedelta64()])
|
| 651 |
+
expected = np.array([3], dtype=np.int64)
|
| 652 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 653 |
+
|
| 654 |
+
res = (10 * td) // np.array([scalar.to_timedelta64(), np.timedelta64("NaT")])
|
| 655 |
+
expected = np.array([10, np.nan])
|
| 656 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 657 |
+
|
| 658 |
+
def test_td_floordiv_numeric_series(self):
|
| 659 |
+
# GH#18846
|
| 660 |
+
td = Timedelta(hours=3, minutes=4)
|
| 661 |
+
ser = pd.Series([1], dtype=np.int64)
|
| 662 |
+
res = td // ser
|
| 663 |
+
assert res.dtype.kind == "m"
|
| 664 |
+
|
| 665 |
+
# ---------------------------------------------------------------
|
| 666 |
+
# Timedelta.__rfloordiv__
|
| 667 |
+
|
| 668 |
+
def test_td_rfloordiv_timedeltalike_scalar(self):
|
| 669 |
+
# GH#18846
|
| 670 |
+
td = Timedelta(hours=3, minutes=3)
|
| 671 |
+
scalar = Timedelta(hours=3, minutes=4)
|
| 672 |
+
|
| 673 |
+
# scalar others
|
| 674 |
+
# x // Timedelta is defined only for timedelta-like x. int-like,
|
| 675 |
+
# float-like, and date-like, in particular, should all either
|
| 676 |
+
# a) raise TypeError directly or
|
| 677 |
+
# b) return NotImplemented, following which the reversed
|
| 678 |
+
# operation will raise TypeError.
|
| 679 |
+
assert td.__rfloordiv__(scalar) == 1
|
| 680 |
+
assert (-td).__rfloordiv__(scalar.to_pytimedelta()) == -2
|
| 681 |
+
assert (2 * td).__rfloordiv__(scalar.to_timedelta64()) == 0
|
| 682 |
+
|
| 683 |
+
def test_td_rfloordiv_null_scalar(self):
|
| 684 |
+
# GH#18846
|
| 685 |
+
td = Timedelta(hours=3, minutes=3)
|
| 686 |
+
|
| 687 |
+
assert np.isnan(td.__rfloordiv__(NaT))
|
| 688 |
+
assert np.isnan(td.__rfloordiv__(np.timedelta64("NaT")))
|
| 689 |
+
|
| 690 |
+
def test_td_rfloordiv_offsets(self):
|
| 691 |
+
# GH#19738
|
| 692 |
+
assert offsets.Hour(1) // Timedelta(minutes=25) == 2
|
| 693 |
+
|
| 694 |
+
def test_td_rfloordiv_invalid_scalar(self):
|
| 695 |
+
# GH#18846
|
| 696 |
+
td = Timedelta(hours=3, minutes=3)
|
| 697 |
+
|
| 698 |
+
dt64 = np.datetime64("2016-01-01", "us")
|
| 699 |
+
|
| 700 |
+
assert td.__rfloordiv__(dt64) is NotImplemented
|
| 701 |
+
|
| 702 |
+
msg = (
|
| 703 |
+
r"unsupported operand type\(s\) for //: 'numpy.datetime64' and 'Timedelta'"
|
| 704 |
+
)
|
| 705 |
+
with pytest.raises(TypeError, match=msg):
|
| 706 |
+
dt64 // td
|
| 707 |
+
|
| 708 |
+
def test_td_rfloordiv_numeric_scalar(self):
|
| 709 |
+
# GH#18846
|
| 710 |
+
td = Timedelta(hours=3, minutes=3)
|
| 711 |
+
|
| 712 |
+
assert td.__rfloordiv__(np.nan) is NotImplemented
|
| 713 |
+
assert td.__rfloordiv__(3.5) is NotImplemented
|
| 714 |
+
assert td.__rfloordiv__(2) is NotImplemented
|
| 715 |
+
assert td.__rfloordiv__(np.float64(2.0)) is NotImplemented
|
| 716 |
+
assert td.__rfloordiv__(np.uint8(9)) is NotImplemented
|
| 717 |
+
assert td.__rfloordiv__(np.int32(2.0)) is NotImplemented
|
| 718 |
+
|
| 719 |
+
msg = r"unsupported operand type\(s\) for //: '.*' and 'Timedelta"
|
| 720 |
+
with pytest.raises(TypeError, match=msg):
|
| 721 |
+
np.float64(2.0) // td
|
| 722 |
+
with pytest.raises(TypeError, match=msg):
|
| 723 |
+
np.uint8(9) // td
|
| 724 |
+
with pytest.raises(TypeError, match=msg):
|
| 725 |
+
# deprecated GH#19761, enforced GH#29797
|
| 726 |
+
np.int32(2.0) // td
|
| 727 |
+
|
| 728 |
+
def test_td_rfloordiv_timedeltalike_array(self):
|
| 729 |
+
# GH#18846
|
| 730 |
+
td = Timedelta(hours=3, minutes=3)
|
| 731 |
+
scalar = Timedelta(hours=3, minutes=4)
|
| 732 |
+
|
| 733 |
+
# Array-like others
|
| 734 |
+
assert td.__rfloordiv__(np.array(scalar.to_timedelta64())) == 1
|
| 735 |
+
|
| 736 |
+
res = td.__rfloordiv__(np.array([(3 * scalar).to_timedelta64()]))
|
| 737 |
+
expected = np.array([3], dtype=np.int64)
|
| 738 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 739 |
+
|
| 740 |
+
arr = np.array([(10 * scalar).to_timedelta64(), np.timedelta64("NaT")])
|
| 741 |
+
res = td.__rfloordiv__(arr)
|
| 742 |
+
expected = np.array([10, np.nan])
|
| 743 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 744 |
+
|
| 745 |
+
def test_td_rfloordiv_intarray(self):
|
| 746 |
+
# deprecated GH#19761, enforced GH#29797
|
| 747 |
+
ints = np.array([1349654400, 1349740800, 1349827200, 1349913600]) * 10**9
|
| 748 |
+
|
| 749 |
+
msg = "Invalid dtype"
|
| 750 |
+
with pytest.raises(TypeError, match=msg):
|
| 751 |
+
ints // Timedelta(1, unit="s")
|
| 752 |
+
|
| 753 |
+
def test_td_rfloordiv_numeric_series(self):
|
| 754 |
+
# GH#18846
|
| 755 |
+
td = Timedelta(hours=3, minutes=3)
|
| 756 |
+
ser = pd.Series([1], dtype=np.int64)
|
| 757 |
+
res = td.__rfloordiv__(ser)
|
| 758 |
+
assert res is NotImplemented
|
| 759 |
+
|
| 760 |
+
msg = "Invalid dtype"
|
| 761 |
+
with pytest.raises(TypeError, match=msg):
|
| 762 |
+
# Deprecated GH#19761, enforced GH#29797
|
| 763 |
+
ser // td
|
| 764 |
+
|
| 765 |
+
# ----------------------------------------------------------------
|
| 766 |
+
# Timedelta.__mod__, __rmod__
|
| 767 |
+
|
| 768 |
+
def test_mod_timedeltalike(self):
|
| 769 |
+
# GH#19365
|
| 770 |
+
td = Timedelta(hours=37)
|
| 771 |
+
|
| 772 |
+
# Timedelta-like others
|
| 773 |
+
result = td % Timedelta(hours=6)
|
| 774 |
+
assert isinstance(result, Timedelta)
|
| 775 |
+
assert result == Timedelta(hours=1)
|
| 776 |
+
|
| 777 |
+
result = td % timedelta(minutes=60)
|
| 778 |
+
assert isinstance(result, Timedelta)
|
| 779 |
+
assert result == Timedelta(0)
|
| 780 |
+
|
| 781 |
+
result = td % NaT
|
| 782 |
+
assert result is NaT
|
| 783 |
+
|
| 784 |
+
def test_mod_timedelta64_nat(self):
|
| 785 |
+
# GH#19365
|
| 786 |
+
td = Timedelta(hours=37)
|
| 787 |
+
|
| 788 |
+
result = td % np.timedelta64("NaT", "ns")
|
| 789 |
+
assert result is NaT
|
| 790 |
+
|
| 791 |
+
def test_mod_timedelta64(self):
|
| 792 |
+
# GH#19365
|
| 793 |
+
td = Timedelta(hours=37)
|
| 794 |
+
|
| 795 |
+
result = td % np.timedelta64(2, "h")
|
| 796 |
+
assert isinstance(result, Timedelta)
|
| 797 |
+
assert result == Timedelta(hours=1)
|
| 798 |
+
|
| 799 |
+
def test_mod_offset(self):
|
| 800 |
+
# GH#19365
|
| 801 |
+
td = Timedelta(hours=37)
|
| 802 |
+
|
| 803 |
+
result = td % offsets.Hour(5)
|
| 804 |
+
assert isinstance(result, Timedelta)
|
| 805 |
+
assert result == Timedelta(hours=2)
|
| 806 |
+
|
| 807 |
+
def test_mod_numeric(self):
|
| 808 |
+
# GH#19365
|
| 809 |
+
td = Timedelta(hours=37)
|
| 810 |
+
|
| 811 |
+
# Numeric Others
|
| 812 |
+
result = td % 2
|
| 813 |
+
assert isinstance(result, Timedelta)
|
| 814 |
+
assert result == Timedelta(0)
|
| 815 |
+
|
| 816 |
+
result = td % 1e12
|
| 817 |
+
assert isinstance(result, Timedelta)
|
| 818 |
+
assert result == Timedelta(minutes=3, seconds=20)
|
| 819 |
+
|
| 820 |
+
result = td % int(1e12)
|
| 821 |
+
assert isinstance(result, Timedelta)
|
| 822 |
+
assert result == Timedelta(minutes=3, seconds=20)
|
| 823 |
+
|
| 824 |
+
def test_mod_invalid(self):
|
| 825 |
+
# GH#19365
|
| 826 |
+
td = Timedelta(hours=37)
|
| 827 |
+
msg = "unsupported operand type"
|
| 828 |
+
with pytest.raises(TypeError, match=msg):
|
| 829 |
+
td % Timestamp("2018-01-22")
|
| 830 |
+
|
| 831 |
+
with pytest.raises(TypeError, match=msg):
|
| 832 |
+
td % []
|
| 833 |
+
|
| 834 |
+
def test_rmod_pytimedelta(self):
|
| 835 |
+
# GH#19365
|
| 836 |
+
td = Timedelta(minutes=3)
|
| 837 |
+
|
| 838 |
+
result = timedelta(minutes=4) % td
|
| 839 |
+
assert isinstance(result, Timedelta)
|
| 840 |
+
assert result == Timedelta(minutes=1)
|
| 841 |
+
|
| 842 |
+
def test_rmod_timedelta64(self):
|
| 843 |
+
# GH#19365
|
| 844 |
+
td = Timedelta(minutes=3)
|
| 845 |
+
result = np.timedelta64(5, "m") % td
|
| 846 |
+
assert isinstance(result, Timedelta)
|
| 847 |
+
assert result == Timedelta(minutes=2)
|
| 848 |
+
|
| 849 |
+
def test_rmod_invalid(self):
|
| 850 |
+
# GH#19365
|
| 851 |
+
td = Timedelta(minutes=3)
|
| 852 |
+
|
| 853 |
+
msg = "unsupported operand"
|
| 854 |
+
with pytest.raises(TypeError, match=msg):
|
| 855 |
+
Timestamp("2018-01-22") % td
|
| 856 |
+
|
| 857 |
+
with pytest.raises(TypeError, match=msg):
|
| 858 |
+
15 % td
|
| 859 |
+
|
| 860 |
+
with pytest.raises(TypeError, match=msg):
|
| 861 |
+
16.0 % td
|
| 862 |
+
|
| 863 |
+
msg = "Invalid dtype int"
|
| 864 |
+
with pytest.raises(TypeError, match=msg):
|
| 865 |
+
np.array([22, 24]) % td
|
| 866 |
+
|
| 867 |
+
# ----------------------------------------------------------------
|
| 868 |
+
# Timedelta.__divmod__, __rdivmod__
|
| 869 |
+
|
| 870 |
+
def test_divmod_numeric(self):
|
| 871 |
+
# GH#19365
|
| 872 |
+
td = Timedelta(days=2, hours=6)
|
| 873 |
+
|
| 874 |
+
result = divmod(td, 53 * 3600 * 1e9)
|
| 875 |
+
assert result[0] == Timedelta(1, unit="ns")
|
| 876 |
+
assert isinstance(result[1], Timedelta)
|
| 877 |
+
assert result[1] == Timedelta(hours=1)
|
| 878 |
+
|
| 879 |
+
assert result
|
| 880 |
+
result = divmod(td, np.nan)
|
| 881 |
+
assert result[0] is NaT
|
| 882 |
+
assert result[1] is NaT
|
| 883 |
+
|
| 884 |
+
def test_divmod(self):
|
| 885 |
+
# GH#19365
|
| 886 |
+
td = Timedelta(days=2, hours=6)
|
| 887 |
+
|
| 888 |
+
result = divmod(td, timedelta(days=1))
|
| 889 |
+
assert result[0] == 2
|
| 890 |
+
assert isinstance(result[1], Timedelta)
|
| 891 |
+
assert result[1] == Timedelta(hours=6)
|
| 892 |
+
|
| 893 |
+
result = divmod(td, 54)
|
| 894 |
+
assert result[0] == Timedelta(hours=1)
|
| 895 |
+
assert isinstance(result[1], Timedelta)
|
| 896 |
+
assert result[1] == Timedelta(0)
|
| 897 |
+
|
| 898 |
+
result = divmod(td, NaT)
|
| 899 |
+
assert np.isnan(result[0])
|
| 900 |
+
assert result[1] is NaT
|
| 901 |
+
|
| 902 |
+
def test_divmod_offset(self):
|
| 903 |
+
# GH#19365
|
| 904 |
+
td = Timedelta(days=2, hours=6)
|
| 905 |
+
|
| 906 |
+
result = divmod(td, offsets.Hour(-4))
|
| 907 |
+
assert result[0] == -14
|
| 908 |
+
assert isinstance(result[1], Timedelta)
|
| 909 |
+
assert result[1] == Timedelta(hours=-2)
|
| 910 |
+
|
| 911 |
+
def test_divmod_invalid(self):
|
| 912 |
+
# GH#19365
|
| 913 |
+
td = Timedelta(days=2, hours=6)
|
| 914 |
+
|
| 915 |
+
msg = r"unsupported operand type\(s\) for //: 'Timedelta' and 'Timestamp'"
|
| 916 |
+
with pytest.raises(TypeError, match=msg):
|
| 917 |
+
divmod(td, Timestamp("2018-01-22"))
|
| 918 |
+
|
| 919 |
+
def test_rdivmod_pytimedelta(self):
|
| 920 |
+
# GH#19365
|
| 921 |
+
result = divmod(timedelta(days=2, hours=6), Timedelta(days=1))
|
| 922 |
+
assert result[0] == 2
|
| 923 |
+
assert isinstance(result[1], Timedelta)
|
| 924 |
+
assert result[1] == Timedelta(hours=6)
|
| 925 |
+
|
| 926 |
+
def test_rdivmod_offset(self):
|
| 927 |
+
result = divmod(offsets.Hour(54), Timedelta(hours=-4))
|
| 928 |
+
assert result[0] == -14
|
| 929 |
+
assert isinstance(result[1], Timedelta)
|
| 930 |
+
assert result[1] == Timedelta(hours=-2)
|
| 931 |
+
|
| 932 |
+
def test_rdivmod_invalid(self):
|
| 933 |
+
# GH#19365
|
| 934 |
+
td = Timedelta(minutes=3)
|
| 935 |
+
msg = "unsupported operand type"
|
| 936 |
+
|
| 937 |
+
with pytest.raises(TypeError, match=msg):
|
| 938 |
+
divmod(Timestamp("2018-01-22"), td)
|
| 939 |
+
|
| 940 |
+
with pytest.raises(TypeError, match=msg):
|
| 941 |
+
divmod(15, td)
|
| 942 |
+
|
| 943 |
+
with pytest.raises(TypeError, match=msg):
|
| 944 |
+
divmod(16.0, td)
|
| 945 |
+
|
| 946 |
+
msg = "Invalid dtype int"
|
| 947 |
+
with pytest.raises(TypeError, match=msg):
|
| 948 |
+
divmod(np.array([22, 24]), td)
|
| 949 |
+
|
| 950 |
+
# ----------------------------------------------------------------
|
| 951 |
+
|
| 952 |
+
@pytest.mark.parametrize(
|
| 953 |
+
"op", [operator.mul, ops.rmul, operator.truediv, ops.rdiv, ops.rsub]
|
| 954 |
+
)
|
| 955 |
+
@pytest.mark.parametrize(
|
| 956 |
+
"arr",
|
| 957 |
+
[
|
| 958 |
+
np.array([Timestamp("20130101 9:01"), Timestamp("20121230 9:02")]),
|
| 959 |
+
np.array([Timestamp("2021-11-09 09:54:00"), Timedelta("1D")]),
|
| 960 |
+
],
|
| 961 |
+
)
|
| 962 |
+
def test_td_op_timedelta_timedeltalike_array(self, op, arr):
|
| 963 |
+
msg = "unsupported operand type|cannot use operands with types"
|
| 964 |
+
with pytest.raises(TypeError, match=msg):
|
| 965 |
+
op(arr, Timedelta("1D"))
|
| 966 |
+
|
| 967 |
+
|
| 968 |
+
class TestTimedeltaComparison:
|
| 969 |
+
def test_compare_pytimedelta_bounds(self):
|
| 970 |
+
# GH#49021 don't overflow on comparison with very large pytimedeltas
|
| 971 |
+
|
| 972 |
+
for unit in ["ns", "us"]:
|
| 973 |
+
tdmax = Timedelta.max.as_unit(unit).max
|
| 974 |
+
tdmin = Timedelta.min.as_unit(unit).min
|
| 975 |
+
|
| 976 |
+
assert tdmax < timedelta.max
|
| 977 |
+
assert tdmax <= timedelta.max
|
| 978 |
+
assert not tdmax > timedelta.max
|
| 979 |
+
assert not tdmax >= timedelta.max
|
| 980 |
+
assert tdmax != timedelta.max
|
| 981 |
+
assert not tdmax == timedelta.max
|
| 982 |
+
|
| 983 |
+
assert tdmin > timedelta.min
|
| 984 |
+
assert tdmin >= timedelta.min
|
| 985 |
+
assert not tdmin < timedelta.min
|
| 986 |
+
assert not tdmin <= timedelta.min
|
| 987 |
+
assert tdmin != timedelta.min
|
| 988 |
+
assert not tdmin == timedelta.min
|
| 989 |
+
|
| 990 |
+
# But the "ms" and "s"-reso bounds extend pass pytimedelta
|
| 991 |
+
for unit in ["ms", "s"]:
|
| 992 |
+
tdmax = Timedelta.max.as_unit(unit).max
|
| 993 |
+
tdmin = Timedelta.min.as_unit(unit).min
|
| 994 |
+
|
| 995 |
+
assert tdmax > timedelta.max
|
| 996 |
+
assert tdmax >= timedelta.max
|
| 997 |
+
assert not tdmax < timedelta.max
|
| 998 |
+
assert not tdmax <= timedelta.max
|
| 999 |
+
assert tdmax != timedelta.max
|
| 1000 |
+
assert not tdmax == timedelta.max
|
| 1001 |
+
|
| 1002 |
+
assert tdmin < timedelta.min
|
| 1003 |
+
assert tdmin <= timedelta.min
|
| 1004 |
+
assert not tdmin > timedelta.min
|
| 1005 |
+
assert not tdmin >= timedelta.min
|
| 1006 |
+
assert tdmin != timedelta.min
|
| 1007 |
+
assert not tdmin == timedelta.min
|
| 1008 |
+
|
| 1009 |
+
def test_compare_pytimedelta_bounds2(self):
|
| 1010 |
+
# a pytimedelta outside the microsecond bounds
|
| 1011 |
+
pytd = timedelta(days=999999999, seconds=86399)
|
| 1012 |
+
# NB: np.timedelta64(td, "s"") incorrectly overflows
|
| 1013 |
+
td64 = np.timedelta64(pytd.days, "D") + np.timedelta64(pytd.seconds, "s")
|
| 1014 |
+
td = Timedelta(td64)
|
| 1015 |
+
assert td.days == pytd.days
|
| 1016 |
+
assert td.seconds == pytd.seconds
|
| 1017 |
+
|
| 1018 |
+
assert td == pytd
|
| 1019 |
+
assert not td != pytd
|
| 1020 |
+
assert not td < pytd
|
| 1021 |
+
assert not td > pytd
|
| 1022 |
+
assert td <= pytd
|
| 1023 |
+
assert td >= pytd
|
| 1024 |
+
|
| 1025 |
+
td2 = td - Timedelta(seconds=1).as_unit("s")
|
| 1026 |
+
assert td2 != pytd
|
| 1027 |
+
assert not td2 == pytd
|
| 1028 |
+
assert td2 < pytd
|
| 1029 |
+
assert td2 <= pytd
|
| 1030 |
+
assert not td2 > pytd
|
| 1031 |
+
assert not td2 >= pytd
|
| 1032 |
+
|
| 1033 |
+
def test_compare_tick(self, tick_classes):
|
| 1034 |
+
cls = tick_classes
|
| 1035 |
+
|
| 1036 |
+
off = cls(4)
|
| 1037 |
+
td = off.delta
|
| 1038 |
+
assert isinstance(td, Timedelta)
|
| 1039 |
+
|
| 1040 |
+
assert td == off
|
| 1041 |
+
assert not td != off
|
| 1042 |
+
assert td <= off
|
| 1043 |
+
assert td >= off
|
| 1044 |
+
assert not td < off
|
| 1045 |
+
assert not td > off
|
| 1046 |
+
|
| 1047 |
+
assert not td == 2 * off
|
| 1048 |
+
assert td != 2 * off
|
| 1049 |
+
assert td <= 2 * off
|
| 1050 |
+
assert td < 2 * off
|
| 1051 |
+
assert not td >= 2 * off
|
| 1052 |
+
assert not td > 2 * off
|
| 1053 |
+
|
| 1054 |
+
def test_comparison_object_array(self):
|
| 1055 |
+
# analogous to GH#15183
|
| 1056 |
+
td = Timedelta("2 days")
|
| 1057 |
+
other = Timedelta("3 hours")
|
| 1058 |
+
|
| 1059 |
+
arr = np.array([other, td], dtype=object)
|
| 1060 |
+
res = arr == td
|
| 1061 |
+
expected = np.array([False, True], dtype=bool)
|
| 1062 |
+
assert (res == expected).all()
|
| 1063 |
+
|
| 1064 |
+
# 2D case
|
| 1065 |
+
arr = np.array([[other, td], [td, other]], dtype=object)
|
| 1066 |
+
res = arr != td
|
| 1067 |
+
expected = np.array([[True, False], [False, True]], dtype=bool)
|
| 1068 |
+
assert res.shape == expected.shape
|
| 1069 |
+
assert (res == expected).all()
|
| 1070 |
+
|
| 1071 |
+
def test_compare_timedelta_ndarray(self):
|
| 1072 |
+
# GH#11835
|
| 1073 |
+
periods = [Timedelta("0 days 01:00:00"), Timedelta("0 days 01:00:00")]
|
| 1074 |
+
arr = np.array(periods)
|
| 1075 |
+
result = arr[0] > arr
|
| 1076 |
+
expected = np.array([False, False])
|
| 1077 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 1078 |
+
|
| 1079 |
+
def test_compare_td64_ndarray(self):
|
| 1080 |
+
# GG#33441
|
| 1081 |
+
arr = np.arange(5).astype("timedelta64[ns]")
|
| 1082 |
+
td = Timedelta(arr[1])
|
| 1083 |
+
|
| 1084 |
+
expected = np.array([False, True, False, False, False], dtype=bool)
|
| 1085 |
+
|
| 1086 |
+
result = td == arr
|
| 1087 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 1088 |
+
|
| 1089 |
+
result = arr == td
|
| 1090 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 1091 |
+
|
| 1092 |
+
result = td != arr
|
| 1093 |
+
tm.assert_numpy_array_equal(result, ~expected)
|
| 1094 |
+
|
| 1095 |
+
result = arr != td
|
| 1096 |
+
tm.assert_numpy_array_equal(result, ~expected)
|
| 1097 |
+
|
| 1098 |
+
def test_compare_custom_object(self):
|
| 1099 |
+
"""
|
| 1100 |
+
Make sure non supported operations on Timedelta returns NonImplemented
|
| 1101 |
+
and yields to other operand (GH#20829).
|
| 1102 |
+
"""
|
| 1103 |
+
|
| 1104 |
+
class CustomClass:
|
| 1105 |
+
def __init__(self, cmp_result=None) -> None:
|
| 1106 |
+
self.cmp_result = cmp_result
|
| 1107 |
+
|
| 1108 |
+
def generic_result(self):
|
| 1109 |
+
if self.cmp_result is None:
|
| 1110 |
+
return NotImplemented
|
| 1111 |
+
else:
|
| 1112 |
+
return self.cmp_result
|
| 1113 |
+
|
| 1114 |
+
def __eq__(self, other):
|
| 1115 |
+
return self.generic_result()
|
| 1116 |
+
|
| 1117 |
+
def __gt__(self, other):
|
| 1118 |
+
return self.generic_result()
|
| 1119 |
+
|
| 1120 |
+
t = Timedelta("1s")
|
| 1121 |
+
|
| 1122 |
+
assert t != "string"
|
| 1123 |
+
assert t != 1
|
| 1124 |
+
assert t != CustomClass()
|
| 1125 |
+
assert t != CustomClass(cmp_result=False)
|
| 1126 |
+
|
| 1127 |
+
assert t < CustomClass(cmp_result=True)
|
| 1128 |
+
assert not t < CustomClass(cmp_result=False)
|
| 1129 |
+
|
| 1130 |
+
assert t == CustomClass(cmp_result=True)
|
| 1131 |
+
|
| 1132 |
+
@pytest.mark.parametrize("val", ["string", 1])
|
| 1133 |
+
def test_compare_unknown_type(self, val):
|
| 1134 |
+
# GH#20829
|
| 1135 |
+
t = Timedelta("1s")
|
| 1136 |
+
msg = "not supported between instances of 'Timedelta' and '(int|str)'"
|
| 1137 |
+
with pytest.raises(TypeError, match=msg):
|
| 1138 |
+
t >= val
|
| 1139 |
+
with pytest.raises(TypeError, match=msg):
|
| 1140 |
+
t > val
|
| 1141 |
+
with pytest.raises(TypeError, match=msg):
|
| 1142 |
+
t <= val
|
| 1143 |
+
with pytest.raises(TypeError, match=msg):
|
| 1144 |
+
t < val
|
| 1145 |
+
|
| 1146 |
+
|
| 1147 |
+
def test_ops_notimplemented():
|
| 1148 |
+
class Other:
|
| 1149 |
+
pass
|
| 1150 |
+
|
| 1151 |
+
other = Other()
|
| 1152 |
+
|
| 1153 |
+
td = Timedelta("1 day")
|
| 1154 |
+
assert td.__add__(other) is NotImplemented
|
| 1155 |
+
assert td.__sub__(other) is NotImplemented
|
| 1156 |
+
assert td.__truediv__(other) is NotImplemented
|
| 1157 |
+
assert td.__mul__(other) is NotImplemented
|
| 1158 |
+
assert td.__floordiv__(other) is NotImplemented
|
| 1159 |
+
|
| 1160 |
+
|
| 1161 |
+
def test_ops_error_str():
|
| 1162 |
+
# GH#13624
|
| 1163 |
+
td = Timedelta("1 day")
|
| 1164 |
+
|
| 1165 |
+
for left, right in [(td, "a"), ("a", td)]:
|
| 1166 |
+
msg = "|".join(
|
| 1167 |
+
[
|
| 1168 |
+
"unsupported operand type",
|
| 1169 |
+
r'can only concatenate str \(not "Timedelta"\) to str',
|
| 1170 |
+
"must be str, not Timedelta",
|
| 1171 |
+
]
|
| 1172 |
+
)
|
| 1173 |
+
with pytest.raises(TypeError, match=msg):
|
| 1174 |
+
left + right
|
| 1175 |
+
|
| 1176 |
+
msg = "not supported between instances of"
|
| 1177 |
+
with pytest.raises(TypeError, match=msg):
|
| 1178 |
+
left > right
|
| 1179 |
+
|
| 1180 |
+
assert not left == right # pylint: disable=unneeded-not
|
| 1181 |
+
assert left != right
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/test_constructors.py
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import timedelta
|
| 2 |
+
from itertools import product
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from pandas._libs.tslibs import OutOfBoundsTimedelta
|
| 8 |
+
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
|
| 9 |
+
|
| 10 |
+
from pandas import (
|
| 11 |
+
NaT,
|
| 12 |
+
Timedelta,
|
| 13 |
+
offsets,
|
| 14 |
+
to_timedelta,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_construct_with_weeks_unit_overflow():
|
| 19 |
+
# GH#47268 don't silently wrap around
|
| 20 |
+
with pytest.raises(OutOfBoundsTimedelta, match="without overflow"):
|
| 21 |
+
Timedelta(1000000000000000000, unit="W")
|
| 22 |
+
|
| 23 |
+
with pytest.raises(OutOfBoundsTimedelta, match="without overflow"):
|
| 24 |
+
Timedelta(1000000000000000000.0, unit="W")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_construct_from_td64_with_unit():
|
| 28 |
+
# ignore the unit, as it may cause silently overflows leading to incorrect
|
| 29 |
+
# results, and in non-overflow cases is irrelevant GH#46827
|
| 30 |
+
obj = np.timedelta64(123456789000000000, "h")
|
| 31 |
+
|
| 32 |
+
with pytest.raises(OutOfBoundsTimedelta, match="123456789000000000 hours"):
|
| 33 |
+
Timedelta(obj, unit="ps")
|
| 34 |
+
|
| 35 |
+
with pytest.raises(OutOfBoundsTimedelta, match="123456789000000000 hours"):
|
| 36 |
+
Timedelta(obj, unit="ns")
|
| 37 |
+
|
| 38 |
+
with pytest.raises(OutOfBoundsTimedelta, match="123456789000000000 hours"):
|
| 39 |
+
Timedelta(obj)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_from_td64_retain_resolution():
|
| 43 |
+
# case where we retain millisecond resolution
|
| 44 |
+
obj = np.timedelta64(12345, "ms")
|
| 45 |
+
|
| 46 |
+
td = Timedelta(obj)
|
| 47 |
+
assert td._value == obj.view("i8")
|
| 48 |
+
assert td._creso == NpyDatetimeUnit.NPY_FR_ms.value
|
| 49 |
+
|
| 50 |
+
# Case where we cast to nearest-supported reso
|
| 51 |
+
obj2 = np.timedelta64(1234, "D")
|
| 52 |
+
td2 = Timedelta(obj2)
|
| 53 |
+
assert td2._creso == NpyDatetimeUnit.NPY_FR_s.value
|
| 54 |
+
assert td2 == obj2
|
| 55 |
+
assert td2.days == 1234
|
| 56 |
+
|
| 57 |
+
# Case that _would_ overflow if we didn't support non-nano
|
| 58 |
+
obj3 = np.timedelta64(1000000000000000000, "us")
|
| 59 |
+
td3 = Timedelta(obj3)
|
| 60 |
+
assert td3.total_seconds() == 1000000000000
|
| 61 |
+
assert td3._creso == NpyDatetimeUnit.NPY_FR_us.value
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_from_pytimedelta_us_reso():
|
| 65 |
+
# pytimedelta has microsecond resolution, so Timedelta(pytd) inherits that
|
| 66 |
+
td = timedelta(days=4, minutes=3)
|
| 67 |
+
result = Timedelta(td)
|
| 68 |
+
assert result.to_pytimedelta() == td
|
| 69 |
+
assert result._creso == NpyDatetimeUnit.NPY_FR_us.value
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_from_tick_reso():
|
| 73 |
+
tick = offsets.Nano()
|
| 74 |
+
assert Timedelta(tick)._creso == NpyDatetimeUnit.NPY_FR_ns.value
|
| 75 |
+
|
| 76 |
+
tick = offsets.Micro()
|
| 77 |
+
assert Timedelta(tick)._creso == NpyDatetimeUnit.NPY_FR_us.value
|
| 78 |
+
|
| 79 |
+
tick = offsets.Milli()
|
| 80 |
+
assert Timedelta(tick)._creso == NpyDatetimeUnit.NPY_FR_ms.value
|
| 81 |
+
|
| 82 |
+
tick = offsets.Second()
|
| 83 |
+
assert Timedelta(tick)._creso == NpyDatetimeUnit.NPY_FR_s.value
|
| 84 |
+
|
| 85 |
+
# everything above Second gets cast to the closest supported reso: second
|
| 86 |
+
tick = offsets.Minute()
|
| 87 |
+
assert Timedelta(tick)._creso == NpyDatetimeUnit.NPY_FR_s.value
|
| 88 |
+
|
| 89 |
+
tick = offsets.Hour()
|
| 90 |
+
assert Timedelta(tick)._creso == NpyDatetimeUnit.NPY_FR_s.value
|
| 91 |
+
|
| 92 |
+
tick = offsets.Day()
|
| 93 |
+
assert Timedelta(tick)._creso == NpyDatetimeUnit.NPY_FR_s.value
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def test_construction():
|
| 97 |
+
expected = np.timedelta64(10, "D").astype("m8[ns]").view("i8")
|
| 98 |
+
assert Timedelta(10, unit="d")._value == expected
|
| 99 |
+
assert Timedelta(10.0, unit="d")._value == expected
|
| 100 |
+
assert Timedelta("10 days")._value == expected
|
| 101 |
+
assert Timedelta(days=10)._value == expected
|
| 102 |
+
assert Timedelta(days=10.0)._value == expected
|
| 103 |
+
|
| 104 |
+
expected += np.timedelta64(10, "s").astype("m8[ns]").view("i8")
|
| 105 |
+
assert Timedelta("10 days 00:00:10")._value == expected
|
| 106 |
+
assert Timedelta(days=10, seconds=10)._value == expected
|
| 107 |
+
assert Timedelta(days=10, milliseconds=10 * 1000)._value == expected
|
| 108 |
+
assert Timedelta(days=10, microseconds=10 * 1000 * 1000)._value == expected
|
| 109 |
+
|
| 110 |
+
# rounding cases
|
| 111 |
+
assert Timedelta(82739999850000)._value == 82739999850000
|
| 112 |
+
assert "0 days 22:58:59.999850" in str(Timedelta(82739999850000))
|
| 113 |
+
assert Timedelta(123072001000000)._value == 123072001000000
|
| 114 |
+
assert "1 days 10:11:12.001" in str(Timedelta(123072001000000))
|
| 115 |
+
|
| 116 |
+
# string conversion with/without leading zero
|
| 117 |
+
# GH#9570
|
| 118 |
+
assert Timedelta("0:00:00") == timedelta(hours=0)
|
| 119 |
+
assert Timedelta("00:00:00") == timedelta(hours=0)
|
| 120 |
+
assert Timedelta("-1:00:00") == -timedelta(hours=1)
|
| 121 |
+
assert Timedelta("-01:00:00") == -timedelta(hours=1)
|
| 122 |
+
|
| 123 |
+
# more strings & abbrevs
|
| 124 |
+
# GH#8190
|
| 125 |
+
assert Timedelta("1 h") == timedelta(hours=1)
|
| 126 |
+
assert Timedelta("1 hour") == timedelta(hours=1)
|
| 127 |
+
assert Timedelta("1 hr") == timedelta(hours=1)
|
| 128 |
+
assert Timedelta("1 hours") == timedelta(hours=1)
|
| 129 |
+
assert Timedelta("-1 hours") == -timedelta(hours=1)
|
| 130 |
+
assert Timedelta("1 m") == timedelta(minutes=1)
|
| 131 |
+
assert Timedelta("1.5 m") == timedelta(seconds=90)
|
| 132 |
+
assert Timedelta("1 minute") == timedelta(minutes=1)
|
| 133 |
+
assert Timedelta("1 minutes") == timedelta(minutes=1)
|
| 134 |
+
assert Timedelta("1 s") == timedelta(seconds=1)
|
| 135 |
+
assert Timedelta("1 second") == timedelta(seconds=1)
|
| 136 |
+
assert Timedelta("1 seconds") == timedelta(seconds=1)
|
| 137 |
+
assert Timedelta("1 ms") == timedelta(milliseconds=1)
|
| 138 |
+
assert Timedelta("1 milli") == timedelta(milliseconds=1)
|
| 139 |
+
assert Timedelta("1 millisecond") == timedelta(milliseconds=1)
|
| 140 |
+
assert Timedelta("1 us") == timedelta(microseconds=1)
|
| 141 |
+
assert Timedelta("1 µs") == timedelta(microseconds=1)
|
| 142 |
+
assert Timedelta("1 micros") == timedelta(microseconds=1)
|
| 143 |
+
assert Timedelta("1 microsecond") == timedelta(microseconds=1)
|
| 144 |
+
assert Timedelta("1.5 microsecond") == Timedelta("00:00:00.000001500")
|
| 145 |
+
assert Timedelta("1 ns") == Timedelta("00:00:00.000000001")
|
| 146 |
+
assert Timedelta("1 nano") == Timedelta("00:00:00.000000001")
|
| 147 |
+
assert Timedelta("1 nanosecond") == Timedelta("00:00:00.000000001")
|
| 148 |
+
|
| 149 |
+
# combos
|
| 150 |
+
assert Timedelta("10 days 1 hour") == timedelta(days=10, hours=1)
|
| 151 |
+
assert Timedelta("10 days 1 h") == timedelta(days=10, hours=1)
|
| 152 |
+
assert Timedelta("10 days 1 h 1m 1s") == timedelta(
|
| 153 |
+
days=10, hours=1, minutes=1, seconds=1
|
| 154 |
+
)
|
| 155 |
+
assert Timedelta("-10 days 1 h 1m 1s") == -timedelta(
|
| 156 |
+
days=10, hours=1, minutes=1, seconds=1
|
| 157 |
+
)
|
| 158 |
+
assert Timedelta("-10 days 1 h 1m 1s") == -timedelta(
|
| 159 |
+
days=10, hours=1, minutes=1, seconds=1
|
| 160 |
+
)
|
| 161 |
+
assert Timedelta("-10 days 1 h 1m 1s 3us") == -timedelta(
|
| 162 |
+
days=10, hours=1, minutes=1, seconds=1, microseconds=3
|
| 163 |
+
)
|
| 164 |
+
assert Timedelta("-10 days 1 h 1.5m 1s 3us") == -timedelta(
|
| 165 |
+
days=10, hours=1, minutes=1, seconds=31, microseconds=3
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
# Currently invalid as it has a - on the hh:mm:dd part
|
| 169 |
+
# (only allowed on the days)
|
| 170 |
+
msg = "only leading negative signs are allowed"
|
| 171 |
+
with pytest.raises(ValueError, match=msg):
|
| 172 |
+
Timedelta("-10 days -1 h 1.5m 1s 3us")
|
| 173 |
+
|
| 174 |
+
# only leading neg signs are allowed
|
| 175 |
+
with pytest.raises(ValueError, match=msg):
|
| 176 |
+
Timedelta("10 days -1 h 1.5m 1s 3us")
|
| 177 |
+
|
| 178 |
+
# no units specified
|
| 179 |
+
msg = "no units specified"
|
| 180 |
+
with pytest.raises(ValueError, match=msg):
|
| 181 |
+
Timedelta("3.1415")
|
| 182 |
+
|
| 183 |
+
# invalid construction
|
| 184 |
+
msg = "cannot construct a Timedelta"
|
| 185 |
+
with pytest.raises(ValueError, match=msg):
|
| 186 |
+
Timedelta()
|
| 187 |
+
|
| 188 |
+
msg = "unit abbreviation w/o a number"
|
| 189 |
+
with pytest.raises(ValueError, match=msg):
|
| 190 |
+
Timedelta("foo")
|
| 191 |
+
|
| 192 |
+
msg = (
|
| 193 |
+
"cannot construct a Timedelta from "
|
| 194 |
+
"the passed arguments, allowed keywords are "
|
| 195 |
+
)
|
| 196 |
+
with pytest.raises(ValueError, match=msg):
|
| 197 |
+
Timedelta(day=10)
|
| 198 |
+
|
| 199 |
+
# floats
|
| 200 |
+
expected = np.timedelta64(10, "s").astype("m8[ns]").view("i8") + np.timedelta64(
|
| 201 |
+
500, "ms"
|
| 202 |
+
).astype("m8[ns]").view("i8")
|
| 203 |
+
assert Timedelta(10.5, unit="s")._value == expected
|
| 204 |
+
|
| 205 |
+
# offset
|
| 206 |
+
assert to_timedelta(offsets.Hour(2)) == Timedelta(hours=2)
|
| 207 |
+
assert Timedelta(offsets.Hour(2)) == Timedelta(hours=2)
|
| 208 |
+
assert Timedelta(offsets.Second(2)) == Timedelta(seconds=2)
|
| 209 |
+
|
| 210 |
+
# GH#11995: unicode
|
| 211 |
+
expected = Timedelta("1H")
|
| 212 |
+
result = Timedelta("1H")
|
| 213 |
+
assert result == expected
|
| 214 |
+
assert to_timedelta(offsets.Hour(2)) == Timedelta("0 days, 02:00:00")
|
| 215 |
+
|
| 216 |
+
msg = "unit abbreviation w/o a number"
|
| 217 |
+
with pytest.raises(ValueError, match=msg):
|
| 218 |
+
Timedelta("foo bar")
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
@pytest.mark.parametrize(
|
| 222 |
+
"item",
|
| 223 |
+
list(
|
| 224 |
+
{
|
| 225 |
+
"days": "D",
|
| 226 |
+
"seconds": "s",
|
| 227 |
+
"microseconds": "us",
|
| 228 |
+
"milliseconds": "ms",
|
| 229 |
+
"minutes": "m",
|
| 230 |
+
"hours": "h",
|
| 231 |
+
"weeks": "W",
|
| 232 |
+
}.items()
|
| 233 |
+
),
|
| 234 |
+
)
|
| 235 |
+
@pytest.mark.parametrize(
|
| 236 |
+
"npdtype", [np.int64, np.int32, np.int16, np.float64, np.float32, np.float16]
|
| 237 |
+
)
|
| 238 |
+
def test_td_construction_with_np_dtypes(npdtype, item):
|
| 239 |
+
# GH#8757: test construction with np dtypes
|
| 240 |
+
pykwarg, npkwarg = item
|
| 241 |
+
expected = np.timedelta64(1, npkwarg).astype("m8[ns]").view("i8")
|
| 242 |
+
assert Timedelta(**{pykwarg: npdtype(1)})._value == expected
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
@pytest.mark.parametrize(
|
| 246 |
+
"val",
|
| 247 |
+
[
|
| 248 |
+
"1s",
|
| 249 |
+
"-1s",
|
| 250 |
+
"1us",
|
| 251 |
+
"-1us",
|
| 252 |
+
"1 day",
|
| 253 |
+
"-1 day",
|
| 254 |
+
"-23:59:59.999999",
|
| 255 |
+
"-1 days +23:59:59.999999",
|
| 256 |
+
"-1ns",
|
| 257 |
+
"1ns",
|
| 258 |
+
"-23:59:59.999999999",
|
| 259 |
+
],
|
| 260 |
+
)
|
| 261 |
+
def test_td_from_repr_roundtrip(val):
|
| 262 |
+
# round-trip both for string and value
|
| 263 |
+
td = Timedelta(val)
|
| 264 |
+
assert Timedelta(td._value) == td
|
| 265 |
+
|
| 266 |
+
assert Timedelta(str(td)) == td
|
| 267 |
+
assert Timedelta(td._repr_base(format="all")) == td
|
| 268 |
+
assert Timedelta(td._repr_base()) == td
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def test_overflow_on_construction():
|
| 272 |
+
# GH#3374
|
| 273 |
+
value = Timedelta("1day")._value * 20169940
|
| 274 |
+
msg = "Cannot cast 1742682816000000000000 from ns to 'ns' without overflow"
|
| 275 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 276 |
+
Timedelta(value)
|
| 277 |
+
|
| 278 |
+
# xref GH#17637
|
| 279 |
+
msg = "Cannot cast 139993 from D to 'ns' without overflow"
|
| 280 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 281 |
+
Timedelta(7 * 19999, unit="D")
|
| 282 |
+
|
| 283 |
+
# used to overflow before non-ns support
|
| 284 |
+
td = Timedelta(timedelta(days=13 * 19999))
|
| 285 |
+
assert td._creso == NpyDatetimeUnit.NPY_FR_us.value
|
| 286 |
+
assert td.days == 13 * 19999
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
@pytest.mark.parametrize(
|
| 290 |
+
"val, unit",
|
| 291 |
+
[
|
| 292 |
+
(3508, "M"),
|
| 293 |
+
(15251, "W"), # 1
|
| 294 |
+
(106752, "D"), # change from previous:
|
| 295 |
+
(2562048, "h"), # 0 hours
|
| 296 |
+
(153722868, "m"), # 13 minutes
|
| 297 |
+
(9223372037, "s"), # 44 seconds
|
| 298 |
+
],
|
| 299 |
+
)
|
| 300 |
+
def test_construction_out_of_bounds_td64ns(val, unit):
|
| 301 |
+
# TODO: parametrize over units just above/below the implementation bounds
|
| 302 |
+
# once GH#38964 is resolved
|
| 303 |
+
|
| 304 |
+
# Timedelta.max is just under 106752 days
|
| 305 |
+
td64 = np.timedelta64(val, unit)
|
| 306 |
+
assert td64.astype("m8[ns]").view("i8") < 0 # i.e. naive astype will be wrong
|
| 307 |
+
|
| 308 |
+
td = Timedelta(td64)
|
| 309 |
+
if unit != "M":
|
| 310 |
+
# with unit="M" the conversion to "s" is poorly defined
|
| 311 |
+
# (and numpy issues DeprecationWarning)
|
| 312 |
+
assert td.asm8 == td64
|
| 313 |
+
assert td.asm8.dtype == "m8[s]"
|
| 314 |
+
msg = r"Cannot cast 1067\d\d days .* to unit='ns' without overflow"
|
| 315 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 316 |
+
td.as_unit("ns")
|
| 317 |
+
|
| 318 |
+
# But just back in bounds and we are OK
|
| 319 |
+
assert Timedelta(td64 - 1) == td64 - 1
|
| 320 |
+
|
| 321 |
+
td64 *= -1
|
| 322 |
+
assert td64.astype("m8[ns]").view("i8") > 0 # i.e. naive astype will be wrong
|
| 323 |
+
|
| 324 |
+
td2 = Timedelta(td64)
|
| 325 |
+
msg = r"Cannot cast -1067\d\d days .* to unit='ns' without overflow"
|
| 326 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 327 |
+
td2.as_unit("ns")
|
| 328 |
+
|
| 329 |
+
# But just back in bounds and we are OK
|
| 330 |
+
assert Timedelta(td64 + 1) == td64 + 1
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
@pytest.mark.parametrize(
|
| 334 |
+
"val, unit",
|
| 335 |
+
[
|
| 336 |
+
(3508 * 10**9, "M"),
|
| 337 |
+
(15251 * 10**9, "W"),
|
| 338 |
+
(106752 * 10**9, "D"),
|
| 339 |
+
(2562048 * 10**9, "h"),
|
| 340 |
+
(153722868 * 10**9, "m"),
|
| 341 |
+
],
|
| 342 |
+
)
|
| 343 |
+
def test_construction_out_of_bounds_td64s(val, unit):
|
| 344 |
+
td64 = np.timedelta64(val, unit)
|
| 345 |
+
with pytest.raises(OutOfBoundsTimedelta, match=str(td64)):
|
| 346 |
+
Timedelta(td64)
|
| 347 |
+
|
| 348 |
+
# But just back in bounds and we are OK
|
| 349 |
+
assert Timedelta(td64 - 10**9) == td64 - 10**9
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
@pytest.mark.parametrize(
|
| 353 |
+
"fmt,exp",
|
| 354 |
+
[
|
| 355 |
+
(
|
| 356 |
+
"P6DT0H50M3.010010012S",
|
| 357 |
+
Timedelta(
|
| 358 |
+
days=6,
|
| 359 |
+
minutes=50,
|
| 360 |
+
seconds=3,
|
| 361 |
+
milliseconds=10,
|
| 362 |
+
microseconds=10,
|
| 363 |
+
nanoseconds=12,
|
| 364 |
+
),
|
| 365 |
+
),
|
| 366 |
+
(
|
| 367 |
+
"P-6DT0H50M3.010010012S",
|
| 368 |
+
Timedelta(
|
| 369 |
+
days=-6,
|
| 370 |
+
minutes=50,
|
| 371 |
+
seconds=3,
|
| 372 |
+
milliseconds=10,
|
| 373 |
+
microseconds=10,
|
| 374 |
+
nanoseconds=12,
|
| 375 |
+
),
|
| 376 |
+
),
|
| 377 |
+
("P4DT12H30M5S", Timedelta(days=4, hours=12, minutes=30, seconds=5)),
|
| 378 |
+
("P0DT0H0M0.000000123S", Timedelta(nanoseconds=123)),
|
| 379 |
+
("P0DT0H0M0.00001S", Timedelta(microseconds=10)),
|
| 380 |
+
("P0DT0H0M0.001S", Timedelta(milliseconds=1)),
|
| 381 |
+
("P0DT0H1M0S", Timedelta(minutes=1)),
|
| 382 |
+
("P1DT25H61M61S", Timedelta(days=1, hours=25, minutes=61, seconds=61)),
|
| 383 |
+
("PT1S", Timedelta(seconds=1)),
|
| 384 |
+
("PT0S", Timedelta(seconds=0)),
|
| 385 |
+
("P1WT0S", Timedelta(days=7, seconds=0)),
|
| 386 |
+
("P1D", Timedelta(days=1)),
|
| 387 |
+
("P1DT1H", Timedelta(days=1, hours=1)),
|
| 388 |
+
("P1W", Timedelta(days=7)),
|
| 389 |
+
("PT300S", Timedelta(seconds=300)),
|
| 390 |
+
("P1DT0H0M00000000000S", Timedelta(days=1)),
|
| 391 |
+
("PT-6H3M", Timedelta(hours=-6, minutes=3)),
|
| 392 |
+
("-PT6H3M", Timedelta(hours=-6, minutes=-3)),
|
| 393 |
+
("-PT-6H+3M", Timedelta(hours=6, minutes=-3)),
|
| 394 |
+
],
|
| 395 |
+
)
|
| 396 |
+
def test_iso_constructor(fmt, exp):
|
| 397 |
+
assert Timedelta(fmt) == exp
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
@pytest.mark.parametrize(
|
| 401 |
+
"fmt",
|
| 402 |
+
[
|
| 403 |
+
"PPPPPPPPPPPP",
|
| 404 |
+
"PDTHMS",
|
| 405 |
+
"P0DT999H999M999S",
|
| 406 |
+
"P1DT0H0M0.0000000000000S",
|
| 407 |
+
"P1DT0H0M0.S",
|
| 408 |
+
"P",
|
| 409 |
+
"-P",
|
| 410 |
+
],
|
| 411 |
+
)
|
| 412 |
+
def test_iso_constructor_raises(fmt):
|
| 413 |
+
msg = f"Invalid ISO 8601 Duration format - {fmt}"
|
| 414 |
+
with pytest.raises(ValueError, match=msg):
|
| 415 |
+
Timedelta(fmt)
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
@pytest.mark.parametrize(
|
| 419 |
+
"constructed_td, conversion",
|
| 420 |
+
[
|
| 421 |
+
(Timedelta(nanoseconds=100), "100ns"),
|
| 422 |
+
(
|
| 423 |
+
Timedelta(
|
| 424 |
+
days=1,
|
| 425 |
+
hours=1,
|
| 426 |
+
minutes=1,
|
| 427 |
+
weeks=1,
|
| 428 |
+
seconds=1,
|
| 429 |
+
milliseconds=1,
|
| 430 |
+
microseconds=1,
|
| 431 |
+
nanoseconds=1,
|
| 432 |
+
),
|
| 433 |
+
694861001001001,
|
| 434 |
+
),
|
| 435 |
+
(Timedelta(microseconds=1) + Timedelta(nanoseconds=1), "1us1ns"),
|
| 436 |
+
(Timedelta(microseconds=1) - Timedelta(nanoseconds=1), "999ns"),
|
| 437 |
+
(Timedelta(microseconds=1) + 5 * Timedelta(nanoseconds=-2), "990ns"),
|
| 438 |
+
],
|
| 439 |
+
)
|
| 440 |
+
def test_td_constructor_on_nanoseconds(constructed_td, conversion):
|
| 441 |
+
# GH#9273
|
| 442 |
+
assert constructed_td == Timedelta(conversion)
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
def test_td_constructor_value_error():
|
| 446 |
+
msg = "Invalid type <class 'str'>. Must be int or float."
|
| 447 |
+
with pytest.raises(TypeError, match=msg):
|
| 448 |
+
Timedelta(nanoseconds="abc")
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def test_timedelta_constructor_identity():
|
| 452 |
+
# Test for #30543
|
| 453 |
+
expected = Timedelta(np.timedelta64(1, "s"))
|
| 454 |
+
result = Timedelta(expected)
|
| 455 |
+
assert result is expected
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
def test_timedelta_pass_td_and_kwargs_raises():
|
| 459 |
+
# don't silently ignore the kwargs GH#48898
|
| 460 |
+
td = Timedelta(days=1)
|
| 461 |
+
msg = (
|
| 462 |
+
"Cannot pass both a Timedelta input and timedelta keyword arguments, "
|
| 463 |
+
r"got \['days'\]"
|
| 464 |
+
)
|
| 465 |
+
with pytest.raises(ValueError, match=msg):
|
| 466 |
+
Timedelta(td, days=2)
|
| 467 |
+
|
| 468 |
+
|
| 469 |
+
@pytest.mark.parametrize(
|
| 470 |
+
"constructor, value, unit, expectation",
|
| 471 |
+
[
|
| 472 |
+
(Timedelta, "10s", "ms", (ValueError, "unit must not be specified")),
|
| 473 |
+
(to_timedelta, "10s", "ms", (ValueError, "unit must not be specified")),
|
| 474 |
+
(to_timedelta, ["1", 2, 3], "s", (ValueError, "unit must not be specified")),
|
| 475 |
+
],
|
| 476 |
+
)
|
| 477 |
+
def test_string_with_unit(constructor, value, unit, expectation):
|
| 478 |
+
exp, match = expectation
|
| 479 |
+
with pytest.raises(exp, match=match):
|
| 480 |
+
_ = constructor(value, unit=unit)
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
@pytest.mark.parametrize(
|
| 484 |
+
"value",
|
| 485 |
+
[
|
| 486 |
+
"".join(elements)
|
| 487 |
+
for repetition in (1, 2)
|
| 488 |
+
for elements in product("+-, ", repeat=repetition)
|
| 489 |
+
],
|
| 490 |
+
)
|
| 491 |
+
def test_string_without_numbers(value):
|
| 492 |
+
# GH39710 Timedelta input string with only symbols and no digits raises an error
|
| 493 |
+
msg = (
|
| 494 |
+
"symbols w/o a number"
|
| 495 |
+
if value != "--"
|
| 496 |
+
else "only leading negative signs are allowed"
|
| 497 |
+
)
|
| 498 |
+
with pytest.raises(ValueError, match=msg):
|
| 499 |
+
Timedelta(value)
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
def test_timedelta_new_npnat():
|
| 503 |
+
# GH#48898
|
| 504 |
+
nat = np.timedelta64("NaT", "h")
|
| 505 |
+
assert Timedelta(nat) is NaT
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
def test_subclass_respected():
|
| 509 |
+
# GH#49579
|
| 510 |
+
class MyCustomTimedelta(Timedelta):
|
| 511 |
+
pass
|
| 512 |
+
|
| 513 |
+
td = MyCustomTimedelta("1 minute")
|
| 514 |
+
assert isinstance(td, MyCustomTimedelta)
|
| 515 |
+
|
| 516 |
+
|
| 517 |
+
def test_non_nano_value():
|
| 518 |
+
# https://github.com/pandas-dev/pandas/issues/49076
|
| 519 |
+
result = Timedelta(10, unit="D").as_unit("s").value
|
| 520 |
+
# `.value` shows nanoseconds, even though unit is 's'
|
| 521 |
+
assert result == 864000000000000
|
| 522 |
+
|
| 523 |
+
# out-of-nanoseconds-bounds `.value` raises informative message
|
| 524 |
+
msg = (
|
| 525 |
+
r"Cannot convert Timedelta to nanoseconds without overflow. "
|
| 526 |
+
r"Use `.asm8.view\('i8'\)` to cast represent Timedelta in its "
|
| 527 |
+
r"own unit \(here, s\).$"
|
| 528 |
+
)
|
| 529 |
+
td = Timedelta(1_000, "D").as_unit("s") * 1_000
|
| 530 |
+
with pytest.raises(OverflowError, match=msg):
|
| 531 |
+
td.value
|
| 532 |
+
# check that the suggested workaround actually works
|
| 533 |
+
result = td.asm8.view("i8")
|
| 534 |
+
assert result == 86400000000
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timedelta/test_formats.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import Timedelta
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
@pytest.mark.parametrize(
|
| 7 |
+
"td, expected_repr",
|
| 8 |
+
[
|
| 9 |
+
(Timedelta(10, unit="d"), "Timedelta('10 days 00:00:00')"),
|
| 10 |
+
(Timedelta(10, unit="s"), "Timedelta('0 days 00:00:10')"),
|
| 11 |
+
(Timedelta(10, unit="ms"), "Timedelta('0 days 00:00:00.010000')"),
|
| 12 |
+
(Timedelta(-10, unit="ms"), "Timedelta('-1 days +23:59:59.990000')"),
|
| 13 |
+
],
|
| 14 |
+
)
|
| 15 |
+
def test_repr(td, expected_repr):
|
| 16 |
+
assert repr(td) == expected_repr
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.mark.parametrize(
|
| 20 |
+
"td, expected_iso",
|
| 21 |
+
[
|
| 22 |
+
(
|
| 23 |
+
Timedelta(
|
| 24 |
+
days=6,
|
| 25 |
+
minutes=50,
|
| 26 |
+
seconds=3,
|
| 27 |
+
milliseconds=10,
|
| 28 |
+
microseconds=10,
|
| 29 |
+
nanoseconds=12,
|
| 30 |
+
),
|
| 31 |
+
"P6DT0H50M3.010010012S",
|
| 32 |
+
),
|
| 33 |
+
(Timedelta(days=4, hours=12, minutes=30, seconds=5), "P4DT12H30M5S"),
|
| 34 |
+
(Timedelta(nanoseconds=123), "P0DT0H0M0.000000123S"),
|
| 35 |
+
# trim nano
|
| 36 |
+
(Timedelta(microseconds=10), "P0DT0H0M0.00001S"),
|
| 37 |
+
# trim micro
|
| 38 |
+
(Timedelta(milliseconds=1), "P0DT0H0M0.001S"),
|
| 39 |
+
# don't strip every 0
|
| 40 |
+
(Timedelta(minutes=1), "P0DT0H1M0S"),
|
| 41 |
+
],
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" test the scalar Timedelta """
|
| 2 |
+
from datetime import timedelta
|
| 3 |
+
|
| 4 |
+
from hypothesis import (
|
| 5 |
+
given,
|
| 6 |
+
strategies as st,
|
| 7 |
+
)
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
from pandas._libs import lib
|
| 12 |
+
from pandas._libs.tslibs import (
|
| 13 |
+
NaT,
|
| 14 |
+
iNaT,
|
| 15 |
+
)
|
| 16 |
+
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
|
| 17 |
+
from pandas.errors import OutOfBoundsTimedelta
|
| 18 |
+
|
| 19 |
+
import pandas as pd
|
| 20 |
+
from pandas import (
|
| 21 |
+
Timedelta,
|
| 22 |
+
TimedeltaIndex,
|
| 23 |
+
to_timedelta,
|
| 24 |
+
)
|
| 25 |
+
import pandas._testing as tm
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TestAsUnit:
|
| 29 |
+
def test_as_unit(self):
|
| 30 |
+
td = Timedelta(days=1)
|
| 31 |
+
|
| 32 |
+
assert td.as_unit("ns") is td
|
| 33 |
+
|
| 34 |
+
res = td.as_unit("us")
|
| 35 |
+
assert res._value == td._value // 1000
|
| 36 |
+
assert res._creso == NpyDatetimeUnit.NPY_FR_us.value
|
| 37 |
+
|
| 38 |
+
rt = res.as_unit("ns")
|
| 39 |
+
assert rt._value == td._value
|
| 40 |
+
assert rt._creso == td._creso
|
| 41 |
+
|
| 42 |
+
res = td.as_unit("ms")
|
| 43 |
+
assert res._value == td._value // 1_000_000
|
| 44 |
+
assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
|
| 45 |
+
|
| 46 |
+
rt = res.as_unit("ns")
|
| 47 |
+
assert rt._value == td._value
|
| 48 |
+
assert rt._creso == td._creso
|
| 49 |
+
|
| 50 |
+
res = td.as_unit("s")
|
| 51 |
+
assert res._value == td._value // 1_000_000_000
|
| 52 |
+
assert res._creso == NpyDatetimeUnit.NPY_FR_s.value
|
| 53 |
+
|
| 54 |
+
rt = res.as_unit("ns")
|
| 55 |
+
assert rt._value == td._value
|
| 56 |
+
assert rt._creso == td._creso
|
| 57 |
+
|
| 58 |
+
def test_as_unit_overflows(self):
|
| 59 |
+
# microsecond that would be just out of bounds for nano
|
| 60 |
+
us = 9223372800000000
|
| 61 |
+
td = Timedelta._from_value_and_reso(us, NpyDatetimeUnit.NPY_FR_us.value)
|
| 62 |
+
|
| 63 |
+
msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow"
|
| 64 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 65 |
+
td.as_unit("ns")
|
| 66 |
+
|
| 67 |
+
res = td.as_unit("ms")
|
| 68 |
+
assert res._value == us // 1000
|
| 69 |
+
assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
|
| 70 |
+
|
| 71 |
+
def test_as_unit_rounding(self):
|
| 72 |
+
td = Timedelta(microseconds=1500)
|
| 73 |
+
res = td.as_unit("ms")
|
| 74 |
+
|
| 75 |
+
expected = Timedelta(milliseconds=1)
|
| 76 |
+
assert res == expected
|
| 77 |
+
|
| 78 |
+
assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
|
| 79 |
+
assert res._value == 1
|
| 80 |
+
|
| 81 |
+
with pytest.raises(ValueError, match="Cannot losslessly convert units"):
|
| 82 |
+
td.as_unit("ms", round_ok=False)
|
| 83 |
+
|
| 84 |
+
def test_as_unit_non_nano(self):
|
| 85 |
+
# case where we are going neither to nor from nano
|
| 86 |
+
td = Timedelta(days=1).as_unit("ms")
|
| 87 |
+
assert td.days == 1
|
| 88 |
+
assert td._value == 86_400_000
|
| 89 |
+
assert td.components.days == 1
|
| 90 |
+
assert td._d == 1
|
| 91 |
+
assert td.total_seconds() == 86400
|
| 92 |
+
|
| 93 |
+
res = td.as_unit("us")
|
| 94 |
+
assert res._value == 86_400_000_000
|
| 95 |
+
assert res.components.days == 1
|
| 96 |
+
assert res.components.hours == 0
|
| 97 |
+
assert res._d == 1
|
| 98 |
+
assert res._h == 0
|
| 99 |
+
assert res.total_seconds() == 86400
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class TestNonNano:
|
| 103 |
+
@pytest.fixture(params=["s", "ms", "us"])
|
| 104 |
+
def unit_str(self, request):
|
| 105 |
+
return request.param
|
| 106 |
+
|
| 107 |
+
@pytest.fixture
|
| 108 |
+
def unit(self, unit_str):
|
| 109 |
+
# 7, 8, 9 correspond to second, millisecond, and microsecond, respectively
|
| 110 |
+
attr = f"NPY_FR_{unit_str}"
|
| 111 |
+
return getattr(NpyDatetimeUnit, attr).value
|
| 112 |
+
|
| 113 |
+
@pytest.fixture
|
| 114 |
+
def val(self, unit):
|
| 115 |
+
# microsecond that would be just out of bounds for nano
|
| 116 |
+
us = 9223372800000000
|
| 117 |
+
if unit == NpyDatetimeUnit.NPY_FR_us.value:
|
| 118 |
+
value = us
|
| 119 |
+
elif unit == NpyDatetimeUnit.NPY_FR_ms.value:
|
| 120 |
+
value = us // 1000
|
| 121 |
+
else:
|
| 122 |
+
value = us // 1_000_000
|
| 123 |
+
return value
|
| 124 |
+
|
| 125 |
+
@pytest.fixture
|
| 126 |
+
def td(self, unit, val):
|
| 127 |
+
return Timedelta._from_value_and_reso(val, unit)
|
| 128 |
+
|
| 129 |
+
def test_from_value_and_reso(self, unit, val):
|
| 130 |
+
# Just checking that the fixture is giving us what we asked for
|
| 131 |
+
td = Timedelta._from_value_and_reso(val, unit)
|
| 132 |
+
assert td._value == val
|
| 133 |
+
assert td._creso == unit
|
| 134 |
+
assert td.days == 106752
|
| 135 |
+
|
| 136 |
+
def test_unary_non_nano(self, td, unit):
|
| 137 |
+
assert abs(td)._creso == unit
|
| 138 |
+
assert (-td)._creso == unit
|
| 139 |
+
assert (+td)._creso == unit
|
| 140 |
+
|
| 141 |
+
def test_sub_preserves_reso(self, td, unit):
|
| 142 |
+
res = td - td
|
| 143 |
+
expected = Timedelta._from_value_and_reso(0, unit)
|
| 144 |
+
assert res == expected
|
| 145 |
+
assert res._creso == unit
|
| 146 |
+
|
| 147 |
+
def test_mul_preserves_reso(self, td, unit):
|
| 148 |
+
# The td fixture should always be far from the implementation
|
| 149 |
+
# bound, so doubling does not risk overflow.
|
| 150 |
+
res = td * 2
|
| 151 |
+
assert res._value == td._value * 2
|
| 152 |
+
assert res._creso == unit
|
| 153 |
+
|
| 154 |
+
def test_cmp_cross_reso(self, td):
|
| 155 |
+
# numpy gets this wrong because of silent overflow
|
| 156 |
+
other = Timedelta(days=106751, unit="ns")
|
| 157 |
+
assert other < td
|
| 158 |
+
assert td > other
|
| 159 |
+
assert not other == td
|
| 160 |
+
assert td != other
|
| 161 |
+
|
| 162 |
+
def test_to_pytimedelta(self, td):
|
| 163 |
+
res = td.to_pytimedelta()
|
| 164 |
+
expected = timedelta(days=106752)
|
| 165 |
+
assert type(res) is timedelta
|
| 166 |
+
assert res == expected
|
| 167 |
+
|
| 168 |
+
def test_to_timedelta64(self, td, unit):
|
| 169 |
+
for res in [td.to_timedelta64(), td.to_numpy(), td.asm8]:
|
| 170 |
+
assert isinstance(res, np.timedelta64)
|
| 171 |
+
assert res.view("i8") == td._value
|
| 172 |
+
if unit == NpyDatetimeUnit.NPY_FR_s.value:
|
| 173 |
+
assert res.dtype == "m8[s]"
|
| 174 |
+
elif unit == NpyDatetimeUnit.NPY_FR_ms.value:
|
| 175 |
+
assert res.dtype == "m8[ms]"
|
| 176 |
+
elif unit == NpyDatetimeUnit.NPY_FR_us.value:
|
| 177 |
+
assert res.dtype == "m8[us]"
|
| 178 |
+
|
| 179 |
+
def test_truediv_timedeltalike(self, td):
|
| 180 |
+
assert td / td == 1
|
| 181 |
+
assert (2.5 * td) / td == 2.5
|
| 182 |
+
|
| 183 |
+
other = Timedelta(td._value)
|
| 184 |
+
msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow."
|
| 185 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 186 |
+
td / other
|
| 187 |
+
|
| 188 |
+
# Timedelta(other.to_pytimedelta()) has microsecond resolution,
|
| 189 |
+
# so the division doesn't require casting all the way to nanos,
|
| 190 |
+
# so succeeds
|
| 191 |
+
res = other.to_pytimedelta() / td
|
| 192 |
+
expected = other.to_pytimedelta() / td.to_pytimedelta()
|
| 193 |
+
assert res == expected
|
| 194 |
+
|
| 195 |
+
# if there's no overflow, we cast to the higher reso
|
| 196 |
+
left = Timedelta._from_value_and_reso(50, NpyDatetimeUnit.NPY_FR_us.value)
|
| 197 |
+
right = Timedelta._from_value_and_reso(50, NpyDatetimeUnit.NPY_FR_ms.value)
|
| 198 |
+
result = left / right
|
| 199 |
+
assert result == 0.001
|
| 200 |
+
|
| 201 |
+
result = right / left
|
| 202 |
+
assert result == 1000
|
| 203 |
+
|
| 204 |
+
def test_truediv_numeric(self, td):
|
| 205 |
+
assert td / np.nan is NaT
|
| 206 |
+
|
| 207 |
+
res = td / 2
|
| 208 |
+
assert res._value == td._value / 2
|
| 209 |
+
assert res._creso == td._creso
|
| 210 |
+
|
| 211 |
+
res = td / 2.0
|
| 212 |
+
assert res._value == td._value / 2
|
| 213 |
+
assert res._creso == td._creso
|
| 214 |
+
|
| 215 |
+
def test_floordiv_timedeltalike(self, td):
|
| 216 |
+
assert td // td == 1
|
| 217 |
+
assert (2.5 * td) // td == 2
|
| 218 |
+
|
| 219 |
+
other = Timedelta(td._value)
|
| 220 |
+
msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow"
|
| 221 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 222 |
+
td // other
|
| 223 |
+
|
| 224 |
+
# Timedelta(other.to_pytimedelta()) has microsecond resolution,
|
| 225 |
+
# so the floordiv doesn't require casting all the way to nanos,
|
| 226 |
+
# so succeeds
|
| 227 |
+
res = other.to_pytimedelta() // td
|
| 228 |
+
assert res == 0
|
| 229 |
+
|
| 230 |
+
# if there's no overflow, we cast to the higher reso
|
| 231 |
+
left = Timedelta._from_value_and_reso(50050, NpyDatetimeUnit.NPY_FR_us.value)
|
| 232 |
+
right = Timedelta._from_value_and_reso(50, NpyDatetimeUnit.NPY_FR_ms.value)
|
| 233 |
+
result = left // right
|
| 234 |
+
assert result == 1
|
| 235 |
+
result = right // left
|
| 236 |
+
assert result == 0
|
| 237 |
+
|
| 238 |
+
def test_floordiv_numeric(self, td):
|
| 239 |
+
assert td // np.nan is NaT
|
| 240 |
+
|
| 241 |
+
res = td // 2
|
| 242 |
+
assert res._value == td._value // 2
|
| 243 |
+
assert res._creso == td._creso
|
| 244 |
+
|
| 245 |
+
res = td // 2.0
|
| 246 |
+
assert res._value == td._value // 2
|
| 247 |
+
assert res._creso == td._creso
|
| 248 |
+
|
| 249 |
+
assert td // np.array(np.nan) is NaT
|
| 250 |
+
|
| 251 |
+
res = td // np.array(2)
|
| 252 |
+
assert res._value == td._value // 2
|
| 253 |
+
assert res._creso == td._creso
|
| 254 |
+
|
| 255 |
+
res = td // np.array(2.0)
|
| 256 |
+
assert res._value == td._value // 2
|
| 257 |
+
assert res._creso == td._creso
|
| 258 |
+
|
| 259 |
+
def test_addsub_mismatched_reso(self, td):
|
| 260 |
+
# need to cast to since td is out of bounds for ns, so
|
| 261 |
+
# so we would raise OverflowError without casting
|
| 262 |
+
other = Timedelta(days=1).as_unit("us")
|
| 263 |
+
|
| 264 |
+
# td is out of bounds for ns
|
| 265 |
+
result = td + other
|
| 266 |
+
assert result._creso == other._creso
|
| 267 |
+
assert result.days == td.days + 1
|
| 268 |
+
|
| 269 |
+
result = other + td
|
| 270 |
+
assert result._creso == other._creso
|
| 271 |
+
assert result.days == td.days + 1
|
| 272 |
+
|
| 273 |
+
result = td - other
|
| 274 |
+
assert result._creso == other._creso
|
| 275 |
+
assert result.days == td.days - 1
|
| 276 |
+
|
| 277 |
+
result = other - td
|
| 278 |
+
assert result._creso == other._creso
|
| 279 |
+
assert result.days == 1 - td.days
|
| 280 |
+
|
| 281 |
+
other2 = Timedelta(500)
|
| 282 |
+
msg = "Cannot cast 106752 days 00:00:00 to unit='ns' without overflow"
|
| 283 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 284 |
+
td + other2
|
| 285 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 286 |
+
other2 + td
|
| 287 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 288 |
+
td - other2
|
| 289 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 290 |
+
other2 - td
|
| 291 |
+
|
| 292 |
+
def test_min(self, td):
|
| 293 |
+
assert td.min <= td
|
| 294 |
+
assert td.min._creso == td._creso
|
| 295 |
+
assert td.min._value == NaT._value + 1
|
| 296 |
+
|
| 297 |
+
def test_max(self, td):
|
| 298 |
+
assert td.max >= td
|
| 299 |
+
assert td.max._creso == td._creso
|
| 300 |
+
assert td.max._value == np.iinfo(np.int64).max
|
| 301 |
+
|
| 302 |
+
def test_resolution(self, td):
|
| 303 |
+
expected = Timedelta._from_value_and_reso(1, td._creso)
|
| 304 |
+
result = td.resolution
|
| 305 |
+
assert result == expected
|
| 306 |
+
assert result._creso == expected._creso
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def test_timedelta_class_min_max_resolution():
|
| 310 |
+
# when accessed on the class (as opposed to an instance), we default
|
| 311 |
+
# to nanoseconds
|
| 312 |
+
assert Timedelta.min == Timedelta(NaT._value + 1)
|
| 313 |
+
assert Timedelta.min._creso == NpyDatetimeUnit.NPY_FR_ns.value
|
| 314 |
+
|
| 315 |
+
assert Timedelta.max == Timedelta(np.iinfo(np.int64).max)
|
| 316 |
+
assert Timedelta.max._creso == NpyDatetimeUnit.NPY_FR_ns.value
|
| 317 |
+
|
| 318 |
+
assert Timedelta.resolution == Timedelta(1)
|
| 319 |
+
assert Timedelta.resolution._creso == NpyDatetimeUnit.NPY_FR_ns.value
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
class TestTimedeltaUnaryOps:
|
| 323 |
+
def test_invert(self):
|
| 324 |
+
td = Timedelta(10, unit="d")
|
| 325 |
+
|
| 326 |
+
msg = "bad operand type for unary ~"
|
| 327 |
+
with pytest.raises(TypeError, match=msg):
|
| 328 |
+
~td
|
| 329 |
+
|
| 330 |
+
# check this matches pytimedelta and timedelta64
|
| 331 |
+
with pytest.raises(TypeError, match=msg):
|
| 332 |
+
~(td.to_pytimedelta())
|
| 333 |
+
|
| 334 |
+
umsg = "ufunc 'invert' not supported for the input types"
|
| 335 |
+
with pytest.raises(TypeError, match=umsg):
|
| 336 |
+
~(td.to_timedelta64())
|
| 337 |
+
|
| 338 |
+
def test_unary_ops(self):
|
| 339 |
+
td = Timedelta(10, unit="d")
|
| 340 |
+
|
| 341 |
+
# __neg__, __pos__
|
| 342 |
+
assert -td == Timedelta(-10, unit="d")
|
| 343 |
+
assert -td == Timedelta("-10d")
|
| 344 |
+
assert +td == Timedelta(10, unit="d")
|
| 345 |
+
|
| 346 |
+
# __abs__, __abs__(__neg__)
|
| 347 |
+
assert abs(td) == td
|
| 348 |
+
assert abs(-td) == td
|
| 349 |
+
assert abs(-td) == Timedelta("10d")
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
class TestTimedeltas:
|
| 353 |
+
@pytest.mark.parametrize(
|
| 354 |
+
"unit, value, expected",
|
| 355 |
+
[
|
| 356 |
+
("us", 9.999, 9999),
|
| 357 |
+
("ms", 9.999999, 9999999),
|
| 358 |
+
("s", 9.999999999, 9999999999),
|
| 359 |
+
],
|
| 360 |
+
)
|
| 361 |
+
def test_rounding_on_int_unit_construction(self, unit, value, expected):
|
| 362 |
+
# GH 12690
|
| 363 |
+
result = Timedelta(value, unit=unit)
|
| 364 |
+
assert result._value == expected
|
| 365 |
+
result = Timedelta(str(value) + unit)
|
| 366 |
+
assert result._value == expected
|
| 367 |
+
|
| 368 |
+
def test_total_seconds_scalar(self):
|
| 369 |
+
# see gh-10939
|
| 370 |
+
rng = Timedelta("1 days, 10:11:12.100123456")
|
| 371 |
+
expt = 1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456.0 / 1e9
|
| 372 |
+
tm.assert_almost_equal(rng.total_seconds(), expt)
|
| 373 |
+
|
| 374 |
+
rng = Timedelta(np.nan)
|
| 375 |
+
assert np.isnan(rng.total_seconds())
|
| 376 |
+
|
| 377 |
+
def test_conversion(self):
|
| 378 |
+
for td in [Timedelta(10, unit="d"), Timedelta("1 days, 10:11:12.012345")]:
|
| 379 |
+
pydt = td.to_pytimedelta()
|
| 380 |
+
assert td == Timedelta(pydt)
|
| 381 |
+
assert td == pydt
|
| 382 |
+
assert isinstance(pydt, timedelta) and not isinstance(pydt, Timedelta)
|
| 383 |
+
|
| 384 |
+
assert td == np.timedelta64(td._value, "ns")
|
| 385 |
+
td64 = td.to_timedelta64()
|
| 386 |
+
|
| 387 |
+
assert td64 == np.timedelta64(td._value, "ns")
|
| 388 |
+
assert td == td64
|
| 389 |
+
|
| 390 |
+
assert isinstance(td64, np.timedelta64)
|
| 391 |
+
|
| 392 |
+
# this is NOT equal and cannot be roundtripped (because of the nanos)
|
| 393 |
+
td = Timedelta("1 days, 10:11:12.012345678")
|
| 394 |
+
assert td != td.to_pytimedelta()
|
| 395 |
+
|
| 396 |
+
def test_fields(self):
|
| 397 |
+
def check(value):
|
| 398 |
+
# that we are int
|
| 399 |
+
assert isinstance(value, int)
|
| 400 |
+
|
| 401 |
+
# compat to datetime.timedelta
|
| 402 |
+
rng = to_timedelta("1 days, 10:11:12")
|
| 403 |
+
assert rng.days == 1
|
| 404 |
+
assert rng.seconds == 10 * 3600 + 11 * 60 + 12
|
| 405 |
+
assert rng.microseconds == 0
|
| 406 |
+
assert rng.nanoseconds == 0
|
| 407 |
+
|
| 408 |
+
msg = "'Timedelta' object has no attribute '{}'"
|
| 409 |
+
with pytest.raises(AttributeError, match=msg.format("hours")):
|
| 410 |
+
rng.hours
|
| 411 |
+
with pytest.raises(AttributeError, match=msg.format("minutes")):
|
| 412 |
+
rng.minutes
|
| 413 |
+
with pytest.raises(AttributeError, match=msg.format("milliseconds")):
|
| 414 |
+
rng.milliseconds
|
| 415 |
+
|
| 416 |
+
# GH 10050
|
| 417 |
+
check(rng.days)
|
| 418 |
+
check(rng.seconds)
|
| 419 |
+
check(rng.microseconds)
|
| 420 |
+
check(rng.nanoseconds)
|
| 421 |
+
|
| 422 |
+
td = Timedelta("-1 days, 10:11:12")
|
| 423 |
+
assert abs(td) == Timedelta("13:48:48")
|
| 424 |
+
assert str(td) == "-1 days +10:11:12"
|
| 425 |
+
assert -td == Timedelta("0 days 13:48:48")
|
| 426 |
+
assert -Timedelta("-1 days, 10:11:12")._value == 49728000000000
|
| 427 |
+
assert Timedelta("-1 days, 10:11:12")._value == -49728000000000
|
| 428 |
+
|
| 429 |
+
rng = to_timedelta("-1 days, 10:11:12.100123456")
|
| 430 |
+
assert rng.days == -1
|
| 431 |
+
assert rng.seconds == 10 * 3600 + 11 * 60 + 12
|
| 432 |
+
assert rng.microseconds == 100 * 1000 + 123
|
| 433 |
+
assert rng.nanoseconds == 456
|
| 434 |
+
msg = "'Timedelta' object has no attribute '{}'"
|
| 435 |
+
with pytest.raises(AttributeError, match=msg.format("hours")):
|
| 436 |
+
rng.hours
|
| 437 |
+
with pytest.raises(AttributeError, match=msg.format("minutes")):
|
| 438 |
+
rng.minutes
|
| 439 |
+
with pytest.raises(AttributeError, match=msg.format("milliseconds")):
|
| 440 |
+
rng.milliseconds
|
| 441 |
+
|
| 442 |
+
# components
|
| 443 |
+
tup = to_timedelta(-1, "us").components
|
| 444 |
+
assert tup.days == -1
|
| 445 |
+
assert tup.hours == 23
|
| 446 |
+
assert tup.minutes == 59
|
| 447 |
+
assert tup.seconds == 59
|
| 448 |
+
assert tup.milliseconds == 999
|
| 449 |
+
assert tup.microseconds == 999
|
| 450 |
+
assert tup.nanoseconds == 0
|
| 451 |
+
|
| 452 |
+
# GH 10050
|
| 453 |
+
check(tup.days)
|
| 454 |
+
check(tup.hours)
|
| 455 |
+
check(tup.minutes)
|
| 456 |
+
check(tup.seconds)
|
| 457 |
+
check(tup.milliseconds)
|
| 458 |
+
check(tup.microseconds)
|
| 459 |
+
check(tup.nanoseconds)
|
| 460 |
+
|
| 461 |
+
tup = Timedelta("-1 days 1 us").components
|
| 462 |
+
assert tup.days == -2
|
| 463 |
+
assert tup.hours == 23
|
| 464 |
+
assert tup.minutes == 59
|
| 465 |
+
assert tup.seconds == 59
|
| 466 |
+
assert tup.milliseconds == 999
|
| 467 |
+
assert tup.microseconds == 999
|
| 468 |
+
assert tup.nanoseconds == 0
|
| 469 |
+
|
| 470 |
+
def test_iso_conversion(self):
|
| 471 |
+
# GH #21877
|
| 472 |
+
expected = Timedelta(1, unit="s")
|
| 473 |
+
assert to_timedelta("P0DT0H0M1S") == expected
|
| 474 |
+
|
| 475 |
+
def test_nat_converters(self):
|
| 476 |
+
result = to_timedelta("nat").to_numpy()
|
| 477 |
+
assert result.dtype.kind == "M"
|
| 478 |
+
assert result.astype("int64") == iNaT
|
| 479 |
+
|
| 480 |
+
result = to_timedelta("nan").to_numpy()
|
| 481 |
+
assert result.dtype.kind == "M"
|
| 482 |
+
assert result.astype("int64") == iNaT
|
| 483 |
+
|
| 484 |
+
@pytest.mark.parametrize(
|
| 485 |
+
"unit, np_unit",
|
| 486 |
+
[(value, "W") for value in ["W", "w"]]
|
| 487 |
+
+ [(value, "D") for value in ["D", "d", "days", "day", "Days", "Day"]]
|
| 488 |
+
+ [
|
| 489 |
+
(value, "m")
|
| 490 |
+
for value in [
|
| 491 |
+
"m",
|
| 492 |
+
"minute",
|
| 493 |
+
"min",
|
| 494 |
+
"minutes",
|
| 495 |
+
"t",
|
| 496 |
+
"Minute",
|
| 497 |
+
"Min",
|
| 498 |
+
"Minutes",
|
| 499 |
+
"T",
|
| 500 |
+
]
|
| 501 |
+
]
|
| 502 |
+
+ [
|
| 503 |
+
(value, "s")
|
| 504 |
+
for value in [
|
| 505 |
+
"s",
|
| 506 |
+
"seconds",
|
| 507 |
+
"sec",
|
| 508 |
+
"second",
|
| 509 |
+
"S",
|
| 510 |
+
"Seconds",
|
| 511 |
+
"Sec",
|
| 512 |
+
"Second",
|
| 513 |
+
]
|
| 514 |
+
]
|
| 515 |
+
+ [
|
| 516 |
+
(value, "ms")
|
| 517 |
+
for value in [
|
| 518 |
+
"ms",
|
| 519 |
+
"milliseconds",
|
| 520 |
+
"millisecond",
|
| 521 |
+
"milli",
|
| 522 |
+
"millis",
|
| 523 |
+
"l",
|
| 524 |
+
"MS",
|
| 525 |
+
"Milliseconds",
|
| 526 |
+
"Millisecond",
|
| 527 |
+
"Milli",
|
| 528 |
+
"Millis",
|
| 529 |
+
"L",
|
| 530 |
+
]
|
| 531 |
+
]
|
| 532 |
+
+ [
|
| 533 |
+
(value, "us")
|
| 534 |
+
for value in [
|
| 535 |
+
"us",
|
| 536 |
+
"microseconds",
|
| 537 |
+
"microsecond",
|
| 538 |
+
"micro",
|
| 539 |
+
"micros",
|
| 540 |
+
"u",
|
| 541 |
+
"US",
|
| 542 |
+
"Microseconds",
|
| 543 |
+
"Microsecond",
|
| 544 |
+
"Micro",
|
| 545 |
+
"Micros",
|
| 546 |
+
"U",
|
| 547 |
+
]
|
| 548 |
+
]
|
| 549 |
+
+ [
|
| 550 |
+
(value, "ns")
|
| 551 |
+
for value in [
|
| 552 |
+
"ns",
|
| 553 |
+
"nanoseconds",
|
| 554 |
+
"nanosecond",
|
| 555 |
+
"nano",
|
| 556 |
+
"nanos",
|
| 557 |
+
"n",
|
| 558 |
+
"NS",
|
| 559 |
+
"Nanoseconds",
|
| 560 |
+
"Nanosecond",
|
| 561 |
+
"Nano",
|
| 562 |
+
"Nanos",
|
| 563 |
+
"N",
|
| 564 |
+
]
|
| 565 |
+
],
|
| 566 |
+
)
|
| 567 |
+
@pytest.mark.parametrize("wrapper", [np.array, list, pd.Index])
|
| 568 |
+
def test_unit_parser(self, unit, np_unit, wrapper):
|
| 569 |
+
# validate all units, GH 6855, GH 21762
|
| 570 |
+
# array-likes
|
| 571 |
+
expected = TimedeltaIndex(
|
| 572 |
+
[np.timedelta64(i, np_unit) for i in np.arange(5).tolist()],
|
| 573 |
+
dtype="m8[ns]",
|
| 574 |
+
)
|
| 575 |
+
# TODO(2.0): the desired output dtype may have non-nano resolution
|
| 576 |
+
result = to_timedelta(wrapper(range(5)), unit=unit)
|
| 577 |
+
tm.assert_index_equal(result, expected)
|
| 578 |
+
result = TimedeltaIndex(wrapper(range(5)), unit=unit)
|
| 579 |
+
tm.assert_index_equal(result, expected)
|
| 580 |
+
|
| 581 |
+
str_repr = [f"{x}{unit}" for x in np.arange(5)]
|
| 582 |
+
result = to_timedelta(wrapper(str_repr))
|
| 583 |
+
tm.assert_index_equal(result, expected)
|
| 584 |
+
result = to_timedelta(wrapper(str_repr))
|
| 585 |
+
tm.assert_index_equal(result, expected)
|
| 586 |
+
|
| 587 |
+
# scalar
|
| 588 |
+
expected = Timedelta(np.timedelta64(2, np_unit).astype("timedelta64[ns]"))
|
| 589 |
+
result = to_timedelta(2, unit=unit)
|
| 590 |
+
assert result == expected
|
| 591 |
+
result = Timedelta(2, unit=unit)
|
| 592 |
+
assert result == expected
|
| 593 |
+
|
| 594 |
+
result = to_timedelta(f"2{unit}")
|
| 595 |
+
assert result == expected
|
| 596 |
+
result = Timedelta(f"2{unit}")
|
| 597 |
+
assert result == expected
|
| 598 |
+
|
| 599 |
+
@pytest.mark.parametrize("unit", ["Y", "y", "M"])
|
| 600 |
+
def test_unit_m_y_raises(self, unit):
|
| 601 |
+
msg = "Units 'M', 'Y', and 'y' are no longer supported"
|
| 602 |
+
with pytest.raises(ValueError, match=msg):
|
| 603 |
+
Timedelta(10, unit)
|
| 604 |
+
|
| 605 |
+
with pytest.raises(ValueError, match=msg):
|
| 606 |
+
to_timedelta(10, unit)
|
| 607 |
+
|
| 608 |
+
with pytest.raises(ValueError, match=msg):
|
| 609 |
+
to_timedelta([1, 2], unit)
|
| 610 |
+
|
| 611 |
+
def test_numeric_conversions(self):
|
| 612 |
+
assert Timedelta(0) == np.timedelta64(0, "ns")
|
| 613 |
+
assert Timedelta(10) == np.timedelta64(10, "ns")
|
| 614 |
+
assert Timedelta(10, unit="ns") == np.timedelta64(10, "ns")
|
| 615 |
+
|
| 616 |
+
assert Timedelta(10, unit="us") == np.timedelta64(10, "us")
|
| 617 |
+
assert Timedelta(10, unit="ms") == np.timedelta64(10, "ms")
|
| 618 |
+
assert Timedelta(10, unit="s") == np.timedelta64(10, "s")
|
| 619 |
+
assert Timedelta(10, unit="d") == np.timedelta64(10, "D")
|
| 620 |
+
|
| 621 |
+
def test_timedelta_conversions(self):
|
| 622 |
+
assert Timedelta(timedelta(seconds=1)) == np.timedelta64(1, "s").astype(
|
| 623 |
+
"m8[ns]"
|
| 624 |
+
)
|
| 625 |
+
assert Timedelta(timedelta(microseconds=1)) == np.timedelta64(1, "us").astype(
|
| 626 |
+
"m8[ns]"
|
| 627 |
+
)
|
| 628 |
+
assert Timedelta(timedelta(days=1)) == np.timedelta64(1, "D").astype("m8[ns]")
|
| 629 |
+
|
| 630 |
+
def test_to_numpy_alias(self):
|
| 631 |
+
# GH 24653: alias .to_numpy() for scalars
|
| 632 |
+
td = Timedelta("10m7s")
|
| 633 |
+
assert td.to_timedelta64() == td.to_numpy()
|
| 634 |
+
|
| 635 |
+
# GH#44460
|
| 636 |
+
msg = "dtype and copy arguments are ignored"
|
| 637 |
+
with pytest.raises(ValueError, match=msg):
|
| 638 |
+
td.to_numpy("m8[s]")
|
| 639 |
+
with pytest.raises(ValueError, match=msg):
|
| 640 |
+
td.to_numpy(copy=True)
|
| 641 |
+
|
| 642 |
+
@pytest.mark.parametrize(
|
| 643 |
+
"freq,s1,s2",
|
| 644 |
+
[
|
| 645 |
+
# This first case has s1, s2 being the same as t1,t2 below
|
| 646 |
+
(
|
| 647 |
+
"N",
|
| 648 |
+
Timedelta("1 days 02:34:56.789123456"),
|
| 649 |
+
Timedelta("-1 days 02:34:56.789123456"),
|
| 650 |
+
),
|
| 651 |
+
(
|
| 652 |
+
"U",
|
| 653 |
+
Timedelta("1 days 02:34:56.789123000"),
|
| 654 |
+
Timedelta("-1 days 02:34:56.789123000"),
|
| 655 |
+
),
|
| 656 |
+
(
|
| 657 |
+
"L",
|
| 658 |
+
Timedelta("1 days 02:34:56.789000000"),
|
| 659 |
+
Timedelta("-1 days 02:34:56.789000000"),
|
| 660 |
+
),
|
| 661 |
+
("S", Timedelta("1 days 02:34:57"), Timedelta("-1 days 02:34:57")),
|
| 662 |
+
("2S", Timedelta("1 days 02:34:56"), Timedelta("-1 days 02:34:56")),
|
| 663 |
+
("5S", Timedelta("1 days 02:34:55"), Timedelta("-1 days 02:34:55")),
|
| 664 |
+
("T", Timedelta("1 days 02:35:00"), Timedelta("-1 days 02:35:00")),
|
| 665 |
+
("12T", Timedelta("1 days 02:36:00"), Timedelta("-1 days 02:36:00")),
|
| 666 |
+
("H", Timedelta("1 days 03:00:00"), Timedelta("-1 days 03:00:00")),
|
| 667 |
+
("d", Timedelta("1 days"), Timedelta("-1 days")),
|
| 668 |
+
],
|
| 669 |
+
)
|
| 670 |
+
def test_round(self, freq, s1, s2):
|
| 671 |
+
t1 = Timedelta("1 days 02:34:56.789123456")
|
| 672 |
+
t2 = Timedelta("-1 days 02:34:56.789123456")
|
| 673 |
+
|
| 674 |
+
r1 = t1.round(freq)
|
| 675 |
+
assert r1 == s1
|
| 676 |
+
r2 = t2.round(freq)
|
| 677 |
+
assert r2 == s2
|
| 678 |
+
|
| 679 |
+
def test_round_invalid(self):
|
| 680 |
+
t1 = Timedelta("1 days 02:34:56.789123456")
|
| 681 |
+
|
| 682 |
+
for freq, msg in [
|
| 683 |
+
("Y", "<YearEnd: month=12> is a non-fixed frequency"),
|
| 684 |
+
("M", "<MonthEnd> is a non-fixed frequency"),
|
| 685 |
+
("foobar", "Invalid frequency: foobar"),
|
| 686 |
+
]:
|
| 687 |
+
with pytest.raises(ValueError, match=msg):
|
| 688 |
+
t1.round(freq)
|
| 689 |
+
|
| 690 |
+
def test_round_implementation_bounds(self):
|
| 691 |
+
# See also: analogous test for Timestamp
|
| 692 |
+
# GH#38964
|
| 693 |
+
result = Timedelta.min.ceil("s")
|
| 694 |
+
expected = Timedelta.min + Timedelta(seconds=1) - Timedelta(145224193)
|
| 695 |
+
assert result == expected
|
| 696 |
+
|
| 697 |
+
result = Timedelta.max.floor("s")
|
| 698 |
+
expected = Timedelta.max - Timedelta(854775807)
|
| 699 |
+
assert result == expected
|
| 700 |
+
|
| 701 |
+
with pytest.raises(OverflowError, match="value too large"):
|
| 702 |
+
Timedelta.min.floor("s")
|
| 703 |
+
|
| 704 |
+
# the second message here shows up in windows builds
|
| 705 |
+
msg = "|".join(
|
| 706 |
+
["Python int too large to convert to C long", "int too big to convert"]
|
| 707 |
+
)
|
| 708 |
+
with pytest.raises(OverflowError, match=msg):
|
| 709 |
+
Timedelta.max.ceil("s")
|
| 710 |
+
|
| 711 |
+
@pytest.mark.xfail(reason="Failing on builds", strict=False)
|
| 712 |
+
@given(val=st.integers(min_value=iNaT + 1, max_value=lib.i8max))
|
| 713 |
+
@pytest.mark.parametrize(
|
| 714 |
+
"method", [Timedelta.round, Timedelta.floor, Timedelta.ceil]
|
| 715 |
+
)
|
| 716 |
+
def test_round_sanity(self, val, method):
|
| 717 |
+
val = np.int64(val)
|
| 718 |
+
td = Timedelta(val)
|
| 719 |
+
|
| 720 |
+
assert method(td, "ns") == td
|
| 721 |
+
|
| 722 |
+
res = method(td, "us")
|
| 723 |
+
nanos = 1000
|
| 724 |
+
assert np.abs((res - td).value) < nanos
|
| 725 |
+
assert res.value % nanos == 0
|
| 726 |
+
|
| 727 |
+
res = method(td, "ms")
|
| 728 |
+
nanos = 1_000_000
|
| 729 |
+
assert np.abs((res - td).value) < nanos
|
| 730 |
+
assert res.value % nanos == 0
|
| 731 |
+
|
| 732 |
+
res = method(td, "s")
|
| 733 |
+
nanos = 1_000_000_000
|
| 734 |
+
assert np.abs((res - td).value) < nanos
|
| 735 |
+
assert res.value % nanos == 0
|
| 736 |
+
|
| 737 |
+
res = method(td, "min")
|
| 738 |
+
nanos = 60 * 1_000_000_000
|
| 739 |
+
assert np.abs((res - td).value) < nanos
|
| 740 |
+
assert res.value % nanos == 0
|
| 741 |
+
|
| 742 |
+
res = method(td, "h")
|
| 743 |
+
nanos = 60 * 60 * 1_000_000_000
|
| 744 |
+
assert np.abs((res - td).value) < nanos
|
| 745 |
+
assert res.value % nanos == 0
|
| 746 |
+
|
| 747 |
+
res = method(td, "D")
|
| 748 |
+
nanos = 24 * 60 * 60 * 1_000_000_000
|
| 749 |
+
assert np.abs((res - td).value) < nanos
|
| 750 |
+
assert res.value % nanos == 0
|
| 751 |
+
|
| 752 |
+
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s"])
|
| 753 |
+
def test_round_non_nano(self, unit):
|
| 754 |
+
td = Timedelta("1 days 02:34:57").as_unit(unit)
|
| 755 |
+
|
| 756 |
+
res = td.round("min")
|
| 757 |
+
assert res == Timedelta("1 days 02:35:00")
|
| 758 |
+
assert res._creso == td._creso
|
| 759 |
+
|
| 760 |
+
res = td.floor("min")
|
| 761 |
+
assert res == Timedelta("1 days 02:34:00")
|
| 762 |
+
assert res._creso == td._creso
|
| 763 |
+
|
| 764 |
+
res = td.ceil("min")
|
| 765 |
+
assert res == Timedelta("1 days 02:35:00")
|
| 766 |
+
assert res._creso == td._creso
|
| 767 |
+
|
| 768 |
+
def test_identity(self):
|
| 769 |
+
td = Timedelta(10, unit="d")
|
| 770 |
+
assert isinstance(td, Timedelta)
|
| 771 |
+
assert isinstance(td, timedelta)
|
| 772 |
+
|
| 773 |
+
def test_short_format_converters(self):
|
| 774 |
+
def conv(v):
|
| 775 |
+
return v.astype("m8[ns]")
|
| 776 |
+
|
| 777 |
+
assert Timedelta("10") == np.timedelta64(10, "ns")
|
| 778 |
+
assert Timedelta("10ns") == np.timedelta64(10, "ns")
|
| 779 |
+
assert Timedelta("100") == np.timedelta64(100, "ns")
|
| 780 |
+
assert Timedelta("100ns") == np.timedelta64(100, "ns")
|
| 781 |
+
|
| 782 |
+
assert Timedelta("1000") == np.timedelta64(1000, "ns")
|
| 783 |
+
assert Timedelta("1000ns") == np.timedelta64(1000, "ns")
|
| 784 |
+
assert Timedelta("1000NS") == np.timedelta64(1000, "ns")
|
| 785 |
+
|
| 786 |
+
assert Timedelta("10us") == np.timedelta64(10000, "ns")
|
| 787 |
+
assert Timedelta("100us") == np.timedelta64(100000, "ns")
|
| 788 |
+
assert Timedelta("1000us") == np.timedelta64(1000000, "ns")
|
| 789 |
+
assert Timedelta("1000Us") == np.timedelta64(1000000, "ns")
|
| 790 |
+
assert Timedelta("1000uS") == np.timedelta64(1000000, "ns")
|
| 791 |
+
|
| 792 |
+
assert Timedelta("1ms") == np.timedelta64(1000000, "ns")
|
| 793 |
+
assert Timedelta("10ms") == np.timedelta64(10000000, "ns")
|
| 794 |
+
assert Timedelta("100ms") == np.timedelta64(100000000, "ns")
|
| 795 |
+
assert Timedelta("1000ms") == np.timedelta64(1000000000, "ns")
|
| 796 |
+
|
| 797 |
+
assert Timedelta("-1s") == -np.timedelta64(1000000000, "ns")
|
| 798 |
+
assert Timedelta("1s") == np.timedelta64(1000000000, "ns")
|
| 799 |
+
assert Timedelta("10s") == np.timedelta64(10000000000, "ns")
|
| 800 |
+
assert Timedelta("100s") == np.timedelta64(100000000000, "ns")
|
| 801 |
+
assert Timedelta("1000s") == np.timedelta64(1000000000000, "ns")
|
| 802 |
+
|
| 803 |
+
assert Timedelta("1d") == conv(np.timedelta64(1, "D"))
|
| 804 |
+
assert Timedelta("-1d") == -conv(np.timedelta64(1, "D"))
|
| 805 |
+
assert Timedelta("1D") == conv(np.timedelta64(1, "D"))
|
| 806 |
+
assert Timedelta("10D") == conv(np.timedelta64(10, "D"))
|
| 807 |
+
assert Timedelta("100D") == conv(np.timedelta64(100, "D"))
|
| 808 |
+
assert Timedelta("1000D") == conv(np.timedelta64(1000, "D"))
|
| 809 |
+
assert Timedelta("10000D") == conv(np.timedelta64(10000, "D"))
|
| 810 |
+
|
| 811 |
+
# space
|
| 812 |
+
assert Timedelta(" 10000D ") == conv(np.timedelta64(10000, "D"))
|
| 813 |
+
assert Timedelta(" - 10000D ") == -conv(np.timedelta64(10000, "D"))
|
| 814 |
+
|
| 815 |
+
# invalid
|
| 816 |
+
msg = "invalid unit abbreviation"
|
| 817 |
+
with pytest.raises(ValueError, match=msg):
|
| 818 |
+
Timedelta("1foo")
|
| 819 |
+
msg = "unit abbreviation w/o a number"
|
| 820 |
+
with pytest.raises(ValueError, match=msg):
|
| 821 |
+
Timedelta("foo")
|
| 822 |
+
|
| 823 |
+
def test_full_format_converters(self):
|
| 824 |
+
def conv(v):
|
| 825 |
+
return v.astype("m8[ns]")
|
| 826 |
+
|
| 827 |
+
d1 = np.timedelta64(1, "D")
|
| 828 |
+
|
| 829 |
+
assert Timedelta("1days") == conv(d1)
|
| 830 |
+
assert Timedelta("1days,") == conv(d1)
|
| 831 |
+
assert Timedelta("- 1days,") == -conv(d1)
|
| 832 |
+
|
| 833 |
+
assert Timedelta("00:00:01") == conv(np.timedelta64(1, "s"))
|
| 834 |
+
assert Timedelta("06:00:01") == conv(np.timedelta64(6 * 3600 + 1, "s"))
|
| 835 |
+
assert Timedelta("06:00:01.0") == conv(np.timedelta64(6 * 3600 + 1, "s"))
|
| 836 |
+
assert Timedelta("06:00:01.01") == conv(
|
| 837 |
+
np.timedelta64(1000 * (6 * 3600 + 1) + 10, "ms")
|
| 838 |
+
)
|
| 839 |
+
|
| 840 |
+
assert Timedelta("- 1days, 00:00:01") == conv(-d1 + np.timedelta64(1, "s"))
|
| 841 |
+
assert Timedelta("1days, 06:00:01") == conv(
|
| 842 |
+
d1 + np.timedelta64(6 * 3600 + 1, "s")
|
| 843 |
+
)
|
| 844 |
+
assert Timedelta("1days, 06:00:01.01") == conv(
|
| 845 |
+
d1 + np.timedelta64(1000 * (6 * 3600 + 1) + 10, "ms")
|
| 846 |
+
)
|
| 847 |
+
|
| 848 |
+
# invalid
|
| 849 |
+
msg = "have leftover units"
|
| 850 |
+
with pytest.raises(ValueError, match=msg):
|
| 851 |
+
Timedelta("- 1days, 00")
|
| 852 |
+
|
| 853 |
+
def test_pickle(self):
|
| 854 |
+
v = Timedelta("1 days 10:11:12.0123456")
|
| 855 |
+
v_p = tm.round_trip_pickle(v)
|
| 856 |
+
assert v == v_p
|
| 857 |
+
|
| 858 |
+
def test_timedelta_hash_equality(self):
|
| 859 |
+
# GH 11129
|
| 860 |
+
v = Timedelta(1, "D")
|
| 861 |
+
td = timedelta(days=1)
|
| 862 |
+
assert hash(v) == hash(td)
|
| 863 |
+
|
| 864 |
+
d = {td: 2}
|
| 865 |
+
assert d[v] == 2
|
| 866 |
+
|
| 867 |
+
tds = [Timedelta(seconds=1) + Timedelta(days=n) for n in range(20)]
|
| 868 |
+
assert all(hash(td) == hash(td.to_pytimedelta()) for td in tds)
|
| 869 |
+
|
| 870 |
+
# python timedeltas drop ns resolution
|
| 871 |
+
ns_td = Timedelta(1, "ns")
|
| 872 |
+
assert hash(ns_td) != hash(ns_td.to_pytimedelta())
|
| 873 |
+
|
| 874 |
+
def test_implementation_limits(self):
|
| 875 |
+
min_td = Timedelta(Timedelta.min)
|
| 876 |
+
max_td = Timedelta(Timedelta.max)
|
| 877 |
+
|
| 878 |
+
# GH 12727
|
| 879 |
+
# timedelta limits correspond to int64 boundaries
|
| 880 |
+
assert min_td._value == iNaT + 1
|
| 881 |
+
assert max_td._value == lib.i8max
|
| 882 |
+
|
| 883 |
+
# Beyond lower limit, a NAT before the Overflow
|
| 884 |
+
assert (min_td - Timedelta(1, "ns")) is NaT
|
| 885 |
+
|
| 886 |
+
msg = "int too (large|big) to convert"
|
| 887 |
+
with pytest.raises(OverflowError, match=msg):
|
| 888 |
+
min_td - Timedelta(2, "ns")
|
| 889 |
+
|
| 890 |
+
with pytest.raises(OverflowError, match=msg):
|
| 891 |
+
max_td + Timedelta(1, "ns")
|
| 892 |
+
|
| 893 |
+
# Same tests using the internal nanosecond values
|
| 894 |
+
td = Timedelta(min_td._value - 1, "ns")
|
| 895 |
+
assert td is NaT
|
| 896 |
+
|
| 897 |
+
msg = "Cannot cast -9223372036854775809 from ns to 'ns' without overflow"
|
| 898 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 899 |
+
Timedelta(min_td._value - 2, "ns")
|
| 900 |
+
|
| 901 |
+
msg = "Cannot cast 9223372036854775808 from ns to 'ns' without overflow"
|
| 902 |
+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
|
| 903 |
+
Timedelta(max_td._value + 1, "ns")
|
| 904 |
+
|
| 905 |
+
def test_total_seconds_precision(self):
|
| 906 |
+
# GH 19458
|
| 907 |
+
assert Timedelta("30S").total_seconds() == 30.0
|
| 908 |
+
assert Timedelta("0").total_seconds() == 0.0
|
| 909 |
+
assert Timedelta("-2S").total_seconds() == -2.0
|
| 910 |
+
assert Timedelta("5.324S").total_seconds() == 5.324
|
| 911 |
+
assert (Timedelta("30S").total_seconds() - 30.0) < 1e-20
|
| 912 |
+
assert (30.0 - Timedelta("30S").total_seconds()) < 1e-20
|
| 913 |
+
|
| 914 |
+
def test_resolution_string(self):
|
| 915 |
+
assert Timedelta(days=1).resolution_string == "D"
|
| 916 |
+
assert Timedelta(days=1, hours=6).resolution_string == "H"
|
| 917 |
+
assert Timedelta(days=1, minutes=6).resolution_string == "T"
|
| 918 |
+
assert Timedelta(days=1, seconds=6).resolution_string == "S"
|
| 919 |
+
assert Timedelta(days=1, milliseconds=6).resolution_string == "L"
|
| 920 |
+
assert Timedelta(days=1, microseconds=6).resolution_string == "U"
|
| 921 |
+
assert Timedelta(days=1, nanoseconds=6).resolution_string == "N"
|
| 922 |
+
|
| 923 |
+
def test_resolution_deprecated(self):
|
| 924 |
+
# GH#21344
|
| 925 |
+
td = Timedelta(days=4, hours=3)
|
| 926 |
+
result = td.resolution
|
| 927 |
+
assert result == Timedelta(nanoseconds=1)
|
| 928 |
+
|
| 929 |
+
# Check that the attribute is available on the class, mirroring
|
| 930 |
+
# the stdlib timedelta behavior
|
| 931 |
+
result = Timedelta.resolution
|
| 932 |
+
assert result == Timedelta(nanoseconds=1)
|
| 933 |
+
|
| 934 |
+
|
| 935 |
+
@pytest.mark.parametrize(
|
| 936 |
+
"value, expected",
|
| 937 |
+
[
|
| 938 |
+
(Timedelta("10S"), True),
|
| 939 |
+
(Timedelta("-10S"), True),
|
| 940 |
+
(Timedelta(10, unit="ns"), True),
|
| 941 |
+
(Timedelta(0, unit="ns"), False),
|
| 942 |
+
(Timedelta(-10, unit="ns"), True),
|
| 943 |
+
(Timedelta(None), True),
|
| 944 |
+
(NaT, True),
|
| 945 |
+
],
|
| 946 |
+
)
|
| 947 |
+
def test_truthiness(value, expected):
|
| 948 |
+
# https://github.com/pandas-dev/pandas/issues/21484
|
| 949 |
+
assert bool(value) is expected
|
| 950 |
+
|
| 951 |
+
|
| 952 |
+
def test_timedelta_attribute_precision():
|
| 953 |
+
# GH 31354
|
| 954 |
+
td = Timedelta(1552211999999999872, unit="ns")
|
| 955 |
+
result = td.days * 86400
|
| 956 |
+
result += td.seconds
|
| 957 |
+
result *= 1000000
|
| 958 |
+
result += td.microseconds
|
| 959 |
+
result *= 1000
|
| 960 |
+
result += td.nanoseconds
|
| 961 |
+
expected = td._value
|
| 962 |
+
assert result == expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (185 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
|
Binary file (9.6 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_comparisons.cpython-310.pyc
ADDED
|
Binary file (8.68 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (25.4 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_formats.cpython-310.pyc
ADDED
|
Binary file (1.65 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_rendering.cpython-310.pyc
ADDED
|
Binary file (3.07 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_timestamp.cpython-310.pyc
ADDED
|
Binary file (32.9 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_timezones.cpython-310.pyc
ADDED
|
Binary file (13.3 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/__pycache__/test_unary_ops.cpython-310.pyc
ADDED
|
Binary file (16.7 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/test_arithmetic.py
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 1 |
+
from datetime import (
|
| 2 |
+
datetime,
|
| 3 |
+
timedelta,
|
| 4 |
+
timezone,
|
| 5 |
+
)
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pytest
|
| 9 |
+
|
| 10 |
+
from pandas._libs.tslibs import (
|
| 11 |
+
OutOfBoundsDatetime,
|
| 12 |
+
OutOfBoundsTimedelta,
|
| 13 |
+
Timedelta,
|
| 14 |
+
Timestamp,
|
| 15 |
+
offsets,
|
| 16 |
+
to_offset,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
import pandas._testing as tm
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class TestTimestampArithmetic:
|
| 23 |
+
def test_overflow_offset(self):
|
| 24 |
+
# no overflow expected
|
| 25 |
+
|
| 26 |
+
stamp = Timestamp("2000/1/1")
|
| 27 |
+
offset_no_overflow = to_offset("D") * 100
|
| 28 |
+
|
| 29 |
+
expected = Timestamp("2000/04/10")
|
| 30 |
+
assert stamp + offset_no_overflow == expected
|
| 31 |
+
|
| 32 |
+
assert offset_no_overflow + stamp == expected
|
| 33 |
+
|
| 34 |
+
expected = Timestamp("1999/09/23")
|
| 35 |
+
assert stamp - offset_no_overflow == expected
|
| 36 |
+
|
| 37 |
+
def test_overflow_offset_raises(self):
|
| 38 |
+
# xref https://github.com/statsmodels/statsmodels/issues/3374
|
| 39 |
+
# ends up multiplying really large numbers which overflow
|
| 40 |
+
|
| 41 |
+
stamp = Timestamp("2017-01-13 00:00:00").as_unit("ns")
|
| 42 |
+
offset_overflow = 20169940 * offsets.Day(1)
|
| 43 |
+
msg = (
|
| 44 |
+
"the add operation between "
|
| 45 |
+
r"\<-?\d+ \* Days\> and \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} "
|
| 46 |
+
"will overflow"
|
| 47 |
+
)
|
| 48 |
+
lmsg2 = r"Cannot cast -?20169940 days \+?00:00:00 to unit='ns' without overflow"
|
| 49 |
+
|
| 50 |
+
with pytest.raises(OutOfBoundsTimedelta, match=lmsg2):
|
| 51 |
+
stamp + offset_overflow
|
| 52 |
+
|
| 53 |
+
with pytest.raises(OverflowError, match=msg):
|
| 54 |
+
offset_overflow + stamp
|
| 55 |
+
|
| 56 |
+
with pytest.raises(OutOfBoundsTimedelta, match=lmsg2):
|
| 57 |
+
stamp - offset_overflow
|
| 58 |
+
|
| 59 |
+
# xref https://github.com/pandas-dev/pandas/issues/14080
|
| 60 |
+
# used to crash, so check for proper overflow exception
|
| 61 |
+
|
| 62 |
+
stamp = Timestamp("2000/1/1").as_unit("ns")
|
| 63 |
+
offset_overflow = to_offset("D") * 100**5
|
| 64 |
+
|
| 65 |
+
lmsg3 = (
|
| 66 |
+
r"Cannot cast -?10000000000 days \+?00:00:00 to unit='ns' without overflow"
|
| 67 |
+
)
|
| 68 |
+
with pytest.raises(OutOfBoundsTimedelta, match=lmsg3):
|
| 69 |
+
stamp + offset_overflow
|
| 70 |
+
|
| 71 |
+
with pytest.raises(OverflowError, match=msg):
|
| 72 |
+
offset_overflow + stamp
|
| 73 |
+
|
| 74 |
+
with pytest.raises(OutOfBoundsTimedelta, match=lmsg3):
|
| 75 |
+
stamp - offset_overflow
|
| 76 |
+
|
| 77 |
+
def test_overflow_timestamp_raises(self):
|
| 78 |
+
# https://github.com/pandas-dev/pandas/issues/31774
|
| 79 |
+
msg = "Result is too large"
|
| 80 |
+
a = Timestamp("2101-01-01 00:00:00").as_unit("ns")
|
| 81 |
+
b = Timestamp("1688-01-01 00:00:00").as_unit("ns")
|
| 82 |
+
|
| 83 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 84 |
+
a - b
|
| 85 |
+
|
| 86 |
+
# but we're OK for timestamp and datetime.datetime
|
| 87 |
+
assert (a - b.to_pydatetime()) == (a.to_pydatetime() - b)
|
| 88 |
+
|
| 89 |
+
def test_delta_preserve_nanos(self):
|
| 90 |
+
val = Timestamp(1337299200000000123)
|
| 91 |
+
result = val + timedelta(1)
|
| 92 |
+
assert result.nanosecond == val.nanosecond
|
| 93 |
+
|
| 94 |
+
def test_rsub_dtscalars(self, tz_naive_fixture):
|
| 95 |
+
# In particular, check that datetime64 - Timestamp works GH#28286
|
| 96 |
+
td = Timedelta(1235345642000)
|
| 97 |
+
ts = Timestamp("2021-01-01", tz=tz_naive_fixture)
|
| 98 |
+
other = ts + td
|
| 99 |
+
|
| 100 |
+
assert other - ts == td
|
| 101 |
+
assert other.to_pydatetime() - ts == td
|
| 102 |
+
if tz_naive_fixture is None:
|
| 103 |
+
assert other.to_datetime64() - ts == td
|
| 104 |
+
else:
|
| 105 |
+
msg = "Cannot subtract tz-naive and tz-aware datetime-like objects"
|
| 106 |
+
with pytest.raises(TypeError, match=msg):
|
| 107 |
+
other.to_datetime64() - ts
|
| 108 |
+
|
| 109 |
+
def test_timestamp_sub_datetime(self):
|
| 110 |
+
dt = datetime(2013, 10, 12)
|
| 111 |
+
ts = Timestamp(datetime(2013, 10, 13))
|
| 112 |
+
assert (ts - dt).days == 1
|
| 113 |
+
assert (dt - ts).days == -1
|
| 114 |
+
|
| 115 |
+
def test_subtract_tzaware_datetime(self):
|
| 116 |
+
t1 = Timestamp("2020-10-22T22:00:00+00:00")
|
| 117 |
+
t2 = datetime(2020, 10, 22, 22, tzinfo=timezone.utc)
|
| 118 |
+
|
| 119 |
+
result = t1 - t2
|
| 120 |
+
|
| 121 |
+
assert isinstance(result, Timedelta)
|
| 122 |
+
assert result == Timedelta("0 days")
|
| 123 |
+
|
| 124 |
+
def test_subtract_timestamp_from_different_timezone(self):
|
| 125 |
+
t1 = Timestamp("20130101").tz_localize("US/Eastern")
|
| 126 |
+
t2 = Timestamp("20130101").tz_localize("CET")
|
| 127 |
+
|
| 128 |
+
result = t1 - t2
|
| 129 |
+
|
| 130 |
+
assert isinstance(result, Timedelta)
|
| 131 |
+
assert result == Timedelta("0 days 06:00:00")
|
| 132 |
+
|
| 133 |
+
def test_subtracting_involving_datetime_with_different_tz(self):
|
| 134 |
+
t1 = datetime(2013, 1, 1, tzinfo=timezone(timedelta(hours=-5)))
|
| 135 |
+
t2 = Timestamp("20130101").tz_localize("CET")
|
| 136 |
+
|
| 137 |
+
result = t1 - t2
|
| 138 |
+
|
| 139 |
+
assert isinstance(result, Timedelta)
|
| 140 |
+
assert result == Timedelta("0 days 06:00:00")
|
| 141 |
+
|
| 142 |
+
result = t2 - t1
|
| 143 |
+
assert isinstance(result, Timedelta)
|
| 144 |
+
assert result == Timedelta("-1 days +18:00:00")
|
| 145 |
+
|
| 146 |
+
def test_subtracting_different_timezones(self, tz_aware_fixture):
|
| 147 |
+
t_raw = Timestamp("20130101")
|
| 148 |
+
t_UTC = t_raw.tz_localize("UTC")
|
| 149 |
+
t_diff = t_UTC.tz_convert(tz_aware_fixture) + Timedelta("0 days 05:00:00")
|
| 150 |
+
|
| 151 |
+
result = t_diff - t_UTC
|
| 152 |
+
|
| 153 |
+
assert isinstance(result, Timedelta)
|
| 154 |
+
assert result == Timedelta("0 days 05:00:00")
|
| 155 |
+
|
| 156 |
+
def test_addition_subtraction_types(self):
|
| 157 |
+
# Assert on the types resulting from Timestamp +/- various date/time
|
| 158 |
+
# objects
|
| 159 |
+
dt = datetime(2014, 3, 4)
|
| 160 |
+
td = timedelta(seconds=1)
|
| 161 |
+
ts = Timestamp(dt)
|
| 162 |
+
|
| 163 |
+
msg = "Addition/subtraction of integers"
|
| 164 |
+
with pytest.raises(TypeError, match=msg):
|
| 165 |
+
# GH#22535 add/sub with integers is deprecated
|
| 166 |
+
ts + 1
|
| 167 |
+
with pytest.raises(TypeError, match=msg):
|
| 168 |
+
ts - 1
|
| 169 |
+
|
| 170 |
+
# Timestamp + datetime not supported, though subtraction is supported
|
| 171 |
+
# and yields timedelta more tests in tseries/base/tests/test_base.py
|
| 172 |
+
assert type(ts - dt) == Timedelta
|
| 173 |
+
assert type(ts + td) == Timestamp
|
| 174 |
+
assert type(ts - td) == Timestamp
|
| 175 |
+
|
| 176 |
+
# Timestamp +/- datetime64 not supported, so not tested (could possibly
|
| 177 |
+
# assert error raised?)
|
| 178 |
+
td64 = np.timedelta64(1, "D")
|
| 179 |
+
assert type(ts + td64) == Timestamp
|
| 180 |
+
assert type(ts - td64) == Timestamp
|
| 181 |
+
|
| 182 |
+
@pytest.mark.parametrize(
|
| 183 |
+
"td", [Timedelta(hours=3), np.timedelta64(3, "h"), timedelta(hours=3)]
|
| 184 |
+
)
|
| 185 |
+
def test_radd_tdscalar(self, td, fixed_now_ts):
|
| 186 |
+
# GH#24775 timedelta64+Timestamp should not raise
|
| 187 |
+
ts = fixed_now_ts
|
| 188 |
+
assert td + ts == ts + td
|
| 189 |
+
|
| 190 |
+
@pytest.mark.parametrize(
|
| 191 |
+
"other,expected_difference",
|
| 192 |
+
[
|
| 193 |
+
(np.timedelta64(-123, "ns"), -123),
|
| 194 |
+
(np.timedelta64(1234567898, "ns"), 1234567898),
|
| 195 |
+
(np.timedelta64(-123, "us"), -123000),
|
| 196 |
+
(np.timedelta64(-123, "ms"), -123000000),
|
| 197 |
+
],
|
| 198 |
+
)
|
| 199 |
+
def test_timestamp_add_timedelta64_unit(self, other, expected_difference):
|
| 200 |
+
now = datetime.utcnow()
|
| 201 |
+
ts = Timestamp(now).as_unit("ns")
|
| 202 |
+
result = ts + other
|
| 203 |
+
valdiff = result._value - ts._value
|
| 204 |
+
assert valdiff == expected_difference
|
| 205 |
+
|
| 206 |
+
ts2 = Timestamp(now)
|
| 207 |
+
assert ts2 + other == result
|
| 208 |
+
|
| 209 |
+
@pytest.mark.parametrize(
|
| 210 |
+
"ts",
|
| 211 |
+
[
|
| 212 |
+
Timestamp("1776-07-04"),
|
| 213 |
+
Timestamp("1776-07-04", tz="UTC"),
|
| 214 |
+
],
|
| 215 |
+
)
|
| 216 |
+
@pytest.mark.parametrize(
|
| 217 |
+
"other",
|
| 218 |
+
[
|
| 219 |
+
1,
|
| 220 |
+
np.int64(1),
|
| 221 |
+
np.array([1, 2], dtype=np.int32),
|
| 222 |
+
np.array([3, 4], dtype=np.uint64),
|
| 223 |
+
],
|
| 224 |
+
)
|
| 225 |
+
def test_add_int_with_freq(self, ts, other):
|
| 226 |
+
msg = "Addition/subtraction of integers and integer-arrays"
|
| 227 |
+
with pytest.raises(TypeError, match=msg):
|
| 228 |
+
ts + other
|
| 229 |
+
with pytest.raises(TypeError, match=msg):
|
| 230 |
+
other + ts
|
| 231 |
+
|
| 232 |
+
with pytest.raises(TypeError, match=msg):
|
| 233 |
+
ts - other
|
| 234 |
+
|
| 235 |
+
msg = "unsupported operand type"
|
| 236 |
+
with pytest.raises(TypeError, match=msg):
|
| 237 |
+
other - ts
|
| 238 |
+
|
| 239 |
+
@pytest.mark.parametrize("shape", [(6,), (2, 3)])
|
| 240 |
+
def test_addsub_m8ndarray(self, shape):
|
| 241 |
+
# GH#33296
|
| 242 |
+
ts = Timestamp("2020-04-04 15:45").as_unit("ns")
|
| 243 |
+
other = np.arange(6).astype("m8[h]").reshape(shape)
|
| 244 |
+
|
| 245 |
+
result = ts + other
|
| 246 |
+
|
| 247 |
+
ex_stamps = [ts + Timedelta(hours=n) for n in range(6)]
|
| 248 |
+
expected = np.array([x.asm8 for x in ex_stamps], dtype="M8[ns]").reshape(shape)
|
| 249 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 250 |
+
|
| 251 |
+
result = other + ts
|
| 252 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 253 |
+
|
| 254 |
+
result = ts - other
|
| 255 |
+
ex_stamps = [ts - Timedelta(hours=n) for n in range(6)]
|
| 256 |
+
expected = np.array([x.asm8 for x in ex_stamps], dtype="M8[ns]").reshape(shape)
|
| 257 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 258 |
+
|
| 259 |
+
msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timestamp'"
|
| 260 |
+
with pytest.raises(TypeError, match=msg):
|
| 261 |
+
other - ts
|
| 262 |
+
|
| 263 |
+
@pytest.mark.parametrize("shape", [(6,), (2, 3)])
|
| 264 |
+
def test_addsub_m8ndarray_tzaware(self, shape):
|
| 265 |
+
# GH#33296
|
| 266 |
+
ts = Timestamp("2020-04-04 15:45", tz="US/Pacific")
|
| 267 |
+
|
| 268 |
+
other = np.arange(6).astype("m8[h]").reshape(shape)
|
| 269 |
+
|
| 270 |
+
result = ts + other
|
| 271 |
+
|
| 272 |
+
ex_stamps = [ts + Timedelta(hours=n) for n in range(6)]
|
| 273 |
+
expected = np.array(ex_stamps).reshape(shape)
|
| 274 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 275 |
+
|
| 276 |
+
result = other + ts
|
| 277 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 278 |
+
|
| 279 |
+
result = ts - other
|
| 280 |
+
ex_stamps = [ts - Timedelta(hours=n) for n in range(6)]
|
| 281 |
+
expected = np.array(ex_stamps).reshape(shape)
|
| 282 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 283 |
+
|
| 284 |
+
msg = r"unsupported operand type\(s\) for -: 'numpy.ndarray' and 'Timestamp'"
|
| 285 |
+
with pytest.raises(TypeError, match=msg):
|
| 286 |
+
other - ts
|
| 287 |
+
|
| 288 |
+
def test_subtract_different_utc_objects(self, utc_fixture, utc_fixture2):
|
| 289 |
+
# GH 32619
|
| 290 |
+
dt = datetime(2021, 1, 1)
|
| 291 |
+
ts1 = Timestamp(dt, tz=utc_fixture)
|
| 292 |
+
ts2 = Timestamp(dt, tz=utc_fixture2)
|
| 293 |
+
result = ts1 - ts2
|
| 294 |
+
expected = Timedelta(0)
|
| 295 |
+
assert result == expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/scalar/timestamp/test_comparisons.py
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import (
|
| 2 |
+
datetime,
|
| 3 |
+
timedelta,
|
| 4 |
+
)
|
| 5 |
+
import operator
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pytest
|
| 9 |
+
|
| 10 |
+
from pandas import Timestamp
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestTimestampComparison:
|
| 15 |
+
def test_compare_non_nano_dt64(self):
|
| 16 |
+
# don't raise when converting dt64 to Timestamp in __richcmp__
|
| 17 |
+
dt = np.datetime64("1066-10-14")
|
| 18 |
+
ts = Timestamp(dt)
|
| 19 |
+
|
| 20 |
+
assert dt == ts
|
| 21 |
+
|
| 22 |
+
def test_comparison_dt64_ndarray(self):
|
| 23 |
+
ts = Timestamp("2021-01-01")
|
| 24 |
+
ts2 = Timestamp("2019-04-05")
|
| 25 |
+
arr = np.array([[ts.asm8, ts2.asm8]], dtype="M8[ns]")
|
| 26 |
+
|
| 27 |
+
result = ts == arr
|
| 28 |
+
expected = np.array([[True, False]], dtype=bool)
|
| 29 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 30 |
+
|
| 31 |
+
result = arr == ts
|
| 32 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 33 |
+
|
| 34 |
+
result = ts != arr
|
| 35 |
+
tm.assert_numpy_array_equal(result, ~expected)
|
| 36 |
+
|
| 37 |
+
result = arr != ts
|
| 38 |
+
tm.assert_numpy_array_equal(result, ~expected)
|
| 39 |
+
|
| 40 |
+
result = ts2 < arr
|
| 41 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 42 |
+
|
| 43 |
+
result = arr < ts2
|
| 44 |
+
tm.assert_numpy_array_equal(result, np.array([[False, False]], dtype=bool))
|
| 45 |
+
|
| 46 |
+
result = ts2 <= arr
|
| 47 |
+
tm.assert_numpy_array_equal(result, np.array([[True, True]], dtype=bool))
|
| 48 |
+
|
| 49 |
+
result = arr <= ts2
|
| 50 |
+
tm.assert_numpy_array_equal(result, ~expected)
|
| 51 |
+
|
| 52 |
+
result = ts >= arr
|
| 53 |
+
tm.assert_numpy_array_equal(result, np.array([[True, True]], dtype=bool))
|
| 54 |
+
|
| 55 |
+
result = arr >= ts
|
| 56 |
+
tm.assert_numpy_array_equal(result, np.array([[True, False]], dtype=bool))
|
| 57 |
+
|
| 58 |
+
@pytest.mark.parametrize("reverse", [True, False])
|
| 59 |
+
def test_comparison_dt64_ndarray_tzaware(self, reverse, comparison_op):
|
| 60 |
+
ts = Timestamp("2021-01-01 00:00:00.00000", tz="UTC")
|
| 61 |
+
arr = np.array([ts.asm8, ts.asm8], dtype="M8[ns]")
|
| 62 |
+
|
| 63 |
+
left, right = ts, arr
|
| 64 |
+
if reverse:
|
| 65 |
+
left, right = arr, ts
|
| 66 |
+
|
| 67 |
+
if comparison_op is operator.eq:
|
| 68 |
+
expected = np.array([False, False], dtype=bool)
|
| 69 |
+
result = comparison_op(left, right)
|
| 70 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 71 |
+
elif comparison_op is operator.ne:
|
| 72 |
+
expected = np.array([True, True], dtype=bool)
|
| 73 |
+
result = comparison_op(left, right)
|
| 74 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 75 |
+
else:
|
| 76 |
+
msg = "Cannot compare tz-naive and tz-aware timestamps"
|
| 77 |
+
with pytest.raises(TypeError, match=msg):
|
| 78 |
+
comparison_op(left, right)
|
| 79 |
+
|
| 80 |
+
def test_comparison_object_array(self):
|
| 81 |
+
# GH#15183
|
| 82 |
+
ts = Timestamp("2011-01-03 00:00:00-0500", tz="US/Eastern")
|
| 83 |
+
other = Timestamp("2011-01-01 00:00:00-0500", tz="US/Eastern")
|
| 84 |
+
naive = Timestamp("2011-01-01 00:00:00")
|
| 85 |
+
|
| 86 |
+
arr = np.array([other, ts], dtype=object)
|
| 87 |
+
res = arr == ts
|
| 88 |
+
expected = np.array([False, True], dtype=bool)
|
| 89 |
+
assert (res == expected).all()
|
| 90 |
+
|
| 91 |
+
# 2D case
|
| 92 |
+
arr = np.array([[other, ts], [ts, other]], dtype=object)
|
| 93 |
+
res = arr != ts
|
| 94 |
+
expected = np.array([[True, False], [False, True]], dtype=bool)
|
| 95 |
+
assert res.shape == expected.shape
|
| 96 |
+
assert (res == expected).all()
|
| 97 |
+
|
| 98 |
+
# tzaware mismatch
|
| 99 |
+
arr = np.array([naive], dtype=object)
|
| 100 |
+
msg = "Cannot compare tz-naive and tz-aware timestamps"
|
| 101 |
+
with pytest.raises(TypeError, match=msg):
|
| 102 |
+
arr < ts
|
| 103 |
+
|
| 104 |
+
def test_comparison(self):
|
| 105 |
+
# 5-18-2012 00:00:00.000
|
| 106 |
+
stamp = 1337299200000000000
|
| 107 |
+
|
| 108 |
+
val = Timestamp(stamp)
|
| 109 |
+
|
| 110 |
+
assert val == val
|
| 111 |
+
assert not val != val
|
| 112 |
+
assert not val < val
|
| 113 |
+
assert val <= val
|
| 114 |
+
assert not val > val
|
| 115 |
+
assert val >= val
|
| 116 |
+
|
| 117 |
+
other = datetime(2012, 5, 18)
|
| 118 |
+
assert val == other
|
| 119 |
+
assert not val != other
|
| 120 |
+
assert not val < other
|
| 121 |
+
assert val <= other
|
| 122 |
+
assert not val > other
|
| 123 |
+
assert val >= other
|
| 124 |
+
|
| 125 |
+
other = Timestamp(stamp + 100)
|
| 126 |
+
|
| 127 |
+
assert val != other
|
| 128 |
+
assert val != other
|
| 129 |
+
assert val < other
|
| 130 |
+
assert val <= other
|
| 131 |
+
assert other > val
|
| 132 |
+
assert other >= val
|
| 133 |
+
|
| 134 |
+
def test_compare_invalid(self):
|
| 135 |
+
# GH#8058
|
| 136 |
+
val = Timestamp("20130101 12:01:02")
|
| 137 |
+
assert not val == "foo"
|
| 138 |
+
assert not val == 10.0
|
| 139 |
+
assert not val == 1
|
| 140 |
+
assert not val == []
|
| 141 |
+
assert not val == {"foo": 1}
|
| 142 |
+
assert not val == np.float64(1)
|
| 143 |
+
assert not val == np.int64(1)
|
| 144 |
+
|
| 145 |
+
assert val != "foo"
|
| 146 |
+
assert val != 10.0
|
| 147 |
+
assert val != 1
|
| 148 |
+
assert val != []
|
| 149 |
+
assert val != {"foo": 1}
|
| 150 |
+
assert val != np.float64(1)
|
| 151 |
+
assert val != np.int64(1)
|
| 152 |
+
|
| 153 |
+
@pytest.mark.parametrize("tz", [None, "US/Pacific"])
|
| 154 |
+
def test_compare_date(self, tz):
|
| 155 |
+
# GH#36131 comparing Timestamp with date object is deprecated
|
| 156 |
+
ts = Timestamp("2021-01-01 00:00:00.00000", tz=tz)
|
| 157 |
+
dt = ts.to_pydatetime().date()
|
| 158 |
+
# in 2.0 we disallow comparing pydate objects with Timestamps,
|
| 159 |
+
# following the stdlib datetime behavior.
|
| 160 |
+
|
| 161 |
+
msg = "Cannot compare Timestamp with datetime.date"
|
| 162 |
+
for left, right in [(ts, dt), (dt, ts)]:
|
| 163 |
+
assert not left == right
|
| 164 |
+
assert left != right
|
| 165 |
+
|
| 166 |
+
with pytest.raises(TypeError, match=msg):
|
| 167 |
+
left < right
|
| 168 |
+
with pytest.raises(TypeError, match=msg):
|
| 169 |
+
left <= right
|
| 170 |
+
with pytest.raises(TypeError, match=msg):
|
| 171 |
+
left > right
|
| 172 |
+
with pytest.raises(TypeError, match=msg):
|
| 173 |
+
left >= right
|
| 174 |
+
|
| 175 |
+
def test_cant_compare_tz_naive_w_aware(self, utc_fixture):
|
| 176 |
+
# see GH#1404
|
| 177 |
+
a = Timestamp("3/12/2012")
|
| 178 |
+
b = Timestamp("3/12/2012", tz=utc_fixture)
|
| 179 |
+
|
| 180 |
+
msg = "Cannot compare tz-naive and tz-aware timestamps"
|
| 181 |
+
assert not a == b
|
| 182 |
+
assert a != b
|
| 183 |
+
with pytest.raises(TypeError, match=msg):
|
| 184 |
+
a < b
|
| 185 |
+
with pytest.raises(TypeError, match=msg):
|
| 186 |
+
a <= b
|
| 187 |
+
with pytest.raises(TypeError, match=msg):
|
| 188 |
+
a > b
|
| 189 |
+
with pytest.raises(TypeError, match=msg):
|
| 190 |
+
a >= b
|
| 191 |
+
|
| 192 |
+
assert not b == a
|
| 193 |
+
assert b != a
|
| 194 |
+
with pytest.raises(TypeError, match=msg):
|
| 195 |
+
b < a
|
| 196 |
+
with pytest.raises(TypeError, match=msg):
|
| 197 |
+
b <= a
|
| 198 |
+
with pytest.raises(TypeError, match=msg):
|
| 199 |
+
b > a
|
| 200 |
+
with pytest.raises(TypeError, match=msg):
|
| 201 |
+
b >= a
|
| 202 |
+
|
| 203 |
+
assert not a == b.to_pydatetime()
|
| 204 |
+
assert not a.to_pydatetime() == b
|
| 205 |
+
|
| 206 |
+
def test_timestamp_compare_scalars(self):
|
| 207 |
+
# case where ndim == 0
|
| 208 |
+
lhs = np.datetime64(datetime(2013, 12, 6))
|
| 209 |
+
rhs = Timestamp("now")
|
| 210 |
+
nat = Timestamp("nat")
|
| 211 |
+
|
| 212 |
+
ops = {"gt": "lt", "lt": "gt", "ge": "le", "le": "ge", "eq": "eq", "ne": "ne"}
|
| 213 |
+
|
| 214 |
+
for left, right in ops.items():
|
| 215 |
+
left_f = getattr(operator, left)
|
| 216 |
+
right_f = getattr(operator, right)
|
| 217 |
+
expected = left_f(lhs, rhs)
|
| 218 |
+
|
| 219 |
+
result = right_f(rhs, lhs)
|
| 220 |
+
assert result == expected
|
| 221 |
+
|
| 222 |
+
expected = left_f(rhs, nat)
|
| 223 |
+
result = right_f(nat, rhs)
|
| 224 |
+
assert result == expected
|
| 225 |
+
|
| 226 |
+
def test_timestamp_compare_with_early_datetime(self):
|
| 227 |
+
# e.g. datetime.min
|
| 228 |
+
stamp = Timestamp("2012-01-01")
|
| 229 |
+
|
| 230 |
+
assert not stamp == datetime.min
|
| 231 |
+
assert not stamp == datetime(1600, 1, 1)
|
| 232 |
+
assert not stamp == datetime(2700, 1, 1)
|
| 233 |
+
assert stamp != datetime.min
|
| 234 |
+
assert stamp != datetime(1600, 1, 1)
|
| 235 |
+
assert stamp != datetime(2700, 1, 1)
|
| 236 |
+
assert stamp > datetime(1600, 1, 1)
|
| 237 |
+
assert stamp >= datetime(1600, 1, 1)
|
| 238 |
+
assert stamp < datetime(2700, 1, 1)
|
| 239 |
+
assert stamp <= datetime(2700, 1, 1)
|
| 240 |
+
|
| 241 |
+
other = Timestamp.min.to_pydatetime(warn=False)
|
| 242 |
+
assert other - timedelta(microseconds=1) < Timestamp.min
|
| 243 |
+
|
| 244 |
+
def test_timestamp_compare_oob_dt64(self):
|
| 245 |
+
us = np.timedelta64(1, "us")
|
| 246 |
+
other = np.datetime64(Timestamp.min).astype("M8[us]")
|
| 247 |
+
|
| 248 |
+
# This may change if the implementation bound is dropped to match
|
| 249 |
+
# DatetimeArray/DatetimeIndex GH#24124
|
| 250 |
+
assert Timestamp.min > other
|
| 251 |
+
# Note: numpy gets the reversed comparison wrong
|
| 252 |
+
|
| 253 |
+
other = np.datetime64(Timestamp.max).astype("M8[us]")
|
| 254 |
+
assert Timestamp.max > other # not actually OOB
|
| 255 |
+
assert other < Timestamp.max
|
| 256 |
+
|
| 257 |
+
assert Timestamp.max < other + us
|
| 258 |
+
# Note: numpy gets the reversed comparison wrong
|
| 259 |
+
|
| 260 |
+
# GH-42794
|
| 261 |
+
other = datetime(9999, 9, 9)
|
| 262 |
+
assert Timestamp.min < other
|
| 263 |
+
assert other > Timestamp.min
|
| 264 |
+
assert Timestamp.max < other
|
| 265 |
+
assert other > Timestamp.max
|
| 266 |
+
|
| 267 |
+
other = datetime(1, 1, 1)
|
| 268 |
+
assert Timestamp.max > other
|
| 269 |
+
assert other < Timestamp.max
|
| 270 |
+
assert Timestamp.min > other
|
| 271 |
+
assert other < Timestamp.min
|
| 272 |
+
|
| 273 |
+
def test_compare_zerodim_array(self, fixed_now_ts):
|
| 274 |
+
# GH#26916
|
| 275 |
+
ts = fixed_now_ts
|
| 276 |
+
dt64 = np.datetime64("2016-01-01", "ns")
|
| 277 |
+
arr = np.array(dt64)
|
| 278 |
+
assert arr.ndim == 0
|
| 279 |
+
|
| 280 |
+
result = arr < ts
|
| 281 |
+
assert result is np.bool_(True)
|
| 282 |
+
result = arr > ts
|
| 283 |
+
assert result is np.bool_(False)
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
def test_rich_comparison_with_unsupported_type():
|
| 287 |
+
# Comparisons with unsupported objects should return NotImplemented
|
| 288 |
+
# (it previously raised TypeError, see #24011)
|
| 289 |
+
|
| 290 |
+
class Inf:
|
| 291 |
+
def __lt__(self, o):
|
| 292 |
+
return False
|
| 293 |
+
|
| 294 |
+
def __le__(self, o):
|
| 295 |
+
return isinstance(o, Inf)
|
| 296 |
+
|
| 297 |
+
def __gt__(self, o):
|
| 298 |
+
return not isinstance(o, Inf)
|
| 299 |
+
|
| 300 |
+
def __ge__(self, o):
|
| 301 |
+
return True
|
| 302 |
+
|
| 303 |
+
def __eq__(self, other) -> bool:
|
| 304 |
+
return isinstance(other, Inf)
|
| 305 |
+
|
| 306 |
+
inf = Inf()
|
| 307 |
+
timestamp = Timestamp("2018-11-30")
|
| 308 |
+
|
| 309 |
+
for left, right in [(inf, timestamp), (timestamp, inf)]:
|
| 310 |
+
assert left > right or left < right
|
| 311 |
+
assert left >= right or left <= right
|
| 312 |
+
assert not left == right # pylint: disable=unneeded-not
|
| 313 |
+
assert left != right
|