Skip to content

Commit da592ac

Browse files
add test for issue #61675
1 parent 5fc3df3 commit da592ac

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
@@ -574,3 +574,41 @@ def test_frame_join_tzaware(self):
574574

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

0 commit comments

Comments
 (0)