Skip to content

Commit 05101b3

Browse files
committed
Change doc string according to comments
1 parent 1c57a07 commit 05101b3

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

pandas/core/arrays/categorical.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,25 @@ def contains(cat, key, container):
200200
return any(loc_ in container for loc_ in loc)
201201

202202

203-
def create_categorical_dtype(values, categories=None, ordered=None,
203+
def create_categorical_dtype(values=None, categories=None, ordered=None,
204204
dtype=None):
205205
"""
206-
Helper function to Construct/return a :class:`CategoricalDtype`.
206+
Construct and return a :class:`~pandas.api.types.CategoricalDtype`.
207207
208-
Construct the CategoricalDtype from typical inputs to :class:`Categorical`.
208+
This is a helper function, and specifically does not do the
209+
factorization step, if that is needed.
209210
210211
Parameters
211212
----------
212-
values : array-like or Categorical, (1-dimensional), optional
213+
values : list-like, optional
214+
The list-like must be 1-dimensional.
213215
categories : list-like, optional
214-
categories for the CategoricalDtype
216+
Categories for the CategoricalDtype.
215217
ordered : bool, optional
216-
designating if the categories are ordered
217-
dtype : CategoricalDtype, optional
218-
Cannot be used in combination with `categories` or `ordered`.
218+
Designating if the categories are ordered.
219+
dtype : CategoricalDtype or the string "category", optional
220+
If ``CategoricalDtype`` cannot be used together with
221+
`categories` or `ordered`.
219222
220223
Returns
221224
-------
@@ -227,10 +230,16 @@ def create_categorical_dtype(values, categories=None, ordered=None,
227230
CategoricalDtype(categories=None, ordered=None)
228231
>>> create_categorical_dtype(categories=['a', 'b'], ordered=True)
229232
CategoricalDtype(categories=['a', 'b'], ordered=True)
230-
>>> dtype = CategoricalDtype(['a', 'b'], ordered=True)
231-
>>> c = Categorical([0, 1], dtype=dtype, fastpath=True)
232-
>>> create_categorical_dtype(c, ['x', 'y'], True, dtype=dtype)
233-
CategoricalDtype(['a', 'b'], ordered=True)
233+
>>> dtype1 = CategoricalDtype(['a', 'b'], ordered=True)
234+
>>> dtype2 = CategoricalDtype(['x', 'y'], ordered=False)
235+
>>> c = Categorical([0, 1], dtype=dtype1, fastpath=True)
236+
>>> create_categorical_dtype(c, ['x', 'y'], ordered=True, dtype=dtype2)
237+
ValueError: Cannot specify `categories` or `ordered` together with `dtype`.
238+
239+
The supplied dtype takes precedence over values's dtype:
240+
241+
>>> create_categorical_dtype(c, dtype=dtype2)
242+
CategoricalDtype(['x', 'y'], ordered=False)
234243
"""
235244
if dtype is not None:
236245
# The dtype argument takes precedence over values.dtype (if any)

pandas/tests/arrays/categorical/test_constructors.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Categorical, CategoricalIndex, DatetimeIndex, Index, Interval,
1414
IntervalIndex, NaT, Series, Timestamp, date_range, period_range,
1515
timedelta_range)
16+
from pandas.core.arrays.categorical import create_categorical_dtype
1617
import pandas.util.testing as tm
1718

1819

@@ -530,3 +531,32 @@ def test_constructor_imaginary(self):
530531
c1 = Categorical(values)
531532
tm.assert_index_equal(c1.categories, Index(values))
532533
tm.assert_numpy_array_equal(np.array(c1), np.array(values))
534+
535+
536+
class TestCreateCategoricalDtype:
537+
dtype1 = CategoricalDtype(['a', 'b'], ordered=True)
538+
dtype2 = CategoricalDtype(['x', 'y'], ordered=False)
539+
c = Categorical([0, 1], dtype=dtype1, fastpath=True)
540+
541+
@pytest.mark.parametrize('values, categories, ordered, dtype, expected', [
542+
[None, None, None, None, CategoricalDtype()],
543+
[None, ['a', 'b'], True, None, dtype1],
544+
[c, None, None, dtype2, dtype2],
545+
[c, ['x', 'y'], False, None, dtype2],
546+
])
547+
def test_create_categorical_dtype(
548+
self, values, categories, ordered, dtype, expected):
549+
result = create_categorical_dtype(values, categories, ordered, dtype)
550+
assert result == expected
551+
552+
@pytest.mark.parametrize('values, categories, ordered, dtype', [
553+
[None, ['a', 'b'], True, dtype2],
554+
[None, ['a', 'b'], None, dtype2],
555+
[None, None, True, dtype2],
556+
])
557+
def test_create_categorical_dtype_raises(self, values, categories, ordered,
558+
dtype):
559+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
560+
561+
with pytest.raises(ValueError, match=msg):
562+
create_categorical_dtype(values, categories, ordered, dtype)

0 commit comments

Comments
 (0)