8000 BUG: Ensure Index.astype('category') returns a CategoricalIndex by jschendel · Pull Request #18677 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: Ensure Index.astype('category') returns a CategoricalIndex #18677

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 6 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix tests broken by II.astype('category') changes
  • Loading branch information
jschendel committed Dec 11, 2017
commit a90acecb15c028e56d2e373f6a0c1280c26c7fa4
21 changes: 12 additions & 9 deletions pandas/tests/reshape/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import numpy as np
from pandas.compat import zip

from pandas import (Series, Index, isna,
to_datetime, DatetimeIndex, Timestamp,
Interval, IntervalIndex, Categorical,
from pandas import (Series, isna, to_datetime, DatetimeIndex,
Timestamp, Interval, IntervalIndex, Categorical,
cut, qcut, date_range)
import pandas.util.testing as tm
from pandas.api.types import CategoricalDtype as CDT
Expand All @@ -29,7 +28,8 @@ def test_bins(self):
result, bins = cut(data, 3, retbins=True)

intervals = IntervalIndex.from_breaks(bins.round(3))
expected = intervals.take([0, 0, 0, 1, 2, 0]).astype('category')
intervals = intervals.take([0, 0, 0, 1, 2, 0])
expected = Categorical(intervals, ordered=True)
tm.assert_categorical_equal(result, expected)
tm.assert_almost_equal(bins, np.array([0.1905, 3.36666667,
6.53333333, 9.7]))
Expand All @@ -38,7 +38,8 @@ def test_right(self):
data = np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1, 2.575])
result, bins = cut(data, 4, right=True, retbins=True)
intervals = IntervalIndex.from_breaks(bins.round(3))
expected = intervals.astype('category').take([0, 0, 0, 2, 3, 0, 0])
expected = Categorical(intervals, ordered=True)
expected = expected.take([0, 0, 0, 2, 3, 0, 0])
tm.assert_categorical_equal(result, expected)
tm.assert_almost_equal(bins, np.array([0.1905, 2.575, 4.95,
7.325, 9.7]))
Expand All @@ -47,7 +48,8 @@ def test_noright(self):
data = np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1, 2.575])
result, bins = cut(data, 4, right=False, retbins=True)
intervals = IntervalIndex.from_breaks(bins.round(3), closed='left')
expected = intervals.take([0, 0, 0, 2, 3, 0, 1]).astype('category')
intervals = intervals.take([0, 0, 0, 2, 3, 0, 1])
expected = Categorical(intervals, ordered=True)
tm.assert_categorical_equal(result, expected)
tm.assert_almost_equal(bins, np.array([0.2, 2.575, 4.95,
7.325, 9.7095]))
Expand All @@ -56,7 +58,8 @@ def test_arraylike(self):
data = [.2, 1.4, 2.5, 6.2, 9.7, 2.1]
result, bins = cut(data, 3, retbins=True)
intervals = IntervalIndex.from_breaks(bins.round(3))
expected = intervals.take([0, 0, 0, 1, 2, 0]).astype('category')
intervals = intervals.take([0, 0, 0, 1, 2, 0])
expected = Categorical(intervals, ordered=True)
tm.assert_categorical_equal(result, expected)
tm.assert_almost_equal(bins, np.array([0.1905, 3.36666667,
6.53333333, 9.7]))
Expand Down Expand Up @@ -249,8 +252,8 @@ def test_qcut_nas(self):

def test_qcut_index(self):
result = qcut([0, 2], 2)
expected = Index([Interval(-0.001, 1), Interval(1, 2)]).astype(
'category')
intervals = [Interval(-0.001, 1), Interval(1, 2)]
expected = Categorical(intervals, ordered=True)
tm.assert_categorical_equal(result, expected)

def test_round_frac(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pandas.core.algorithms as algos
from pandas.core.common import _asarray_tuplesafe
import pandas.util.testing as tm
from pandas.core.dtypes.dtypes import CategoricalDtype as CDT
from pandas.compat.numpy import np_array_datetime64_compat
from pandas.util.testing import assert_almost_equal

Expand Down Expand Up @@ -565,8 +566,8 @@ def test_value_counts(self):
# assert isinstance(factor, n)
result = algos.value_counts(factor)
breaks = [-1.194, -0.535, 0.121, 0.777, 1.433]
5044 expected_index = IntervalIndex.from_breaks(breaks).astype('category')
expected = Series([1, 1, 1, 1], index=expected_index)
index = IntervalIndex.from_breaks(breaks).astype(CDT(ordered=True))
expected = Series([1, 1, 1, 1], index=index)
tm.assert_series_equal(result.sort_index(), expected.sort_index())

def test_value_counts_bins(self):
Expand Down
0