-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
BUG: RangeIndex.astype('category') #41263
BUG: RangeIndex.astype('category') #41263
Conversation
@@ -667,19 +667,19 @@ def test_astype_category(self, copy, name, ordered, simple_index): | |||
# standard categories | |||
dtype = CategoricalDtype(ordered=ordered) | |||
result = idx.astype(dtype, copy=copy) | |||
expected = CategoricalIndex(idx.values, name=name, ordered=ordered) | |||
expected = CategoricalIndex(idx, name=name, ordered=ordered) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe have a test explicitly for RangeIndex?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests.indexes.ranges.test_range.py::TestRangeIndex
inherits from this base class, so it's tested that way. Do you have something else in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some tm.assert_foo functions dont raise when we assert an Int64Index equals a RangeIndex. So off the top my head it isn't obvious that this edited test would fail in master. im asking for a test that is super-obviously tied to this bugfix
looks fine ex @jbrockmendel comment. |
6ce8940
to
7840030
Compare
I`ve updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@pytest.mark.parametrize("name", [None, "foo"]) | ||
@pytest.mark.parametrize("ordered", [True, False]) | ||
def test_astype_category(self, copy, name, ordered, simple_index): | ||
super().test_astype_category(copy, name, ordered, simple_index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm we don't usually call the super functions as they are already called so this is confusing. i think you can remove. (assume my assertion is true)
# dtype='category' defaults to ordered=False, so only test once | ||
dtypes.append("category") | ||
|
||
for dtype in dtypes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you not simply construct the expected result and use assert_index_equal(exact=True)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's better. It however exposed a bug because assert_index_equal(exact=True)
didn't actually work as expected.
I fixed that bug in assert_index_equal
and changed these tests to use exact=True
.
thanks @topper-123 (and also for fixing exact!) |
There's currently inconsistent behaviour when converting
RangeIndex
toCategoricalIndex
using different methods. This fixes that.In general, when supplying a Index subclass to
Categorical
/CategoricalIndex
, the new categories should be of that type (unless the supplied index is aCategoricalIndex
itself).Discovered working on tests for #41153.