You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/whatsnew/v0.16.0.txt
+50-26Lines changed: 50 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -312,51 +312,54 @@ Categorical Changes
312
312
313
313
.. _whatsnew_0160.api_breaking.categorical:
314
314
315
-
In prior versions, Categoricals that with had an unspecified ordering (meaning no ``ordered`` keyword was passed) were defaulted to have a lexographic ordering of the values and designated as ``ordered`` Categoricals. Going forward, the ``ordered`` keyword in the ``Categorical`` constructor will default to ``False``, so ordering must now be explicit.
315
+
In prior versions, ``Categoricals`` that had an unspecified ordering (meaning no ``ordered`` keyword was passed) were defaulted as ``ordered`` Categoricals. Going forward, the ``ordered`` keyword in the ``Categorical`` constructor will default to ``False``, ordering must now be explicit.
316
316
317
-
Furthermore, previously you *could* change the ``ordered`` attribute of a Categorical by just setting the attribute, e.g. ``cat.ordered=True``; This is now deprecated and you should use ``cat.set_ordered(True)``. This will by default return a **new** object and not modify the existing object.
317
+
Furthermore, previously you *could* change the ``ordered`` attribute of a Categorical by just setting the attribute, e.g. ``cat.ordered=True``; This is now deprecated and you should use ``cat.as_ordered()`` or ``cat.as_unordered()``. These will by default return a **new** object and not modify the existing object. (:issue:`9347`, :issue:`9190`)
318
318
319
319
Previous Behavior
320
320
321
321
.. code-block:: python
322
322
323
-
In [1]: cat = pd.Categorical([0,1,2])
323
+
In [3]: s = Series([0,1,2], dtype='category')
324
324
325
-
In [2]: cat
326
-
Out[2]:
327
-
[0, 1, 2]
325
+
In [4]: s
326
+
Out[4]:
327
+
0 0
328
+
1 1
329
+
2 2
330
+
dtype: category
328
331
Categories (3, int64): [0 < 1 < 2]
329
332
330
-
In [3]: cat.ordered
331
-
Out[3]: True
333
+
In [5]: s.cat.ordered
334
+
Out[5]: True
332
335
333
-
In [4]: cat.ordered=False
336
+
In [6]: s.cat.ordered = False
334
337
335
-
In [5]: cat
336
-
Out[5]:
337
-
[0, 1, 2]
338
+
In [7]: s
339
+
Out[7]:
340
+
0 0
341
+
1 1
342
+
2 2
343
+
dtype: category
338
344
Categories (3, int64): [0, 1, 2]
339
345
340
-
341
346
New Behavior
342
347
343
348
.. ipython:: python
344
349
345
-
cat = pd.Categorical([0,1,2])
346
-
cat
347
-
cat.ordered
348
-
cat.ordered=True
349
-
cat = cat.set_ordered(True)
350
-
cat
351
-
cat.ordered
350
+
s = Series([0,1,2], dtype='category')
351
+
s
352
+
s.cat.ordered
353
+
s = s.cat.as_ordered()
354
+
s
355
+
s.cat.ordered
352
356
353
-
# you can set in the construtor
354
-
cat = pd.Categorical([0,1,2],ordered=True)
355
-
cat
356
-
cat.ordered
357
+
# you can set in the constructor of the Categorical
358
+
s = Series(Categorical([0,1,2],ordered=True))
359
+
s
360
+
s.cat.ordered
357
361
358
-
For ease of creation of series of ``Categoricals``, we have added the ability to pass keywords when calling ``.astype()``, these
359
-
are passed directly to the constructor.
362
+
For ease of creation of series of categorical data, we have added the ability to pass keywords when calling ``.astype()``, these are passed directly to the constructor.
360
363
361
364
.. ipython:: python
362
365
@@ -365,6 +368,27 @@ are passed directly to the constructor.
365
368
s = Series(["a","b","c","a"]).astype('category',categories=list('abcdef'),ordered=False)
366
369
s
367
370
371
+
.. warning::
372
+
373
+
This simple API change may have suprising effects if a user is relying on the previous defaulted behavior implicity. In particular,
374
+
sorting operations with a ``Categorical`` will now raise an error:
0 commit comments