Skip to content

Commit 8843c10

Browse files
committed
Fix TypeError when merging categorical dates
1 parent 692b5ee commit 8843c10

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

pandas/core/reshape/merge.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
is_numeric_dtype,
2525
is_integer,
2626
is_int_or_datetime_dtype,
27+
is_datetimelike,
2728
is_dtype_equal,
2829
is_bool,
2930
is_list_like,
@@ -877,7 +878,7 @@ def _get_merge_keys(self):
877878
return left_keys, right_keys, join_names
878879

879880
def _maybe_coerce_merge_keys(self):
880-
# we have valid mergee's but we may have to further
881+
# we have valid mergees but we may have to further
881882
# coerce these if they are originally incompatible types
882883
#
883884
# for example if these are categorical, but are not dtype_equal
@@ -894,6 +895,13 @@ def _maybe_coerce_merge_keys(self):
894895
if is_categorical_dtype(lk) and is_categorical_dtype(rk):
895896
if lk.is_dtype_equal(rk):
896897
continue
898+
899+
# if we are dates with differing categories
900+
# then allow them to proceed because
901+
# coercing to object below results in integers.
902+
if is_datetimelike(lk.categories) and is_datetimelike(rk.categories):
903+
continue
904+
897905
elif is_categorical_dtype(lk) or is_categorical_dtype(rk):
898906
pass
899907

@@ -904,7 +912,7 @@ def _maybe_coerce_merge_keys(self):
904912
# kinds to proceed, eg. int64 and int8
905913
# further if we are object, but we infer to
906914
# the same, then proceed
907-
if (is_numeric_dtype(lk) and is_numeric_dtype(rk)):
915+
if is_numeric_dtype(lk) and is_numeric_dtype(rk):
908916
if lk.dtype.kind == rk.dtype.kind:
909917
continue
910918

pandas/tests/reshape/test_merge.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pylint: disable=E1103
22

33
import pytest
4-
from datetime import datetime
4+
from datetime import datetime, date
55
from numpy.random import randn
66
from numpy import nan
77
import numpy as np
@@ -1515,6 +1515,27 @@ def test_self_join_multiple_categories(self):
15151515

15161516
assert_frame_equal(result, df)
15171517

1518+
def test_dtype_on_categorical_dates(self):
1519+
# GH 16900
1520+
# dates should not be coerced to ints
1521+
1522+
df = pd.DataFrame(
1523+
[[date(2001, 1, 1), 1.1],
1524+
[date(2001, 1, 2), 1.3]],
1525+
columns=['date', 'num2']
1526+
)
1527+
df['date'] = df['date'].astype('category')
1528+
1529+
df2 = pd.DataFrame(
1530+
[[date(2001, 1, 1), 1.3],
1531+
[date(2001, 1, 3), 1.4]],
1532+
columns=['date', 'num4']
1533+
)
1534+
df2['date'] = df2['date'].astype('category')
1535+
1536+
result = pd.merge(df, df2, how='outer', on=['date'])
1537+
assert result['date'].dtype == 'category'
1538+
15181539

15191540
@pytest.fixture
15201541
def left_df():

0 commit comments

Comments
 (0)