Skip to content

Commit 1a3bee7

Browse files
add test for issue #61675
1 parent 9234ed5 commit 1a3bee7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pandas/tests/frame/methods/test_join.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,41 @@ def test_frame_join_tzaware(self):
575575

576576
tm.assert_index_equal(result.index, expected)
577577
assert result.index.tz.key == "US/Central"
578+
579+
def test_frame_join_categorical_index(self):
580+
# GH 61675
581+
cat_data = pd.Categorical(
582+
[15, 16, 17, 18],
583+
categories=pd.Series(list(range(3, 24)), dtype="Int64"),
584+
ordered=True,
585+
)
586+
values1 = "a b c d".split()
587+
values2 = "xyzzy foo bar ...".split()
588+
df1 = DataFrame({"hr": cat_data, "values1": values1}).set_index("hr")
589+
df2 = DataFrame({"hr": cat_data, "values2": values2}).set_index("hr")
590+
df1.columns = pd.CategoricalIndex([4], dtype=cat_data.dtype, name="other_hr")
591+
df2.columns = pd.CategoricalIndex([3], dtype=cat_data.dtype, name="other_hr")
592+
593+
df_joined_1 = (
594+
df1.reset_index(level="hr")
595+
.merge(df2.reset_index(level="hr"), on="hr")
596+
.set_index("hr")
597+
)
598+
expected1 = DataFrame(
599+
{"hr": cat_data, "values1": values1, "values2": values2}
600+
).set_index("hr")
601+
expected1.columns = Index([4, 3], dtype="object", name="other_hr")
602+
603+
tm.assert_frame_equal(df_joined_1, expected1)
604+
605+
df_joined_2 = df1.join(df2)
606+
expected2 = DataFrame(
607+
{"hr": cat_data, "values1": values1, "values2": values2}
608+
).set_index("hr")
609+
expected2.columns = pd.CategoricalIndex(
610+
[4, 3], dtype=cat_data.dtype, name="other_hr"
611+
)
612+
613+
tm.assert_frame_equal(df_joined_2, expected2)
614+
615+
assert df_joined_1.equals(df_joined_2)

0 commit comments

Comments
 (0)