1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pytest
 
from pandas._libs.tslibs.dtypes import NpyDatetimeUnit
from pandas.errors import OutOfBoundsDatetime
 
from pandas import Timestamp
 
 
class TestTimestampAsUnit:
    def test_as_unit(self):
        ts = Timestamp("1970-01-01").as_unit("ns")
        assert ts.unit == "ns"
 
        assert ts.as_unit("ns") is ts
 
        res = ts.as_unit("us")
        assert res._value == ts._value // 1000
        assert res._creso == NpyDatetimeUnit.NPY_FR_us.value
 
        rt = res.as_unit("ns")
        assert rt._value == ts._value
        assert rt._creso == ts._creso
 
        res = ts.as_unit("ms")
        assert res._value == ts._value // 1_000_000
        assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
 
        rt = res.as_unit("ns")
        assert rt._value == ts._value
        assert rt._creso == ts._creso
 
        res = ts.as_unit("s")
        assert res._value == ts._value // 1_000_000_000
        assert res._creso == NpyDatetimeUnit.NPY_FR_s.value
 
        rt = res.as_unit("ns")
        assert rt._value == ts._value
        assert rt._creso == ts._creso
 
    def test_as_unit_overflows(self):
        # microsecond that would be just out of bounds for nano
        us = 9223372800000000
        ts = Timestamp._from_value_and_reso(us, NpyDatetimeUnit.NPY_FR_us.value, None)
 
        msg = "Cannot cast 2262-04-12 00:00:00 to unit='ns' without overflow"
        with pytest.raises(OutOfBoundsDatetime, match=msg):
            ts.as_unit("ns")
 
        res = ts.as_unit("ms")
        assert res._value == us // 1000
        assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
 
    def test_as_unit_rounding(self):
        ts = Timestamp(1_500_000)  # i.e. 1500 microseconds
        res = ts.as_unit("ms")
 
        expected = Timestamp(1_000_000)  # i.e. 1 millisecond
        assert res == expected
 
        assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
        assert res._value == 1
 
        with pytest.raises(ValueError, match="Cannot losslessly convert units"):
            ts.as_unit("ms", round_ok=False)
 
    def test_as_unit_non_nano(self):
        # case where we are going neither to nor from nano
        ts = Timestamp("1970-01-02").as_unit("ms")
        assert ts.year == 1970
        assert ts.month == 1
        assert ts.day == 2
        assert ts.hour == ts.minute == ts.second == ts.microsecond == ts.nanosecond == 0
 
        res = ts.as_unit("s")
        assert res._value == 24 * 3600
        assert res.year == 1970
        assert res.month == 1
        assert res.day == 2
        assert (
            res.hour
            == res.minute
            == res.second
            == res.microsecond
            == res.nanosecond
            == 0
        )