|
| 1 | +.. _usage: |
| 2 | + |
| 3 | +Usage |
| 4 | +===== |
| 5 | + |
| 6 | +This document describes how to use the methods and classes provided by |
| 7 | +``cyberpandas``. |
| 8 | + |
| 9 | +We'll assume that the following imports have been performed. |
| 10 | + |
| 11 | +.. ipython:: python |
| 12 | +
|
| 13 | + import ipaddress |
| 14 | + import pandas as pd |
| 15 | + from cyberpandas import IPArray, to_ipaddress |
| 16 | +
|
| 17 | +Parsing |
| 18 | +------- |
| 19 | + |
| 20 | +First, you'll need some IP Address data. Much like pandas' |
| 21 | +:func:`pandas.to_datetime`, ``cyberpandas`` provides :func:`to_ipaddress` for |
| 22 | +converting sequences of anything to a specialized array, :class:`IPArray` in |
| 23 | +this case. |
| 24 | + |
| 25 | +From Strings |
| 26 | +"""""""""""" |
| 27 | + |
| 28 | +:func:`to_ipaddress` can parse a sequence strings where each element represents |
| 29 | +an IP address. |
| 30 | + |
| 31 | +.. ipython:: python |
| 32 | +
|
| 33 | + to_ipaddress([ |
| 34 | + '192.168.1.1', |
| 35 | + '2001:0db8:85a3:0000:0000:8a2e:0370:7334', |
| 36 | + ]) |
| 37 | +
|
| 38 | +You can also parse a *container* of bytes (Python 2 parlance). |
| 39 | + |
| 40 | +.. ipython:: python |
| 41 | +
|
| 42 | + to_ipaddress([ |
| 43 | + b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01\x01', |
| 44 | + b' \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4', |
| 45 | + ]) |
| 46 | +
|
| 47 | +If you have a buffer / bytestring, see :ref:`from_bytes`. |
| 48 | + |
| 49 | +From Integers |
| 50 | +""""""""""""" |
| 51 | + |
| 52 | +IP Addresses are just integers, and :func:`to_ipaddress` can parse a sequence of |
| 53 | +them. |
| 54 | + |
| 55 | +.. ipython:: python |
| 56 | +
|
| 57 | + to_ipaddress([ |
| 58 | + 3232235777, |
| 59 | + 42540766452641154071740215577757643572 |
| 60 | + ]) |
| 61 | +
|
| 62 | +There's also the :meth:`IPArray.from_pyints` method that does the same thing. |
| 63 | + |
| 64 | +.. _from_bytes: |
| 65 | + |
| 66 | +From Bytes |
| 67 | +"""""""""" |
| 68 | + |
| 69 | +If you have a correctly structured buffer of bytes or bytestring, you can |
| 70 | +directly construct an ``IPArray`` without any intermediate copies. |
| 71 | + |
| 72 | +.. ipython:: python |
| 73 | +
|
| 74 | + stream = (b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01' |
| 75 | + b'\x01 \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4') |
| 76 | + IPArray.from_bytes(stream) |
| 77 | +
|
| 78 | +``stream`` is expected to be a sequence of bytes representing IP Addresses (note |
| 79 | +that it's just a bytestring that's be split across two lines for readability). |
| 80 | +Each IP Address should be 128 bits, left padded with 0s for IPv4 addresses. |
| 81 | +In particular, :meth:`IPArray.to_bytes` produces such a sequence of bytes. |
| 82 | + |
| 83 | +Pandas Integration |
| 84 | +------------------ |
| 85 | + |
| 86 | +``IPArray`` satisfies pandas extension array interface, which means that it can |
| 87 | +safely be stored inside pandas' Series and DataFrame. |
| 88 | + |
| 89 | +.. ipython:: python |
| 90 | +
|
| 91 | + values = to_ipaddress([ |
| 92 | + 0, |
| 93 | + 3232235777, |
| 94 | + 42540766452641154071740215577757643572 |
| 95 | + ]) |
| 96 | + values |
| 97 | +
|
| 98 | + ser = pd.Series(values) |
| 99 | + ser |
| 100 | + df = pd.DataFrame({"addresses": values}) |
| 101 | + df |
| 102 | +
|
| 103 | +Most pandas methods that make sense should work. The following section will call |
| 104 | +out points of interest. |
| 105 | + |
| 106 | +Indexing |
| 107 | +"""""""" |
| 108 | + |
| 109 | +If your selection returns a scalar, you get back an |
| 110 | +:class:`ipaddress.IPv4Address` or :class:`ipaddress.IPv6Address`. |
| 111 | + |
| 112 | +.. ipython:: python |
| 113 | +
|
| 114 | + ser[0] |
| 115 | + df.loc[2, 'addresses'] |
| 116 | +
|
| 117 | +Missing Data |
| 118 | +"""""""""""" |
| 119 | + |
| 120 | +The address 0 (``0.0.0.0``) is used to represent missing values. |
| 121 | + |
| 122 | +.. ipython:: python |
| 123 | +
|
| 124 | + ser.isna() |
| 125 | + ser.dropna() |
| 126 | +
|
| 127 | +IP Accessor |
| 128 | +----------- |
| 129 | + |
| 130 | +``cyberpandas`` offers an accessor for IP-specific methods. |
| 131 | + |
| 132 | +.. ipython:: python |
| 133 | +
|
| 134 | + ser.ip.isna |
| 135 | + df['addresses'].ip.is_ipv6 |
0 commit comments