Skip to content

Commit ed3afc4

Browse files
reidy-pTomAugspurger
authored andcommitted
ERR: Better error msg when merging on tz-aware and tz-naive columns (pandas-dev#19525)
1 parent e8620ab commit ed3afc4

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ Other API Changes
342342
- Addition and subtraction of ``NaN`` from a :class:`Series` with ``dtype='timedelta64[ns]'`` will raise a ``TypeError` instead of treating the ``NaN`` as ``NaT`` (:issue:`19274`)
343343
- Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`)
344344
- :class:`DateOffset` objects render more simply, e.g. "<DateOffset: days=1>" instead of "<DateOffset: kwds={'days': 1}>" (:issue:`19403`)
345+
- :func:`pandas.merge` provides a more informative error message when trying to merge on timezone-aware and timezone-naive columns (:issue:`15800`)
345346

346347
.. _whatsnew_0230.deprecations:
347348

pandas/core/reshape/merge.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,11 @@ def _maybe_coerce_merge_keys(self):
940940
elif is_dtype_equal(lk.dtype, rk.dtype):
941941
continue
942942

943+
msg = ("You are trying to merge on {lk_dtype} and "
944+
"{rk_dtype} columns. If you wish to proceed "
945+
"you should use pd.concat".format(lk_dtype=lk.dtype,
946+
rk_dtype=rk.dtype))
947+
943948
# if we are numeric, then allow differing
944949
# kinds to proceed, eg. int64 and int8, int and float
945950
# further if we are object, but we infer to
@@ -968,30 +973,18 @@ def _maybe_coerce_merge_keys(self):
968973
pass
969974

970975
# Check if we are trying to merge on obviously
971-
# incompatible dtypes GH 9780
976+
# incompatible dtypes GH 9780, GH 15800
972977
elif is_numeric_dtype(lk) and not is_numeric_dtype(rk):
973-
msg = ("You are trying to merge on {lk_dtype} and "
974-
"{rk_dtype} columns. If you wish to proceed "
975-
"you should use pd.concat".format(lk_dtype=lk.dtype,
976-
rk_dtype=rk.dtype))
977978
raise ValueError(msg)
978979
elif not is_numeric_dtype(lk) and is_numeric_dtype(rk):
979-
msg = ("You are trying to merge on {lk_dtype} and "
980-
"{rk_dtype} columns. If you wish to proceed "
981-
"you should use pd.concat".format(lk_dtype=lk.dtype,
982-
rk_dtype=rk.dtype))
983980
raise ValueError(msg)
984981
elif is_datetimelike(lk) and not is_datetimelike(rk):
985-
msg = ("You are trying to merge on {lk_dtype} and "
986-
"{rk_dtype} columns. If you wish to proceed "
987-
"you should use pd.concat".format(lk_dtype=lk.dtype,
988-
rk_dtype=rk.dtype))
989982
raise ValueError(msg)
990983
elif not is_datetimelike(lk) and is_datetimelike(rk):
991-
msg = ("You are trying to merge on {lk_dtype} and "
992-
"{rk_dtype} columns. If you wish to proceed "
993-
"you should use pd.concat".format(lk_dtype=lk.dtype,
994-
rk_dtype=rk.dtype))
984+
raise ValueError(msg)
985+
elif is_datetime64tz_dtype(lk) and not is_datetime64tz_dtype(rk):
986+
raise ValueError(msg)
987+
elif not is_datetime64tz_dtype(lk) and is_datetime64tz_dtype(rk):
995988
raise ValueError(msg)
996989

997990
# Houston, we have a problem!

pandas/tests/reshape/merge/test_merge.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1512,11 +1512,13 @@ def test_merge_on_ints_floats_warning(self):
15121512
'2011-01-02']),
15131513
(pd.date_range('1/1/2011', periods=2, freq='D'), [0, 1]),
15141514
(pd.date_range('1/1/2011', periods=2, freq='D'), [0.0, 1.0]),
1515+
(pd.date_range('20130101', periods=3),
1516+
pd.date_range('20130101', periods=3, tz='US/Eastern')),
15151517
([0, 1, 2], Series(['a', 'b', 'a']).astype('category')),
15161518
([0.0, 1.0, 2.0], Series(['a', 'b', 'a']).astype('category')),
15171519
])
15181520
def test_merge_incompat_dtypes(self, df1_vals, df2_vals):
1519-
# GH 9780
1521+
# GH 9780, GH 15800
15201522
# Raise a ValueError when a user tries to merge on
15211523
# dtypes that are incompatible (e.g., obj and int/float)
15221524

0 commit comments

Comments
 (0)