@@ -1632,6 +1632,8 @@ def sequence_to_dt64ns(data, dtype=None, copy=False,
1632
1632
1633
1633
inferred_freq = None
1634
1634
1635
+ dtype = _validate_dt64_dtype (dtype )
1636
+
1635
1637
if not hasattr (data , "dtype" ):
1636
1638
# e.g. list, tuple
1637
1639
if np .ndim (data ) == 0 :
@@ -1836,7 +1838,7 @@ def maybe_convert_dtype(data, copy):
1836
1838
data = data .view (_NS_DTYPE )
1837
1839
1838
1840
elif is_period_dtype (data ):
1839
- # Note: without explicitly raising here, PeriondIndex
1841
+ # Note: without explicitly raising here, PeriodIndex
1840
1842
# test_setops.test_join_does_not_recur fails
1841
1843
raise TypeError ("Passing PeriodDtype data is invalid. "
1842
1844
"Use `data.to_timestamp()` instead" )
@@ -1889,6 +1891,38 @@ def maybe_infer_tz(tz, inferred_tz):
1889
1891
return tz
1890
1892
1891
1893
1894
+ def _validate_dt64_dtype (dtype ):
1895
+ """
1896
+ Check that a dtype, if passed, represents either a numpy datetime64[ns]
1897
+ dtype or a pandas DatetimeTZDtype.
1898
+
1899
+ Parameters
1900
+ ----------
1901
+ dtype : object
1902
+
1903
+ Returns
1904
+ -------
1905
+ dtype : None, numpy.dtype, or DatetimeTZDtype
1906
+
1907
+ Raises
1908
+ ------
1909
+ ValueError : invalid dtype
1910
+
1911
+ Notes
1912
+ -----
1913
+ Unlike validate_tz_from_dtype, this does _not_ allow non-existent
1914
+ tz errors to go through
1915
+ """
1916
+ if dtype is not None :
1917
+ dtype = pandas_dtype (dtype )
1918
+ if ((isinstance (dtype , np .dtype ) and dtype != _NS_DTYPE )
1919
+ or not isinstance (dtype , (np .dtype , DatetimeTZDtype ))):
1920
+ raise ValueError ("Unexpected value for 'dtype': '{dtype}'. "
1921
+ "Must be 'datetime64[ns]' or DatetimeTZDtype'."
1922
+ .format (dtype = dtype ))
1923
+ return dtype
1924
+
1925
+
1892
1926
def validate_tz_from_dtype (dtype , tz ):
1893
1927
"""
1894
1928
If the given dtype is a DatetimeTZDtype, extract the implied
0 commit comments