123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- """
- Tests for offsets.Tick and subclasses
- """
- from datetime import (
- datetime,
- timedelta,
- )
- from hypothesis import (
- assume,
- example,
- given,
- )
- import numpy as np
- import pytest
- from pandas._libs.tslibs.offsets import delta_to_tick
- from pandas import (
- Timedelta,
- Timestamp,
- )
- import pandas._testing as tm
- from pandas._testing._hypothesis import INT_NEG_999_TO_POS_999
- from pandas.tests.tseries.offsets.common import assert_offset_equal
- from pandas.tseries import offsets
- from pandas.tseries.offsets import (
- Hour,
- Micro,
- Milli,
- Minute,
- Nano,
- Second,
- )
- tick_classes = [Hour, Minute, Second, Milli, Micro, Nano]
- def test_apply_ticks():
- result = offsets.Hour(3) + offsets.Hour(4)
- exp = offsets.Hour(7)
- assert result == exp
- def test_delta_to_tick():
- delta = timedelta(3)
- tick = delta_to_tick(delta)
- assert tick == offsets.Day(3)
- td = Timedelta(nanoseconds=5)
- tick = delta_to_tick(td)
- assert tick == Nano(5)
- @pytest.mark.parametrize("cls", tick_classes)
- @example(n=2, m=3)
- @example(n=800, m=300)
- @example(n=1000, m=5)
- @given(n=INT_NEG_999_TO_POS_999, m=INT_NEG_999_TO_POS_999)
- def test_tick_add_sub(cls, n, m):
-
-
-
- left = cls(n)
- right = cls(m)
- expected = cls(n + m)
- assert left + right == expected
- expected = cls(n - m)
- assert left - right == expected
- @pytest.mark.arm_slow
- @pytest.mark.parametrize("cls", tick_classes)
- @example(n=2, m=3)
- @given(n=INT_NEG_999_TO_POS_999, m=INT_NEG_999_TO_POS_999)
- def test_tick_equality(cls, n, m):
- assume(m != n)
-
- left = cls(n)
- right = cls(m)
- assert left != right
- right = cls(n)
- assert left == right
- assert not left != right
- if n != 0:
- assert cls(n) != cls(-n)
- def test_Hour():
- assert_offset_equal(Hour(), datetime(2010, 1, 1), datetime(2010, 1, 1, 1))
- assert_offset_equal(Hour(-1), datetime(2010, 1, 1, 1), datetime(2010, 1, 1))
- assert_offset_equal(2 * Hour(), datetime(2010, 1, 1), datetime(2010, 1, 1, 2))
- assert_offset_equal(-1 * Hour(), datetime(2010, 1, 1, 1), datetime(2010, 1, 1))
- assert Hour(3) + Hour(2) == Hour(5)
- assert Hour(3) - Hour(2) == Hour()
- assert Hour(4) != Hour(1)
- def test_Minute():
- assert_offset_equal(Minute(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 1))
- assert_offset_equal(Minute(-1), datetime(2010, 1, 1, 0, 1), datetime(2010, 1, 1))
- assert_offset_equal(2 * Minute(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 2))
- assert_offset_equal(-1 * Minute(), datetime(2010, 1, 1, 0, 1), datetime(2010, 1, 1))
- assert Minute(3) + Minute(2) == Minute(5)
- assert Minute(3) - Minute(2) == Minute()
- assert Minute(5) != Minute()
- def test_Second():
- assert_offset_equal(Second(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 0, 1))
- assert_offset_equal(Second(-1), datetime(2010, 1, 1, 0, 0, 1), datetime(2010, 1, 1))
- assert_offset_equal(
- 2 * Second(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 0, 2)
- )
- assert_offset_equal(
- -1 * Second(), datetime(2010, 1, 1, 0, 0, 1), datetime(2010, 1, 1)
- )
- assert Second(3) + Second(2) == Second(5)
- assert Second(3) - Second(2) == Second()
- def test_Millisecond():
- assert_offset_equal(
- Milli(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 0, 0, 1000)
- )
- assert_offset_equal(
- Milli(-1), datetime(2010, 1, 1, 0, 0, 0, 1000), datetime(2010, 1, 1)
- )
- assert_offset_equal(
- Milli(2), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 0, 0, 2000)
- )
- assert_offset_equal(
- 2 * Milli(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 0, 0, 2000)
- )
- assert_offset_equal(
- -1 * Milli(), datetime(2010, 1, 1, 0, 0, 0, 1000), datetime(2010, 1, 1)
- )
- assert Milli(3) + Milli(2) == Milli(5)
- assert Milli(3) - Milli(2) == Milli()
- def test_MillisecondTimestampArithmetic():
- assert_offset_equal(
- Milli(), Timestamp("2010-01-01"), Timestamp("2010-01-01 00:00:00.001")
- )
- assert_offset_equal(
- Milli(-1), Timestamp("2010-01-01 00:00:00.001"), Timestamp("2010-01-01")
- )
- def test_Microsecond():
- assert_offset_equal(Micro(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 0, 0, 1))
- assert_offset_equal(
- Micro(-1), datetime(2010, 1, 1, 0, 0, 0, 1), datetime(2010, 1, 1)
- )
- assert_offset_equal(
- 2 * Micro(), datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 0, 0, 2)
- )
- assert_offset_equal(
- -1 * Micro(), datetime(2010, 1, 1, 0, 0, 0, 1), datetime(2010, 1, 1)
- )
- assert Micro(3) + Micro(2) == Micro(5)
- assert Micro(3) - Micro(2) == Micro()
- def test_NanosecondGeneric():
- timestamp = Timestamp(datetime(2010, 1, 1))
- assert timestamp.nanosecond == 0
- result = timestamp + Nano(10)
- assert result.nanosecond == 10
- reverse_result = Nano(10) + timestamp
- assert reverse_result.nanosecond == 10
- def test_Nanosecond():
- timestamp = Timestamp(datetime(2010, 1, 1))
- assert_offset_equal(Nano(), timestamp, timestamp + np.timedelta64(1, "ns"))
- assert_offset_equal(Nano(-1), timestamp + np.timedelta64(1, "ns"), timestamp)
- assert_offset_equal(2 * Nano(), timestamp, timestamp + np.timedelta64(2, "ns"))
- assert_offset_equal(-1 * Nano(), timestamp + np.timedelta64(1, "ns"), timestamp)
- assert Nano(3) + Nano(2) == Nano(5)
- assert Nano(3) - Nano(2) == Nano()
-
- assert Nano(1) + Nano(10) == Nano(11)
- assert Nano(5) + Micro(1) == Nano(1005)
- assert Micro(5) + Nano(1) == Nano(5001)
- @pytest.mark.parametrize(
- "kls, expected",
- [
- (Hour, Timedelta(hours=5)),
- (Minute, Timedelta(hours=2, minutes=3)),
- (Second, Timedelta(hours=2, seconds=3)),
- (Milli, Timedelta(hours=2, milliseconds=3)),
- (Micro, Timedelta(hours=2, microseconds=3)),
- (Nano, Timedelta(hours=2, nanoseconds=3)),
- ],
- )
- def test_tick_addition(kls, expected):
- offset = kls(3)
- td = Timedelta(hours=2)
- for other in [td, td.to_pytimedelta(), td.to_timedelta64()]:
- result = offset + other
- assert isinstance(result, Timedelta)
- assert result == expected
- result = other + offset
- assert isinstance(result, Timedelta)
- assert result == expected
- @pytest.mark.parametrize("cls", tick_classes)
- def test_tick_division(cls):
- off = cls(10)
- assert off / cls(5) == 2
- assert off / 2 == cls(5)
- assert off / 2.0 == cls(5)
- assert off / off.delta == 1
- assert off / off.delta.to_timedelta64() == 1
- assert off / Nano(1) == off.delta / Nano(1).delta
- if cls is not Nano:
-
- result = off / 1000
- assert isinstance(result, offsets.Tick)
- assert not isinstance(result, cls)
- assert result.delta == off.delta / 1000
- if cls._nanos_inc < Timedelta(seconds=1)._value:
-
- result = off / 0.001
- assert isinstance(result, offsets.Tick)
- assert not isinstance(result, cls)
- assert result.delta == off.delta / 0.001
- def test_tick_mul_float():
- off = Micro(2)
-
- result = off * 1.5
- expected = Micro(3)
- assert result == expected
- assert isinstance(result, Micro)
-
- result = off * 1.25
- expected = Nano(2500)
- assert result == expected
- assert isinstance(result, Nano)
- @pytest.mark.parametrize("cls", tick_classes)
- def test_tick_rdiv(cls):
- off = cls(10)
- delta = off.delta
- td64 = delta.to_timedelta64()
- instance__type = ".".join([cls.__module__, cls.__name__])
- msg = (
- "unsupported operand type\\(s\\) for \\/: 'int'|'float' and "
- f"'{instance__type}'"
- )
- with pytest.raises(TypeError, match=msg):
- 2 / off
- with pytest.raises(TypeError, match=msg):
- 2.0 / off
- assert (td64 * 2.5) / off == 2.5
- if cls is not Nano:
-
- assert (delta.to_pytimedelta() * 2) / off == 2
- result = np.array([2 * td64, td64]) / off
- expected = np.array([2.0, 1.0])
- tm.assert_numpy_array_equal(result, expected)
- @pytest.mark.parametrize("cls1", tick_classes)
- @pytest.mark.parametrize("cls2", tick_classes)
- def test_tick_zero(cls1, cls2):
- assert cls1(0) == cls2(0)
- assert cls1(0) + cls2(0) == cls1(0)
- if cls1 is not Nano:
- assert cls1(2) + cls2(0) == cls1(2)
- if cls1 is Nano:
- assert cls1(2) + Nano(0) == cls1(2)
- @pytest.mark.parametrize("cls", tick_classes)
- def test_tick_equalities(cls):
- assert cls() == cls(1)
- @pytest.mark.parametrize("cls", tick_classes)
- def test_tick_offset(cls):
- assert not cls().is_anchored()
- @pytest.mark.parametrize("cls", tick_classes)
- def test_compare_ticks(cls):
- three = cls(3)
- four = cls(4)
- assert three < cls(4)
- assert cls(3) < four
- assert four > cls(3)
- assert cls(4) > three
- assert cls(3) == cls(3)
- assert cls(3) != cls(4)
- @pytest.mark.parametrize("cls", tick_classes)
- def test_compare_ticks_to_strs(cls):
-
- off = cls(19)
-
-
-
- assert not off == "infer"
- assert not "foo" == off
- instance_type = ".".join([cls.__module__, cls.__name__])
- msg = (
- "'<'|'<='|'>'|'>=' not supported between instances of "
- f"'str' and '{instance_type}'|'{instance_type}' and 'str'"
- )
- for left, right in [("infer", off), (off, "infer")]:
- with pytest.raises(TypeError, match=msg):
- left < right
- with pytest.raises(TypeError, match=msg):
- left <= right
- with pytest.raises(TypeError, match=msg):
- left > right
- with pytest.raises(TypeError, match=msg):
- left >= right
- @pytest.mark.parametrize("cls", tick_classes)
- def test_compare_ticks_to_timedeltalike(cls):
- off = cls(19)
- td = off.delta
- others = [td, td.to_timedelta64()]
- if cls is not Nano:
- others.append(td.to_pytimedelta())
- for other in others:
- assert off == other
- assert not off != other
- assert not off < other
- assert not off > other
- assert off <= other
- assert off >= other
|