@@ -21,6 +21,18 @@ def fullmatch(regex, string, flags=0):
2121 regex_string = regex
2222 return re .match ("(?:" + regex_string + r")\Z" , string , flags = flags )
2323
24+ def to_non_numpy_type (np , v ):
25+ """
26+ Convert a numpy scalar value to a native python type.
27+ Handle the special case of datetime64[ns] -> datetime64[us] conversion,
28+ since python datetime only supports microsecond precision.
29+ Without this special case handling, calling .item() on a numpy datetime64[ns]
30+ value returns an integer.
31+ """
32+ if np and hasattr (v , "dtype" ) and v .dtype == np .dtype ('datetime64[ns]' ):
33+ return v .astype ('datetime64[us]' ).item ()
34+ else :
35+ return v .item ()
2436
2537# Utility functions
2638# -----------------
@@ -35,12 +47,12 @@ def to_scalar_or_list(v):
3547 np = get_module ("numpy" , should_load = False )
3648 pd = get_module ("pandas" , should_load = False )
3749 if np and np .isscalar (v ) and hasattr (v , "item" ):
38- return v . item ( )
50+ return to_non_numpy_type ( np , v )
3951 if isinstance (v , (list , tuple )):
4052 return [to_scalar_or_list (e ) for e in v ]
4153 elif np and isinstance (v , np .ndarray ):
4254 if v .ndim == 0 :
43- return v . item ( )
55+ return to_non_numpy_type ( np , v )
4456 return [to_scalar_or_list (e ) for e in v ]
4557 elif pd and isinstance (v , (pd .Series , pd .Index )):
4658 return [to_scalar_or_list (e ) for e in v ]
0 commit comments