29
29
Timedelta ,
30
30
Timestamp ,
31
31
astype_overflowsafe ,
32
- get_supported_reso ,
33
32
get_unit_from_dtype ,
34
33
is_supported_unit ,
35
- npy_unit_to_abbrev ,
36
34
)
37
35
from pandas ._libs .tslibs .timedeltas import array_to_timedelta64
38
36
from pandas ._typing import (
@@ -1301,17 +1299,19 @@ def _ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
1301
1299
"""
1302
1300
Convert dtypes with granularity less than nanosecond to nanosecond
1303
1301
1304
- >>> _ensure_nanosecond_dtype(np.dtype("M8[D]"))
1305
- dtype('<M8[s]')
1306
-
1307
1302
>>> _ensure_nanosecond_dtype(np.dtype("M8[us]"))
1308
1303
dtype('<M8[us]')
1309
1304
1305
+ >>> _ensure_nanosecond_dtype(np.dtype("M8[D]"))
1306
+ Traceback (most recent call last):
1307
+ ...
1308
+ TypeError: dtype=datetime64[D] is not supported. Supported resolutions are 's', 'ms', 'us', and 'ns'
1309
+
1310
1310
>>> _ensure_nanosecond_dtype(np.dtype("m8[ps]"))
1311
1311
Traceback (most recent call last):
1312
1312
...
1313
- TypeError: cannot convert timedeltalike to dtype [ timedelta64[ps]]
1314
- """
1313
+ TypeError: dtype= timedelta64[ps] is not supported. Supported resolutions are 's', 'ms', 'us', and 'ns'
1314
+ """ # noqa:E501
1315
1315
msg = (
1316
1316
f"The '{ dtype .name } ' dtype has no unit. "
1317
1317
f"Please pass in '{ dtype .name } [ns]' instead."
@@ -1324,29 +1324,19 @@ def _ensure_nanosecond_dtype(dtype: DtypeObj) -> DtypeObj:
1324
1324
# i.e. datetime64tz
1325
1325
pass
1326
1326
1327
- elif dtype .kind == "M" and not is_supported_unit (get_unit_from_dtype (dtype )):
1328
- # pandas supports dtype whose granularity is less than [ns]
1329
- # e.g., [ps], [fs], [as]
1330
- if dtype <= np .dtype ("M8[ns]" ):
1331
- if dtype .name == "datetime64" :
1327
+ elif dtype .kind in ["m" , "M" ]:
1328
+ reso = get_unit_from_dtype (dtype )
1329
+ if not is_supported_unit (reso ):
1330
+ # pre-2.0 we would silently swap in nanos for lower-resolutions,
1331
+ # raise for above-nano resolutions
1332
+ if dtype .name in ["datetime64" , "timedelta64" ]:
1332
1333
raise ValueError (msg )
1333
- reso = get_supported_reso (get_unit_from_dtype (dtype ))
1334
- unit = npy_unit_to_abbrev (reso )
1335
- dtype = np .dtype (f"M8[{ unit } ]" )
1336
- else :
1337
- raise TypeError (f"cannot convert datetimelike to dtype [{ dtype } ]" )
1338
-
1339
- elif dtype .kind == "m" and dtype != TD64NS_DTYPE :
1340
- # pandas supports dtype whose granularity is less than [ns]
1341
- # e.g., [ps], [fs], [as]
1342
- if dtype <= np .dtype ("m8[ns]" ):
1343
- if dtype .name == "timedelta64" :
1344
- raise ValueError (msg )
1345
- reso = get_supported_reso (get_unit_from_dtype (dtype ))
1346
- unit = npy_unit_to_abbrev (reso )
1347
- dtype = np .dtype (f"m8[{ unit } ]" )
1348
- else :
1349
- raise TypeError (f"cannot convert timedeltalike to dtype [{ dtype } ]" )
1334
+ # TODO: ValueError or TypeError? existing test
1335
+ # test_constructor_generic_timestamp_bad_frequency expects TypeError
1336
+ raise TypeError (
1337
+ f"dtype={ dtype } is not supported. Supported resolutions are 's', "
1338
+ "'ms', 'us', and 'ns'"
1339
+ )
1350
1340
return dtype
1351
1341
1352
1342
0 commit comments