Skip to main content

Polars extension for IP address parsing and enrichment including geolocation

Project description

Polars IPTools

Polars IPTools is a Rust-based extension to accelerates IP address manipulation and enrichment in Polars dataframes. This library includes various utility functions for working with IPv4 and IPv6 addresses and geoip and anonymization/proxy enrichment using MaxMind databases.

Documentation PyPI - Python Version uv ruff ty License: MIT Claude Gemini

Install

pip install polars-iptools

Examples

Simple enrichments

IPTools' Rust implementation gives you speedy answers to basic IP questions like "is this a private IP?"

>>> import polars as pl
>>> import polars_iptools as ip
>>> df = pl.DataFrame({'ip': ['8.8.8.8', '2606:4700::1111', '192.168.100.100', '172.21.1.1', '172.34.5.5', 'a.b.c.d']})
>>> df.with_columns(ip.is_private(pl.col('ip')).alias('is_private'))
shape: (6, 2)
┌─────────────────┬────────────┐
 ip               is_private 
 ---              ---        
 str              bool       
╞═════════════════╪════════════╡
 8.8.8.8          false      
 2606:4700::1111  false      
 192.168.100.100  true       
 172.21.1.1       true       
 172.34.5.5       false      
 a.b.c.d          false      
└─────────────────┴────────────┘

IP extension types

IPTools provides two Arrow extension types for storing IP addresses efficiently. IPv4 uses 4-byte UInt32 storage; IPAddress uses 16-byte binary and handles both IPv4 and IPv6. Both types survive Parquet and IPC round-trips with dtype preserved.

>>> import polars as pl
>>> import polars_iptools as ip

>>> df = pl.DataFrame({"ip": ["8.8.8.8", "2606:4700::1111", "192.168.1.1", "invalid"]})
>>> df.with_columns(ip.to_address("ip"))
shape: (4, 2)
┌─────────────────┬─────────────────┐
 ip               ip              
 ---              ---             
 str              ip_addr         
╞═════════════════╪═════════════════╡
 8.8.8.8          8.8.8.8         
 2606:4700::1111  2606:4700::1111 
 192.168.1.1      192.168.1.1     
 invalid          null            
└─────────────────┴─────────────────┘

>>> # Write typed column to Parquet — dtype is preserved on read
>>> typed = df.with_columns(ip.to_address("ip"))
>>> typed.write_parquet("/tmp/ips.parquet")
>>> pl.read_parquet("/tmp/ips.parquet").dtypes
[String, IPAddress]

is_in but for network ranges

Pandas and Polars have is_in functions to perform membership lookups. IPTools extends this to enable IP address membership in IP networks. This function works seamlessly with both IPv4 and IPv6 addresses and converts the specified networks into a Level-Compressed trie (LC-Trie) for fast, efficient lookups.

>>> import polars as pl
>>> import polars_iptools as ip
>>> df = pl.DataFrame({'ip': ['8.8.8.8', '1.1.1.1', '2606:4700::1111']})
>>> networks = ['8.8.8.0/24', '2606:4700::/32']
>>> df.with_columns(ip.is_in(pl.col('ip'), networks).alias('is_in'))
shape: (3, 2)
┌─────────────────┬───────┐
 ip               is_in 
 ---              ---   
 str              bool  
╞═════════════════╪═══════╡
 8.8.8.8          true  
 1.1.1.1          false 
 2606:4700::1111  true  
└─────────────────┴───────┘

GeoIP enrichment

Using MaxMind's GeoLite2-ASN.mmdb and GeoLite2-City.mmdb databases, IPTools provides offline enrichment of network ownership and geolocation.

ip.geoip.full returns a Polars struct containing all available metadata parameters. If you just want the ASN and AS organization, you can use ip.geoip.asn.

>>> import polars as pl
>>> import polars_iptools as ip

>>> df = pl.DataFrame({"ip":["8.8.8.8", "192.168.1.1", "2606:4700::1111", "999.abc.def.123"]})
>>> df.with_columns([ip.geoip.full(pl.col("ip")).alias("geoip")])

shape: (4, 2)
┌─────────────────┬─────────────────────────────────┐
 ip               geoip                           
 ---              ---                             
 str              struct[11]                      
╞═════════════════╪═════════════════════════════════╡
 8.8.8.8          {15169,"GOOGLE","","NA","","", 
 192.168.1.1      {0,"","","","","","","",0.0,0. 
 2606:4700::1111  {13335,"CLOUDFLARENET","","","… │
 999.abc.def.123  {null,null,null,null,null,null 
└─────────────────┴─────────────────────────────────┘

>>> df.with_columns([ip.geoip.asn(pl.col("ip")).alias("asn")])
shape: (4, 2)
┌─────────────────┬───────────────────────┐
 ip               asn                   
 ---              ---                   
 str              str                   
╞═════════════════╪═══════════════════════╡
 8.8.8.8          AS15169 GOOGLE        
 192.168.1.1                            
 2606:4700::1111  AS13335 CLOUDFLARENET 
 999.abc.def.123                        
└─────────────────┴───────────────────────┘

Spur enrichment

Spur is a commercial service that provides "data to detect VPNs, residential proxies, and bots". One of its offerings is a Maxmind mmdb format of at most 2,000,000 "busiest" Anonymous or Anonymous+Residential ips.

ip.spur.full returns a Polars struct containing all available metadata parameters.

>>> import polars as pl
>>> import polars_iptools as ip

>>> df = pl.DataFrame({"ip":["8.8.8.8", "192.168.1.1", "999.abc.def.123"]})
>>> df.with_columns([ip.spur.full(pl.col("ip")).alias("spur")])

shape: (3, 2)
┌─────────────────┬─────────────────────────────────┐
 ip               spur                            
 ---              ---                             
 str              struct[7]                       
╞═════════════════╪═════════════════════════════════╡
 8.8.8.8          {0.0,"","","","","",null}       
 192.168.1.1      {0.0,"","","","","",null}       
 999.abc.def.123  {null,null,null,null,null,null 
└─────────────────┴─────────────────────────────────┘

Environment Configuration

IPTools uses two MaxMind databases: GeoLite2-ASN.mmdb and GeoLite2-City.mmdb. You only need these files if you call the geoip functions.

Set the MAXMIND_MMDB_DIR environment variable to tell the extension where these files are located.

export MAXMIND_MMDB_DIR=/path/to/your/mmdb/files
# or Windows users
set MAXMIND_MMDB_DIR=c:\path\to\your\mmdb\files

If the environment is not set, polars_iptools will check two other common locations (on Mac/Linux):

/usr/local/share/GeoIP
/opt/homebrew/var/GeoIP

Spur Environment

If you're a Spur customer, export the feed as spur.mmdb and specify its location using SPUR_MMDB_DIR environment variable.

export SPUR_MMDB_DIR=/path/to/spur/mmdb
# or Windows users
set SPUR_MMDB_DIR=c:\path\to\spur\mmdb

Credit

Developing this extension was super easy by following Marco Gorelli's tutorial and cookiecutter template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_iptools-0.2.1.tar.gz (157.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_iptools-0.2.1-cp310-abi3-win_amd64.whl (6.2 MB view details)

Uploaded CPython 3.10+Windows x86-64

polars_iptools-0.2.1-cp310-abi3-win32.whl (5.5 MB view details)

Uploaded CPython 3.10+Windows x86

polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_armv7l.whl (5.6 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_aarch64.whl (5.3 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (5.4 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.1 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

polars_iptools-0.2.1-cp310-abi3-macosx_11_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

polars_iptools-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl (5.5 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

polars_iptools-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file polars_iptools-0.2.1.tar.gz.

File metadata

  • Download URL: polars_iptools-0.2.1.tar.gz
  • Upload date:
  • Size: 157.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_iptools-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2f4aea275db028b00ab6205484913d2eb3d2bf07090239f8c910398b982d9634
MD5 5a34e3718b9bd9673fcfb719f1223015
BLAKE2b-256 19585193f99b2b722966176314772aa22de76e257d135df239a1b715a992bac5

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1.tar.gz:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7a2d0a0b07c1da29cb7ee130a81fd7a069c49d50743f4810d5bef23cce3e5fee
MD5 7543a5c396c55b8d3f3469530f32bb59
BLAKE2b-256 caf8805a78ce96ac8de6b9aa0c1eb2f9008a3e9979b195482e5d7e141c322f17

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-win_amd64.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-win32.whl.

File metadata

  • Download URL: polars_iptools-0.2.1-cp310-abi3-win32.whl
  • Upload date:
  • Size: 5.5 MB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 9eb73c38cdadaddbde5b4ec631d5cc42105b05f204a7c201ec3e4d2307951dc8
MD5 42c1ae1be4cdb4f7f5355d7af82f5708
BLAKE2b-256 b2b017f19ee9807964c98856ec445d91fe2b56e1f36aa534ffec23e74ee7331d

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-win32.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 630e8ed3dc703861a757abc163246682618649b83939b44c67ef92794fc1f73c
MD5 c537b10c27ccd9226fd490415ac93c97
BLAKE2b-256 c5b4cf6bebca6317cfd2696503c40270a725346a34853a958580d830fe87a5d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 b9e8e9e99c8894eb003425166372f6875876e513d4d7f499bc35b86fedd37dae
MD5 5964b93fbea3555c7c44809ad97f0ba6
BLAKE2b-256 31d190dce305435fe972f199e56d90c9296a10f629974734277f4c46c7dd5943

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_armv7l.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8a04141efc6edba0b16e1c912d9190f04e8a37187f9efffc28a6b7ec440e040e
MD5 a767a987534b8db6d15fc739733c48f9
BLAKE2b-256 57dcc4bc3a5785c1379aacc0f57b9d49dc65e88e7477f28db437843999868a47

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-musllinux_1_2_aarch64.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 f6eab24b80fc4447eff155f32e5f8e8733b65c66044c3f88c4f0787cd83e0602
MD5 6ddc7873d24f5546c98241081cc6c265
BLAKE2b-256 81fe0302202a46b450e1e58b58945d43a75eaa19312a4f1c6773bd1d1af11f17

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a5157b624174ec294a3f5694d38fafb0ad7cc0ff7ca900c6f107e55b77520f54
MD5 49e0346b8fccaa18ba2eb50881204a1d
BLAKE2b-256 7bcbb9ad18d0116bc0a7c42e1e5e791bb367d6dd0ffdc152f44f7058ac30c5af

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 21e65029f33964940824bd77b2804fb2fb1e5596f31b42e7ef78c95af900fde4
MD5 99963a06890935ec637773aa4a2c300b
BLAKE2b-256 b099ad08088892e6e34ce35d9a6e2364c958c4cd129675a4730bbb13868eb21d

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7e45ac992eeace6369bfe96054840ecb82b0f444781737b9236c518b0d21f49e
MD5 a33e31244e5c83492500bae88fda4d92
BLAKE2b-256 aec079b2318354f10e848c4f69e85f2764cb32af444977c853e0e80cbb028fad

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_iptools-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_iptools-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d3cf779f88493561187aaaa02a08b0932cf9adde0816d51445c5d16d1b68399d
MD5 d2e87238c5b130e4d152641ce78bb349
BLAKE2b-256 8608f8f93a19fd4400439a3d6aa77d83b5a10ee7b456e19ec1847d0ad7542951

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_iptools-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish_to_pypi.yml on erichutchins/polars_iptools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page