-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
DEPR: Deprecate ordered=None for CategoricalDtype #26403
Changes from 12 commits
adc0bca
4ad16c9
2da967d
b03eadd
77f171d
cf12a1f
060eb08
c9c2333
bc16e98
99b6a30
d999bd8
fdb5770
c45f159
219f03c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,7 +331,7 @@ def __init__(self, values, categories=None, ordered=None, dtype=None, | |
# sanitize input | ||
if is_categorical_dtype(values): | ||
if dtype.categories is None: | ||
dtype = CategoricalDtype(values.categories, dtype.ordered) | ||
dtype = CategoricalDtype(values.categories, dtype._ordered) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we really need to user the internal one here? (and all others); I really don't want to expose that even to our code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the overview that Jeremy gave above (#26403 (comment)) and this comment for more details on why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok that's fine then, @jschendel can you create an issue for this so we don't forget at removal time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, back at work today so will create the issue later on tonight |
||
elif not isinstance(values, (ABCIndexClass, ABCSeries)): | ||
# sanitize_array coerces np.nan to a string under certain versions | ||
# of numpy | ||
|
@@ -354,7 +354,7 @@ def __init__(self, values, categories=None, ordered=None, dtype=None, | |
codes, categories = factorize(values, sort=True) | ||
except TypeError: | ||
codes, categories = factorize(values, sort=False) | ||
if dtype.ordered: | ||
if dtype._ordered: | ||
# raise, as we don't have a sortable data structure and so | ||
# the user should give us one by specifying categories | ||
raise TypeError("'values' is not ordered, please " | ||
|
@@ -367,7 +367,7 @@ def __init__(self, values, categories=None, ordered=None, dtype=None, | |
"supported at this time") | ||
|
||
# we're inferring from values | ||
dtype = CategoricalDtype(categories, dtype.ordered) | ||
dtype = CategoricalDtype(categories, dtype._ordered) | ||
|
||
elif is_categorical_dtype(values): | ||
old_codes = (values._values.codes if isinstance(values, ABCSeries) | ||
|
@@ -432,7 +432,7 @@ def ordered(self): | |
""" | ||
Whether the categories have an ordered relationship. | ||
""" | ||
return self.dtype.ordered | ||
return self.dtype._ordered | ||
|
||
@property | ||
def dtype(self) -> CategoricalDtype: | ||
|
@@ -846,7 +846,7 @@ def set_categories(self, new_categories, ordered=None, rename=False, | |
""" | ||
inplace = validate_bool_kwarg(inplace, 'inplace') | ||
if ordered is None: | ||
ordered = self.dtype.ordered | ||
ordered = self.dtype._ordered | ||
new_dtype = CategoricalDtype(new_categories, ordered=ordered) | ||
|
||
cat = self if inplace else self.copy() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,8 @@ | |
is_datetime64tz_dtype, is_datetimetz, is_dtype_equal, is_interval_dtype, | ||
is_period, is_period_dtype, is_string_dtype) | ||
from pandas.core.dtypes.dtypes import ( | ||
CategoricalDtype, DatetimeTZDtype, IntervalDtype, PeriodDtype, registry) | ||
CategoricalDtype, DatetimeTZDtype, IntervalDtype, PeriodDtype, | ||
ordered_sentinel, registry) | ||
|
||
import pandas as pd | ||
from pandas import ( | ||
|
@@ -54,7 +55,8 @@ def test_pickle(self): | |
class TestCategoricalDtype(Base): | ||
|
||
def create(self): | ||
return CategoricalDtype() | ||
# TODO(GH 26403): Remove when default ordered becomes False | ||
return CategoricalDtype(ordered=None) | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_pickle(self): | ||
# make sure our cache is NOT pickled | ||
|
@@ -675,7 +677,8 @@ def test_unordered_same(self, ordered): | |
def test_categories(self): | ||
result = CategoricalDtype(['a', 'b', 'c']) | ||
tm.assert_index_equal(result.categories, pd.Index(['a', 'b', 'c'])) | ||
assert result.ordered is None | ||
with tm.assert_produces_warning(FutureWarning): | ||
assert result.ordered is None | ||
|
||
def test_equal_but_different(self, ordered_fixture): | ||
c1 = CategoricalDtype([1, 2, 3]) | ||
|
@@ -804,7 +807,8 @@ def test_categorical_categories(self): | |
|
||
@pytest.mark.parametrize('new_categories', [ | ||
list('abc'), list('cba'), list('wxyz'), None]) | ||
@pytest.mark.parametrize('new_ordered', [True, False, None]) | ||
@pytest.mark.parametrize('new_ordered', [ | ||
True, False, None, ordered_sentinel]) | ||
def test_update_dtype(self, ordered_fixture, new_categories, new_ordered): | ||
dtype = CategoricalDtype(list('abc'), ordered_fixture) | ||
new_dtype = CategoricalDtype(new_categories, new_ordered) | ||
|
@@ -813,11 +817,18 @@ def test_update_dtype(self, ordered_fixture, new_categories, new_ordered): | |
if expected_categories is None: | ||
expected_categories = dtype.categories | ||
|
||
expected_ordered = new_dtype.ordered | ||
if expected_ordered is None: | ||
expected_ordered = new_ordered | ||
if new_ordered is ordered_sentinel or new_ordered is None: | ||
expected_ordered = dtype.ordered | ||
|
||
result = dtype.update_dtype(new_dtype) | ||
# GH 26336 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you give some explanation on what you are testing here (the cases) |
||
if new_ordered is ordered_sentinel and ordered_fixture is True: | ||
with tm.assert_produces_warning(FutureWarning, | ||
check_stacklevel=False): | ||
result = dtype.update_dtype(new_dtype) | ||
else: | ||
result = dtype.update_dtype(new_dtype) | ||
|
||
tm.assert_index_equal(result.categories, expected_categories) | ||
assert result.ordered is expected_ordered | ||
|
||
|
@@ -837,6 +848,14 @@ def test_update_dtype_errors(self, bad_dtype): | |
with pytest.raises(ValueError, match=msg): | ||
dtype.update_dtype(bad_dtype) | ||
|
||
@pytest.mark.parametrize('ordered', [ordered_sentinel, None, True, False]) | ||
def test_ordered_none_default_deprecated(self, ordered): | ||
# GH 26403: CDT.ordered only warns if ordered is not explicitly passed | ||
dtype = CategoricalDtype(list('abc'), ordered=ordered) | ||
warning = FutureWarning if ordered is ordered_sentinel else None | ||
with tm.assert_produces_warning(warning): | ||
dtype.ordered | ||
|
||
|
||
@pytest.mark.parametrize('dtype', [ | ||
CategoricalDtype, | ||
|
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.
Looks like an old whatsnew note references behavior that will be changed, so switched over to an ipython code block to keep the previous behavior static when this gets generated in the future