1
1
import numpy as np
2
2
import pytest
3
3
4
+ from pandas ._libs .tslibs .dtypes import NpyDatetimeUnit
4
5
from pandas ._libs .tslibs .np_datetime import (
5
6
OutOfBoundsDatetime ,
6
7
OutOfBoundsTimedelta ,
@@ -37,42 +38,42 @@ def test_is_unitless():
37
38
38
39
def test_get_unit_from_dtype ():
39
40
# 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
43
44
# 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
54
55
55
56
# 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
59
60
# 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
70
71
71
72
72
73
def test_td64_to_tdstruct ():
73
74
val = 12454636234 # arbitrary value
74
75
75
- res1 = py_td64_to_tdstruct (val , 10 ) # ns
76
+ res1 = py_td64_to_tdstruct (val , NpyDatetimeUnit . NPY_FR_ns . value )
76
77
exp1 = {
77
78
"days" : 0 ,
78
79
"hrs" : 0 ,
@@ -87,7 +88,7 @@ def test_td64_to_tdstruct():
87
88
}
88
89
assert res1 == exp1
89
90
90
- res2 = py_td64_to_tdstruct (val , 9 ) # us
91
+ res2 = py_td64_to_tdstruct (val , NpyDatetimeUnit . NPY_FR_us . value )
91
92
exp2 = {
92
93
"days" : 0 ,
93
94
"hrs" : 3 ,
@@ -102,7 +103,7 @@ def test_td64_to_tdstruct():
102
103
}
103
104
assert res2 == exp2
104
105
105
- res3 = py_td64_to_tdstruct (val , 8 ) # ms
106
+ res3 = py_td64_to_tdstruct (val , NpyDatetimeUnit . NPY_FR_ms . value )
106
107
exp3 = {
107
108
"days" : 144 ,
108
109
"hrs" : 3 ,
@@ -118,7 +119,7 @@ def test_td64_to_tdstruct():
118
119
assert res3 == exp3
119
120
120
121
# 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 )
122
123
exp4 = {
123
124
"days" : 144150 ,
124
125
"hrs" : 21 ,
0 commit comments