11import numpy as np
22import pytest
33
4+ from pandas ._libs .tslibs .dtypes import NpyDatetimeUnit
45from pandas ._libs .tslibs .np_datetime import (
56 OutOfBoundsDatetime ,
67 OutOfBoundsTimedelta ,
@@ -37,42 +38,42 @@ def test_is_unitless():
3738
3839def test_get_unit_from_dtype ():
3940 # datetime64
40- assert py_get_unit_from_dtype (np .dtype ("M8[Y]" )) == 0
41- assert py_get_unit_from_dtype (np .dtype ("M8[M]" )) == 1
42- assert py_get_unit_from_dtype (np .dtype ("M8[W]" )) == 2
41+ assert py_get_unit_from_dtype (np .dtype ("M8[Y]" )) == NpyDatetimeUnit . NPY_FR_Y . value
42+ assert py_get_unit_from_dtype (np .dtype ("M8[M]" )) == NpyDatetimeUnit . NPY_FR_M . value
43+ assert py_get_unit_from_dtype (np .dtype ("M8[W]" )) == NpyDatetimeUnit . NPY_FR_W . value
4344 # B has been deprecated and removed -> no 3
44- assert py_get_unit_from_dtype (np .dtype ("M8[D]" )) == 4
45- assert py_get_unit_from_dtype (np .dtype ("M8[h]" )) == 5
46- assert py_get_unit_from_dtype (np .dtype ("M8[m]" )) == 6
47- assert py_get_unit_from_dtype (np .dtype ("M8[s]" )) == 7
48- assert py_get_unit_from_dtype (np .dtype ("M8[ms]" )) == 8
49- assert py_get_unit_from_dtype (np .dtype ("M8[us]" )) == 9
50- assert py_get_unit_from_dtype (np .dtype ("M8[ns]" )) == 10
51- assert py_get_unit_from_dtype (np .dtype ("M8[ps]" )) == 11
52- assert py_get_unit_from_dtype (np .dtype ("M8[fs]" )) == 12
53- assert py_get_unit_from_dtype (np .dtype ("M8[as]" )) == 13
45+ assert py_get_unit_from_dtype (np .dtype ("M8[D]" )) == NpyDatetimeUnit . NPY_FR_D . value
46+ assert py_get_unit_from_dtype (np .dtype ("M8[h]" )) == NpyDatetimeUnit . NPY_FR_h . value
47+ assert py_get_unit_from_dtype (np .dtype ("M8[m]" )) == NpyDatetimeUnit . NPY_FR_m . value
48+ assert py_get_unit_from_dtype (np .dtype ("M8[s]" )) == NpyDatetimeUnit . NPY_FR_s . value
49+ assert py_get_unit_from_dtype (np .dtype ("M8[ms]" )) == NpyDatetimeUnit . NPY_FR_ms . value
50+ assert py_get_unit_from_dtype (np .dtype ("M8[us]" )) == NpyDatetimeUnit . NPY_FR_us . value
51+ assert py_get_unit_from_dtype (np .dtype ("M8[ns]" )) == NpyDatetimeUnit . NPY_FR_ns . value
52+ assert py_get_unit_from_dtype (np .dtype ("M8[ps]" )) == NpyDatetimeUnit . NPY_FR_ps . value
53+ assert py_get_unit_from_dtype (np .dtype ("M8[fs]" )) == NpyDatetimeUnit . NPY_FR_fs . value
54+ assert py_get_unit_from_dtype (np .dtype ("M8[as]" )) == NpyDatetimeUnit . NPY_FR_as . value
5455
5556 # timedelta64
56- assert py_get_unit_from_dtype (np .dtype ("m8[Y]" )) == 0
57- assert py_get_unit_from_dtype (np .dtype ("m8[M]" )) == 1
58- assert py_get_unit_from_dtype (np .dtype ("m8[W]" )) == 2
57+ assert py_get_unit_from_dtype (np .dtype ("m8[Y]" )) == NpyDatetimeUnit . NPY_FR_Y . value
58+ assert py_get_unit_from_dtype (np .dtype ("m8[M]" )) == NpyDatetimeUnit . NPY_FR_M . value
59+ assert py_get_unit_from_dtype (np .dtype ("m8[W]" )) == NpyDatetimeUnit . NPY_FR_W . value
5960 # B has been deprecated and removed -> no 3
60- assert py_get_unit_from_dtype (np .dtype ("m8[D]" )) == 4
61- assert py_get_unit_from_dtype (np .dtype ("m8[h]" )) == 5
62- assert py_get_unit_from_dtype (np .dtype ("m8[m]" )) == 6
63- assert py_get_unit_from_dtype (np .dtype ("m8[s]" )) == 7
64- assert py_get_unit_from_dtype (np .dtype ("m8[ms]" )) == 8
65- assert py_get_unit_from_dtype (np .dtype ("m8[us]" )) == 9
66- assert py_get_unit_from_dtype (np .dtype ("m8[ns]" )) == 10
67- assert py_get_unit_from_dtype (np .dtype ("m8[ps]" )) == 11
68- assert py_get_unit_from_dtype (np .dtype ("m8[fs]" )) == 12
69- assert py_get_unit_from_dtype (np .dtype ("m8[as]" )) == 13
61+ assert py_get_unit_from_dtype (np .dtype ("m8[D]" )) == NpyDatetimeUnit . NPY_FR_D . value
62+ assert py_get_unit_from_dtype (np .dtype ("m8[h]" )) == NpyDatetimeUnit . NPY_FR_h . value
63+ assert py_get_unit_from_dtype (np .dtype ("m8[m]" )) == NpyDatetimeUnit . NPY_FR_m . value
64+ assert py_get_unit_from_dtype (np .dtype ("m8[s]" )) == NpyDatetimeUnit . NPY_FR_s . value
65+ assert py_get_unit_from_dtype (np .dtype ("m8[ms]" )) == NpyDatetimeUnit . NPY_FR_ms . value
66+ assert py_get_unit_from_dtype (np .dtype ("m8[us]" )) == NpyDatetimeUnit . NPY_FR_us . value
67+ assert py_get_unit_from_dtype (np .dtype ("m8[ns]" )) == NpyDatetimeUnit . NPY_FR_ns . value
68+ assert py_get_unit_from_dtype (np .dtype ("m8[ps]" )) == NpyDatetimeUnit . NPY_FR_ps . value
69+ assert py_get_unit_from_dtype (np .dtype ("m8[fs]" )) == NpyDatetimeUnit . NPY_FR_fs . value
70+ assert py_get_unit_from_dtype (np .dtype ("m8[as]" )) == NpyDatetimeUnit . NPY_FR_as . value
7071
7172
7273def test_td64_to_tdstruct ():
7374 val = 12454636234 # arbitrary value
7475
75- res1 = py_td64_to_tdstruct (val , 10 ) # ns
76+ res1 = py_td64_to_tdstruct (val , NpyDatetimeUnit . NPY_FR_ns . value )
7677 exp1 = {
7778 "days" : 0 ,
7879 "hrs" : 0 ,
@@ -87,7 +88,7 @@ def test_td64_to_tdstruct():
8788 }
8889 assert res1 == exp1
8990
90- res2 = py_td64_to_tdstruct (val , 9 ) # us
91+ res2 = py_td64_to_tdstruct (val , NpyDatetimeUnit . NPY_FR_us . value )
9192 exp2 = {
9293 "days" : 0 ,
9394 "hrs" : 3 ,
@@ -102,7 +103,7 @@ def test_td64_to_tdstruct():
102103 }
103104 assert res2 == exp2
104105
105- res3 = py_td64_to_tdstruct (val , 8 ) # ms
106+ res3 = py_td64_to_tdstruct (val , NpyDatetimeUnit . NPY_FR_ms . value )
106107 exp3 = {
107108 "days" : 144 ,
108109 "hrs" : 3 ,
@@ -118,7 +119,7 @@ def test_td64_to_tdstruct():
118119 assert res3 == exp3
119120
120121 # Note this out of bounds for nanosecond Timedelta
121- res4 = py_td64_to_tdstruct (val , 7 ) # s
122+ res4 = py_td64_to_tdstruct (val , NpyDatetimeUnit . NPY_FR_s . value )
122123 exp4 = {
123124 "days" : 144150 ,
124125 "hrs" : 21 ,
0 commit comments