@@ -299,6 +299,63 @@ Supplying a ``CategoricalDtype`` will make the categories in each column consist
299
299
df['A'].dtype
300
300
df['B'].dtype
301
301
302
+ .. _whatsnew_023.enhancements.extension:
303
+
304
+ Extending Pandas with Custom Types
305
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
306
+
307
+ Pandas now supports storing array-like objects that aren't necessarily 1-D NumPy
308
+ arrays as columns in a DataFrame or values in a Series. This allows third-party
309
+ libraries to implement extensions to NumPy's types, similar to how pandas
310
+ implemented categoricals, datetimes with timezones, periods, and intervals.
311
+
312
+ As a demonstration, we'll use cyberpandas_, which provides an ``IPArray`` type
313
+ for storing ip addresses.
314
+
315
+ .. code-block:: ipython
316
+
317
+ In [1]: from cyberpandas import IPArray
318
+
319
+ In [2]: values = IPArray([
320
+ ...: 0,
321
+ ...: 3232235777,
322
+ ...: 42540766452641154071740215577757643572
323
+ ...: ])
324
+ ...:
325
+ ...:
326
+
327
+ ``IPArray`` isn't a normal 1-D NumPy array, but because it's a pandas
328
+ ``ExtensionArray``, it can be stored properly inside pandas' containers.
329
+
330
+ .. code-block:: ipython
331
+
332
+ In [3]: ser = pd.Series(values)
333
+ ...:
334
+
335
+ In [4]: ser
336
+ Out[4]:
337
+ 0 0.0.0.0
338
+ 1 192.168.1.1
339
+ 2 2001:db8:85a3::8a2e:370:7334
340
+ dtype: ip
341
+
342
+ Notice that the dtype is ``ip``. The missing value semantics of the underlying
343
+ array are respected:
344
+
345
+ In [5]: ser.isna()
346
+ ...:
347
+ Out[5]:
348
+ 0 True
349
+ 1 False
350
+ 2 False
351
+ dtype: bool
352
+
353
+ For more, see the :ref:`extension types <extending.extension-types>`
354
+ documentation. If you build an extension array, publicize it on our
355
+ :ref:`ecosystem page <ecosystem.extensions>`.
356
+
357
+ .. _cyberpandas: https://cyberpandas.readthedocs.io/en/latest/
358
+
302
359
.. _whatsnew_0230.enhancements.other:
303
360
304
361
Other Enhancements
0 commit comments