File tree Expand file tree Collapse file tree 3 files changed +20
-9
lines changed Expand file tree Collapse file tree 3 files changed +20
-9
lines changed Original file line number Diff line number Diff line change 4545 tzconversion ,
4646)
4747from pandas ._libs .tslibs .dtypes import abbrev_to_npy_unit
48- from pandas .errors import PerformanceWarning
48+ from pandas .errors import PerformanceWarning , TimezoneDtypeMismatchError
4949from pandas .util ._exceptions import find_stack_level
5050from pandas .util ._validators import validate_inclusive
5151
@@ -2830,7 +2830,7 @@ def _validate_tz_from_dtype(
28302830 # We also need to check for the case where the user passed a
28312831 # tz-naive dtype (i.e. datetime64[ns])
28322832 if tz is not None and not timezones .tz_compare (tz , dtz ):
2833- raise ValueError (
2833+ raise TimezoneDtypeMismatchError (
28342834 "cannot supply both a tz and a "
28352835 "timezone-naive dtype (i.e. datetime64[ns])"
28362836 )
Original file line number Diff line number Diff line change 4545from pandas .errors import (
4646 IntCastingNaNError ,
4747 LossySetitemError ,
48+ TimezoneDtypeMismatchError ,
4849)
4950
5051from pandas .core .dtypes .common import (
@@ -1095,16 +1096,15 @@ def maybe_cast_to_datetime(
10951096 else :
10961097 try :
10971098 dta = DatetimeArray ._from_sequence (value , dtype = dtype )
1098- except ValueError as err :
1099- # We can give a Series-specific exception message.
1100- if "cannot supply both a tz and a timezone-naive dtype" in str (err ):
1101- raise ValueError (
1099+ except TimezoneDtypeMismatchError as err :
1100+ raise ValueError (
11021101 "Cannot convert timezone-aware data to "
11031102 "timezone-naive dtype. Use "
11041103 "pd.Series(values).dt.tz_localize(None) instead."
1105- ) from err
1106- raise
1107-
1104+ ) from err
1105+ except ValueError :
1106+ raise
1107+
11081108 return dta
11091109
11101110
Original file line number Diff line number Diff line change @@ -1038,6 +1038,16 @@ class InvalidComparison(Exception):
10381038 """
10391039
10401040
1041+ class TimezoneDtypeMismatchError (ValueError ):
1042+ """
1043+ Raised when both a separate tz and a tz-naive numpy datetime64 dtype are
1044+ supplied (e.g. tz is not None and dtype is datetime64[ns]).
1045+
1046+ Use case / message:
1047+ "cannot supply both a tz and a timezone-naive dtype (i.e. datetime64[ns])"
1048+ """
1049+
1050+
10411051__all__ = [
10421052 "AbstractMethodError" ,
10431053 "AttributeConflictWarning" ,
@@ -1081,6 +1091,7 @@ class InvalidComparison(Exception):
10811091 "PyperclipException" ,
10821092 "PyperclipWindowsException" ,
10831093 "SpecificationError" ,
1094+ "TimezoneDtypeMismatchError" ,
10841095 "UndefinedVariableError" ,
10851096 "UnsortedIndexError" ,
10861097 "UnsupportedFunctionCall" ,
You can’t perform that action at this time.
0 commit comments