@@ -242,6 +242,80 @@ def union_categoricals(to_union, sort_categories=False, ignore_order=False):
242
242
- sort_categories=True and Categoricals are ordered
243
243
ValueError
244
244
Empty list of categoricals passed
245
+
246
+ Examples
247
+ --------
248
+ If you want to combine categoricals that do not necessarily have the same
249
+ categories, the union_categoricals function will combine a list-like of
250
+ categoricals. The new categories will be the union of the categories being
251
+ combined.
252
+
253
+ >>> from pandas.api.types import union_categoricals
254
+
255
+ >>> a = pd.Categorical(["b", "c"])
256
+
257
+ >>> b = pd.Categorical(["a", "b"])
258
+
259
+ >>> union_categoricals([a, b])
260
+ [b, c, a, b]
261
+ Categories (3, object): [b, c, a]
262
+
263
+ By default, the resulting categories will be ordered as they appear in the
264
+ data. If you want the categories to be lexsorted, use sort_categories=True
265
+ argument.
266
+
267
+ >>> union_categoricals([a, b], sort_categories=True)
268
+ [b, c, a, b]
269
+ Categories (3, object): [a, b, c]
270
+
271
+ union_categoricals also works with the “easy” case of combining two
272
+ categoricals of the same categories and order information (e.g. what you
273
+ could also append for).
274
+
275
+ >>> a = pd.Categorical(["a", "b"], ordered=True)
276
+
277
+ >>> b = pd.Categorical(["a", "b", "a"], ordered=True)
278
+
279
+ >>> union_categoricals([a, b])
280
+ [a, b, a, b, a]
281
+ Categories (2, object): [a < b]
282
+
283
+ The below raises TypeError because the categories are ordered and not
284
+ identical.
285
+
286
+ >>> a = pd.Categorical(["a", "b"], ordered=True)
287
+ >>> b = pd.Categorical(["a", "b", "c"], ordered=True)
288
+ >>> union_categoricals([a, b])
289
+ TypeError: to union ordered Categoricals, all categories must be the same
290
+
291
+ Ordered categoricals with different categories or orderings can be combined
292
+ by using the ignore_ordered=True argument.
293
+
294
+ >>> a = pd.Categorical(["a", "b", "c"], ordered=True)
295
+
296
+ >>> b = pd.Categorical(["c", "b", "a"], ordered=True)
297
+
298
+ >>> union_categoricals([a, b], ignore_order=True)
299
+ [a, b, c, c, b, a]
300
+ Categories (3, object): [a, b, c]
301
+
302
+ union_categoricals also works with a CategoricalIndex, or Series containing
303
+ categorical data, but note that the resulting array will always be a plain
304
+ Categorical
305
+
306
+ >>> a = pd.Series(["b", "c"], dtype='category')
307
+
308
+ >>> b = pd.Series(["a", "b"], dtype='category')
309
+
310
+ >>> union_categoricals([a, b])
311
+ [b, c, a, b]
312
+ Categories (3, object): [b, c, a]
313
+
314
+ Notes
315
+ -----
316
+ To learn more about categories, please see `this link
317
+ <http://pandas.pydata.org/pandas-docs/stable/categorical.html#unioning>`.
318
+
245
319
"""
246
320
from pandas import Index , Categorical , CategoricalIndex , Series
247
321
0 commit comments