| Server IP : 209.209.40.120 / Your IP : 216.73.217.112 Web Server : Microsoft-IIS/10.0 System : Windows NT NEWWWW 10.0 build 17763 (Windows Server 2019) i586 User : NEWWWW$ ( 0) PHP Version : 8.3.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/Python312/Lib/site-packages/pandas/tests/scalar/timestamp/methods/ |
Upload File : |
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
)