Spanish Personal Identity Codes

Introduction

The function clean_es_dni() cleans a column containing Spanish personal identity code (DNI) strings, and standardizes them in a given format. The function validate_es_dni() validates either a single DNI strings, a column of DNI strings or a DataFrame of DNI strings, returning True if the value is valid, and False otherwise.

DNI strings can be converted to the following formats via the output_format parameter:

  • compact: only number strings without any seperators or whitespace, like “54362315K”

  • standard: DNI strings with proper whitespace in the proper places. Note that in the case of DNI, the compact format is the same as the standard one.

Invalid parsing is handled with the errors parameter:

  • coerce (default): invalid parsing will be set to NaN

  • ignore: invalid parsing will return the input

  • raise: invalid parsing will raise an exception

The following sections demonstrate the functionality of clean_es_dni() and validate_es_dni().

An example dataset containing DNI strings

[1]:
import pandas as pd
import numpy as np
df = pd.DataFrame(
    {
        "dni": [
            '54362315-K',
            '54362315',
            'BE 428759497',
            'BE431150351',
            "002 724 334",
            "hello",
            np.nan,
            "NULL",
        ],
        "address": [
            "123 Pine Ave.",
            "main st",
            "1234 west main heights 57033",
            "apt 1 789 s maple rd manhattan",
            "robie house, 789 north main street",
            "1111 S Figueroa St, Los Angeles, CA 90015",
            "(staples center) 1111 S Figueroa St, Los Angeles",
            "hello",
        ]
    }
)
df
[1]:
dni address
0 54362315-K 123 Pine Ave.
1 54362315 main st
2 BE 428759497 1234 west main heights 57033
3 BE431150351 apt 1 789 s maple rd manhattan
4 002 724 334 robie house, 789 north main street
5 hello 1111 S Figueroa St, Los Angeles, CA 90015
6 NaN (staples center) 1111 S Figueroa St, Los Angeles
7 NULL hello

1. Default clean_es_dni

By default, clean_es_dni will clean dni strings and output them in the standard format with proper separators.

[2]:
from dataprep.clean import clean_es_dni
clean_es_dni(df, column = "dni")
[2]:
dni address dni_clean
0 54362315-K 123 Pine Ave. 54362315K
1 54362315 main st NaN
2 BE 428759497 1234 west main heights 57033 NaN
3 BE431150351 apt 1 789 s maple rd manhattan NaN
4 002 724 334 robie house, 789 north main street NaN
5 hello 1111 S Figueroa St, Los Angeles, CA 90015 NaN
6 NaN (staples center) 1111 S Figueroa St, Los Angeles NaN
7 NULL hello NaN

2. Output formats

This section demonstrates the output parameter.

standard (default)

[3]:
clean_es_dni(df, column = "dni", output_format="standard")
[3]:
dni address dni_clean
0 54362315-K 123 Pine Ave. 54362315K
1 54362315 main st NaN
2 BE 428759497 1234 west main heights 57033 NaN
3 BE431150351 apt 1 789 s maple rd manhattan NaN
4 002 724 334 robie house, 789 north main street NaN
5 hello 1111 S Figueroa St, Los Angeles, CA 90015 NaN
6 NaN (staples center) 1111 S Figueroa St, Los Angeles NaN
7 NULL hello NaN

compact

[4]:
clean_es_dni(df, column = "dni", output_format="compact")
[4]:
dni address dni_clean
0 54362315-K 123 Pine Ave. 54362315K
1 54362315 main st NaN
2 BE 428759497 1234 west main heights 57033 NaN
3 BE431150351 apt 1 789 s maple rd manhattan NaN
4 002 724 334 robie house, 789 north main street NaN
5 hello 1111 S Figueroa St, Los Angeles, CA 90015 NaN
6 NaN (staples center) 1111 S Figueroa St, Los Angeles NaN
7 NULL hello NaN

3. inplace parameter

This deletes the given column from the returned DataFrame. A new column containing cleaned DNI strings is added with a title in the format "{original title}_clean".

[5]:
clean_es_dni(df, column="dni", inplace=True)
[5]:
dni_clean address
0 54362315K 123 Pine Ave.
1 NaN main st
2 NaN 1234 west main heights 57033
3 NaN apt 1 789 s maple rd manhattan
4 NaN robie house, 789 north main street
5 NaN 1111 S Figueroa St, Los Angeles, CA 90015
6 NaN (staples center) 1111 S Figueroa St, Los Angeles
7 NaN hello

4. errors parameter

coerce (default)

[6]:
clean_es_dni(df, "dni", errors="coerce")
[6]:
dni address dni_clean
0 54362315-K 123 Pine Ave. 54362315K
1 54362315 main st NaN
2 BE 428759497 1234 west main heights 57033 NaN
3 BE431150351 apt 1 789 s maple rd manhattan NaN
4 002 724 334 robie house, 789 north main street NaN
5 hello 1111 S Figueroa St, Los Angeles, CA 90015 NaN
6 NaN (staples center) 1111 S Figueroa St, Los Angeles NaN
7 NULL hello NaN

ignore

[7]:
clean_es_dni(df, "dni", errors="ignore")
[7]:
dni address dni_clean
0 54362315-K 123 Pine Ave. 54362315K
1 54362315 main st 54362315
2 BE 428759497 1234 west main heights 57033 BE 428759497
3 BE431150351 apt 1 789 s maple rd manhattan BE431150351
4 002 724 334 robie house, 789 north main street 002 724 334
5 hello 1111 S Figueroa St, Los Angeles, CA 90015 hello
6 NaN (staples center) 1111 S Figueroa St, Los Angeles NaN
7 NULL hello NaN

4. validate_es_dni()

validate_es_dni() returns True when the input is a valid DNI. Otherwise it returns False.

The input of validate_es_dni() can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.

When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn’t need to specify a column name to be validated.

When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, validate_es_dni() only returns the validation result for the specified column. If user doesn’t specify the column name, validate_es_dni() returns the validation result for the whole DataFrame.

[8]:
from dataprep.clean import validate_es_dni
print(validate_es_dni("54362315-K"))
print(validate_es_dni("54362315"))
print(validate_es_dni('BE 428759497'))
print(validate_es_dni('BE431150351'))
print(validate_es_dni("004085616"))
print(validate_es_dni("hello"))
print(validate_es_dni(np.nan))
print(validate_es_dni("NULL"))
True
False
False
False
False
False
False
False

Series

[9]:
validate_es_dni(df["dni"])
[9]:
0     True
1    False
2    False
3    False
4    False
5    False
6    False
7    False
Name: dni, dtype: bool

DataFrame + Specify Column

[10]:
validate_es_dni(df, column="dni")
[10]:
0     True
1    False
2    False
3    False
4    False
5    False
6    False
7    False
Name: dni, dtype: bool

Only DataFrame

[11]:
validate_es_dni(df)
[11]:
dni address
0 True False
1 False False
2 False False
3 False False
4 False False
5 False False
6 False False
7 False False
[ ]: