Skip to content

Commit 92f0e2e

Browse files
committed
timezonedtypemismatcherror
1 parent a329dc3 commit 92f0e2e

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
tzconversion,
4646
)
4747
from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit
48-
from pandas.errors import PerformanceWarning
48+
from pandas.errors import PerformanceWarning, TimezoneDtypeMismatchError
4949
from pandas.util._exceptions import find_stack_level
5050
from 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
)

pandas/core/dtypes/cast.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from pandas.errors import (
4646
IntCastingNaNError,
4747
LossySetitemError,
48+
TimezoneDtypeMismatchError,
4849
)
4950

5051
from 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

pandas/errors/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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",

0 commit comments

Comments
 (0)