Skip to content

Commit 6008c08

Browse files
committed
updates
1 parent 7002235 commit 6008c08

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

doc/source/whatsnew/v0.24.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ Other Enhancements
403403
- :meth:`pandas.api.types.is_list_like` has gained a keyword ``allow_sets`` which is ``True`` by default; if ``False``,
404404
all instances of ``set`` will not be considered "list-like" anymore (:issue:`23061`)
405405
- :meth:`Index.to_frame` now supports overriding column name(s) (:issue:`22580`).
406-
- :meth:`Categorical.from_codes` now can take a dtype parameter (:issue:`24398`).
406+
- :meth:`Categorical.from_codes` now can take a ``dtype`` parameter as an alternative to passing ``categories`` and ``ordered`` (:issue:`24398`).
407407
- New attribute :attr:`__git_version__` will return git commit sha of current build (:issue:`21295`).
408408
- Compatibility with Matplotlib 3.0 (:issue:`22790`).
409409
- Added :meth:`Interval.overlaps`, :meth:`IntervalArray.overlaps`, and :meth:`IntervalIndex.overlaps` for determining overlaps between interval-like objects (:issue:`21998`)

pandas/core/arrays/categorical.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -621,27 +621,22 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
621621
categories or dtype.categories, or else is -1 for NaN
622622
categories : index-like, optional
623623
The categories for the categorical. Items need to be unique.
624-
.. versionchanged:: 0.24.0
625-
626-
The `categories` parameter has been made optional.
627624
ordered : bool, optional
628625
Whether or not this categorical is treated as an ordered
629-
categorical. If not given, the resulting categorical will be
630-
unordered.
631-
632-
.. versionchanged:: 0.24.0
633-
634-
The default value has been changed to ``None``. Previously
635-
the default value was ``False``.
626+
categorical. If not given here or in `dtype`, the resulting
627+
categorical will be unordered.
636628
dtype : CategoricalDtype or the string "category", optional
637629
If :class:`CategoricalDtype`, cannot be used together with
638630
`categories` or `ordered`.
639631
640632
.. versionadded:: 0.24.0
641633
634+
When `dtype` is provided, neither `categories` nor `ordered`
635+
should be provided.
636+
642637
Examples
643638
--------
644-
>>> dtype = pd.api.types.CategoricalDtype(['a', 'b'], ordered=True)
639+
>>> dtype = pd.CategoricalDtype(['a', 'b'], ordered=True)
645640
>>> pd.Categorical.from_codes(codes=[0, 1, 0, 1], dtype=dtype)
646641
[a, b, a, b]
647642
Categories (2, object): [a < b]

pandas/tests/arrays/categorical/test_constructors.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,6 @@ def test_from_codes(self):
462462
res = Categorical.from_codes([0, 1, 2], dtype=dtype)
463463
tm.assert_categorical_equal(exp, res)
464464

465-
codes = np.random.choice([0, 1], 5, p=[0.9, 0.1])
466-
dtype = CategoricalDtype(categories=["train", "test"])
467-
Categorical.from_codes(codes, categories=dtype.categories)
468-
Categorical.from_codes(codes, dtype=dtype)
469-
470465
def test_from_codes_with_categorical_categories(self):
471466
# GH17884
472467
expected = Categorical(['a', 'b'], categories=['a', 'b', 'c'])
@@ -516,6 +511,16 @@ def test_from_codes_with_float(self):
516511
match="codes need to be array-like integers"):
517512
Categorical.from_codes(codes, dtype=dtype)
518513

514+
def test_from_codes_with_dtype_raises(self):
515+
msg = 'Cannot specify'
516+
with pytest.raises(ValueError, match=msg):
517+
Categorical([0, 1], categories=['a', 'b'],
518+
dtype=CategoricalDtype(['a', 'b']))
519+
520+
with pytest.raises(ValueError, match=msg):
521+
Categorical([0, 1], ordered=True,
522+
dtype=CategoricalDtype(['a', 'b']))
523+
519524
@pytest.mark.parametrize('dtype', [None, 'category'])
520525
def test_from_inferred_categories(self, dtype):
521526
cats = ['a', 'b']

0 commit comments

Comments
 (0)