@@ -328,13 +328,23 @@ old categories must be included in the new categories and no new categories are
328
328
Comparisons
329
329
-----------
330
330
331
- Comparing ` Categoricals ` with other objects is possible in two cases:
331
+ Comparing categorical data with other objects is possible in three cases:
332
332
333
- * comparing a categorical Series to another categorical Series, when `categories ` and `ordered ` is
334
- the same or
335
- * comparing a categorical Series to a scalar.
333
+ * comparing equality (``== `` and ``!= ``) to a list-like object (list, Series, array,
334
+ ...) of the same length as the categorical data or
335
+ * all comparisons (``== ``, ``!= ``, ``> ``, ``>= ``, ``< ``, and ``<= ``) of categorical data to
336
+ another categorical Series, when ``ordered==True `` and the `categories ` are the same or
337
+ * all comparisons of a categorical data to a scalar.
336
338
337
- All other comparisons will raise a TypeError.
339
+ All other comparisons, especially "non-equality" comparisons of two categoricals with different
340
+ categories or a categorical with any list-like object, will raise a TypeError.
341
+
342
+ .. note ::
343
+
344
+ Any "non-equality" comparisons of categorical data with a `Series `, `np.array `, `list ` or
345
+ categorical data with different categories or ordering will raise an `TypeError ` because custom
346
+ categories ordering could be interpreted in two ways: one with taking in account the
347
+ ordering and one without.
338
348
339
349
.. ipython :: python
340
350
@@ -353,6 +363,13 @@ Comparing to a categorical with the same categories and ordering or to a scalar
353
363
cat > cat_base
354
364
cat > 2
355
365
366
+ Equality comparisons work with any list-like object of same length and scalars:
367
+
368
+ .. ipython :: python
369
+
370
+ cat == cat_base2
371
+ cat == 2
372
+
356
373
This doesn't work because the categories are not the same:
357
374
358
375
.. ipython :: python
@@ -362,13 +379,9 @@ This doesn't work because the categories are not the same:
362
379
except TypeError as e:
363
380
print (" TypeError: " + str (e))
364
381
365
- .. note ::
366
-
367
- Comparisons with `Series `, `np.array ` or a `Categorical ` with different categories or ordering
368
- will raise an `TypeError ` because custom categories ordering could be interpreted in two ways:
369
- one with taking in account the ordering and one without. If you want to compare a categorical
370
- series with such a type, you need to be explicit and convert the categorical data back to the
371
- original values:
382
+ If you want to do a "non-equality" comparison of a categorical series with a list-like object
383
+ which is not categorical data, you need to be explicit and convert the categorical data back to
384
+ the original values:
372
385
373
386
.. ipython :: python
374
387
0 commit comments