Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/pyspark/pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,7 @@ def append(self, other: "Index") -> "Index":
)
"""
from pyspark.pandas.indexes.multi import MultiIndex
from pyspark.pandas.indexes.category import CategoricalIndex

if isinstance(self, MultiIndex) != isinstance(other, MultiIndex):
raise NotImplementedError(
Expand All @@ -1907,6 +1908,9 @@ def append(self, other: "Index") -> "Index":
)

index_fields = self._index_fields_for_union_like(other, func_name="append")
# Since pandas 1.5.0, the order of category matters.
if isinstance(other, CategoricalIndex):
other = other.reorder_categories(self.categories.to_list())

sdf_self = self._internal.spark_frame.select(self._internal.index_spark_columns)
sdf_other = other._internal.spark_frame.select(other._internal.index_spark_columns)
Expand Down
15 changes: 12 additions & 3 deletions python/pyspark/pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,18 @@ def test_append(self):
psidx3 = ps.from_pandas(pidx3)

self.assert_eq(psidx1.append(psidx2), pidx1.append(pidx2))
self.assert_eq(
psidx1.append(psidx3.astype("category")), pidx1.append(pidx3.astype("category"))
)
if LooseVersion(pd.__version__) >= LooseVersion("1.5.0"):
self.assert_eq(
psidx1.append(psidx3.astype("category")), pidx1.append(pidx3.astype("category"))
)
else:
expected_result = ps.CategoricalIndex(
["x", "y", "z", "y", "x", "w", "z"],
categories=["z", "y", "x", "w"],
ordered=False,
dtype="category",
)
self.assert_eq(psidx1.append(psidx3.astype("category")), expected_result)

# TODO: append non-categorical or categorical with a different category
self.assertRaises(NotImplementedError, lambda: psidx1.append(psidx3))
Expand Down