Skip to content

Commit a4843f0

Browse files
jankatinsjreback
authored andcommitted
DOC: equality comparisons with categorical data are now allowed
1 parent d3bb77e commit a4843f0

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

doc/source/categorical.rst

+25-12
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,23 @@ old categories must be included in the new categories and no new categories are
328328
Comparisons
329329
-----------
330330

331-
Comparing `Categoricals` with other objects is possible in two cases:
331+
Comparing categorical data with other objects is possible in three cases:
332332

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.
336338

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.
338348

339349
.. ipython:: python
340350
@@ -353,6 +363,13 @@ Comparing to a categorical with the same categories and ordering or to a scalar
353363
cat > cat_base
354364
cat > 2
355365
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+
356373
This doesn't work because the categories are not the same:
357374

358375
.. ipython:: python
@@ -362,13 +379,9 @@ This doesn't work because the categories are not the same:
362379
except TypeError as e:
363380
print("TypeError: " + str(e))
364381
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:
372385

373386
.. ipython:: python
374387

0 commit comments

Comments
 (0)