From 743124c76e11d3c4878b2fb722c1575015635802 Mon Sep 17 00:00:00 2001 From: Chang She Date: Thu, 8 Nov 2012 10:48:41 -0500 Subject: [PATCH] BUG: join_non_unique doesn't sort properly for DatetimeIndex #2196 --- pandas/core/common.py | 7 +++++++ pandas/tools/merge.py | 2 +- pandas/tseries/tests/test_timeseries.py | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 7bbbaab49e864..60a0c30a49d78 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -852,6 +852,13 @@ def is_integer_dtype(arr_or_dtype): (issubclass(tipo, np.datetime64) or issubclass(tipo, np.timedelta64))) +def _is_int_or_datetime_dtype(arr_or_dtype): + # also timedelta64 + if isinstance(arr_or_dtype, np.dtype): + tipo = arr_or_dtype.type + else: + tipo = arr_or_dtype.dtype.type + return issubclass(tipo, np.integer) def is_datetime64_dtype(arr_or_dtype): if isinstance(arr_or_dtype, np.dtype): diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index d92ed1cb01c42..62529201b287c 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -555,7 +555,7 @@ def _right_outer_join(x, y, max_groups): def _factorize_keys(lk, rk, sort=True): - if com.is_integer_dtype(lk) and com.is_integer_dtype(rk): + if com._is_int_or_datetime_dtype(lk) and com._is_int_or_datetime_dtype(rk): klass = lib.Int64Factorizer lk = com._ensure_int64(lk) rk = com._ensure_int64(rk) diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index eabacc2222ebf..daaa86f681ee1 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -1645,6 +1645,14 @@ def _check_join(left, right, how='inner'): _check_join(index[:15], obj_index[5:], how='right') _check_join(index[:15], obj_index[5:], how='left') + def test_join_nonunique(self): + idx1 = to_datetime(['2012-11-06 16:00:11.477563', + '2012-11-06 16:00:11.477563']) + idx2 = to_datetime(['2012-11-06 15:11:09.006507', + '2012-11-06 15:11:09.006507']) + rs = idx1.join(idx2, how='outer') + self.assert_(rs.is_monotonic) + def test_unpickle_daterange(self): pth, _ = os.path.split(os.path.abspath(__file__)) filepath = os.path.join(pth, 'data', 'daterange_073.pickle')