Skip to content

TST: categorical reindex with added categories #43210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 30, 2021
Merged
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
27 changes: 27 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
Series,
Timedelta,
Timestamp,
cut,
date_range,
isna,
)
Expand Down Expand Up @@ -2353,6 +2354,32 @@ def test_construct_with_two_categoricalindex_series(self):
)
tm.assert_frame_equal(result, expected)

def test_constructor_series_nonexact_categoricalindex(self):
# GH 42424
ser = Series(range(0, 100))
ser1 = cut(ser, 10).value_counts().head(5)
ser2 = cut(ser, 10).value_counts().tail(5)
result = DataFrame({"1": ser1, "2": ser2})
index = CategoricalIndex(
[
Interval(-0.099, 9.9, closed="right"),
Interval(9.9, 19.8, closed="right"),
Interval(19.8, 29.7, closed="right"),
Interval(29.7, 39.6, closed="right"),
Interval(39.6, 49.5, closed="right"),
Interval(49.5, 59.4, closed="right"),
Interval(59.4, 69.3, closed="right"),
Interval(69.3, 79.2, closed="right"),
Interval(79.2, 89.1, closed="right"),
Interval(89.1, 99, closed="right"),
],
ordered=True,
)
expected = DataFrame(
{"1": [10] * 5 + [np.nan] * 5, "2": [np.nan] * 5 + [10] * 5}, index=index
)
tm.assert_frame_equal(expected, result)

def test_from_M8_structured(self):
dates = [(datetime(2012, 9, 9, 0, 0), datetime(2012, 9, 8, 15, 10))]
arr = np.array(dates, dtype=[("Date", "M8[us]"), ("Forecasting", "M8[us]")])
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/indexes/categorical/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CategoricalIndex,
DataFrame,
Index,
Interval,
Series,
)
import pandas._testing as tm
Expand Down Expand Up @@ -103,3 +104,22 @@ def test_reindex_not_category(self, index_df, index_res, index_exp):
result = df.reindex(index=index_res)
expected = DataFrame(index=index_exp)
tm.assert_frame_equal(result, expected)

def test_reindex_categorical_added_category(self):
# GH 42424
ci = CategoricalIndex(
[Interval(0, 1, closed="right"), Interval(1, 2, closed="right")],
ordered=True,
)
ci_add = CategoricalIndex(
[
Interval(0, 1, closed="right"),
Interval(1, 2, closed="right"),
Interval(2, 3, closed="right"),
Interval(3, 4, closed="right"),
],
ordered=True,
)
result, _ = ci.reindex(ci_add)
expected = ci_add
tm.assert_index_equal(expected, result)