String transformer
EmailTransformer
¶
Bases: BaseTransformer
Transforms an email address into multiple features.
Example:
import pandas as pd
from sk_transformers import EmailTransformer
X = pd.DataFrame({"foo": ["person-123@test.com"]})
transformer = EmailTransformer(["foo"])
transformer.fit_transform(X)
foo foo_domain foo_num_of_digits foo_num_of_letters 0 person-123 test 3 6
foo_num_of_special_chars foo_num_of_repeated_chars foo_num_of_words
0 1 1 2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[str]
|
List of features which should be transformed. |
required |
Source code in src/sk_transformers/string_transformer.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
transform(X)
¶
Transforms the one column from X, containing the email addresses, into multiple columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Transformed dataframe containing the extra columns. |
Source code in src/sk_transformers/string_transformer.py
IPAddressEncoderTransformer
¶
Bases: BaseTransformer
Encodes IPv4 and IPv6 strings addresses to a float representation. To
shrink the values to a reasonable size IPv4 addresses are divided by 2^10
and IPv6 addresses are divided by 2^48. Those values can be changed using
the ip4_divisor
and ip6_divisor
parameters.
Example:
import pandas as pd
from sk_transformers import IPAddressEncoderTransformer
X = pd.DataFrame({"foo": ["192.168.1.1", "2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b"]})
transformer = IPAddressEncoderTransformer(["foo"])
transformer.fit_transform(X)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[str]
|
List of features which should be transformed. |
required |
ip4_divisor |
float
|
Divisor for IPv4 addresses. |
10000000000.0
|
ip6_divisor |
float
|
Divisor for IPv6 addresses. |
1e+48
|
error_value |
Union[int, float]
|
Value if parsing fails. |
-999
|
Source code in src/sk_transformers/string_transformer.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
transform(X)
¶
Transforms the column containing the IP addresses to float column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Transformed dataframe. |
Source code in src/sk_transformers/string_transformer.py
PhoneTransformer
¶
Bases: BaseTransformer
Transforms a phone number into multiple features.
Example:
import pandas as pd
from sk_transformers import PhoneTransformer
X = pd.DataFrame({"foo": ["+49123456789", "0044987654321", "3167891234"]})
transformer = PhoneTransformer(["foo"])
transformer.fit_transform(X)
foo foo_national_number foo_country_code
0 +49123456789 0.123457 0.49
1 0044987654321 0.987654 0.44
2 3167891234 -999.000000 -999.00
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[str]
|
List of features which should be transformed. |
required |
national_number_divisor |
float
|
Divider |
1000000000.0
|
country_code_divisor |
flat
|
Divider for |
100.0
|
error_value |
str
|
Value to use if the phone number is invalid or the parsing fails. |
'-999'
|
Source code in src/sk_transformers/string_transformer.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
|
transform(X)
¶
Calculates the similarity of two strings provided in features
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Original dataframe containing the extra column with the calculated similarity. |
Source code in src/sk_transformers/string_transformer.py
StringCombinationTransformer
¶
Bases: BaseTransformer
Concatenates two string columns with a separator in between, but the
concatenated strings are in alphabetical order. This is useful to get
combinations of two strings regardless of the columns they belong in. For
example, a place A
in a departure
column, and a place B
in a
arrival
column and vice verse would both be treated as a AB
in a new
route
column.
Example:
import pandas as pd
from sk_transformers import StringCombinationTransformer
X = pd.DataFrame({"foo": ["a", "b", "c"], "bar": ["b", "a", "a"]})
transformer = StringCombinationTransformer([("foo", "bar", "_")])
transformer.fit_transform(X)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[Tuple[str, str, str]]
|
A list of tuples containing the names of the two columns to be concatenated along with the separator. |
required |
Source code in src/sk_transformers/string_transformer.py
transform(X)
¶
Contatenates two string columns after ordering them alphabetically first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Dataframe containing the additional columns. |
Source code in src/sk_transformers/string_transformer.py
StringSimilarityTransformer
¶
Bases: BaseTransformer
Calculates the similarity between two strings using the gestalt pattern
matching
algorithm from the SequenceMatcher
class.
Example:
import pandas as pd
from sk_transformers import StringSimilarityTransformer
X = pd.DataFrame(
{
"foo": ["abcdefgh", "ijklmnop", "qrstuvwx"],
"bar": ["ghabcdef", "ijklmnop", "qr000000"],
}
)
transformer = StringSimilarityTransformer(("foo", "bar"))
transformer.fit_transform(X)
foo bar foo_bar_similarity
0 abcdefgh ghabcdef 0.75
1 ijklmnop ijklmnop 1.00
2 qrstuvwx qr000000 0.25
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
Tuple[str, str]
|
The two columns that contain the strings for which the similarity should be calculated. |
required |
Source code in src/sk_transformers/string_transformer.py
transform(X)
¶
Calculates the similarity of two strings provided in features
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Original dataframe containing the extra column with the calculated similarity. |
Source code in src/sk_transformers/string_transformer.py
StringSlicerTransformer
¶
Bases: BaseTransformer
Slices all entries of specified string features using the slice()
function.
Note: The arguments for the slice()
function are passed as a tuple. This shares
the python quirk of writing a tuple with a single argument with the trailing comma.
Example:
import pandas as pd
from sk_transformers import StringSlicerTransformer
X = pd.DataFrame({"foo": ["abc", "def", "ghi"], "bar": ["jkl", "mno", "pqr"]})
transformer = StringSlicerTransformer([("foo", (1, 3)), ("bar", (2,))])
transformer.fit_transform(X)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[Tuple[str, Union[Tuple[int], Tuple[int, int], Tuple[int, int, int]], Optional[str]]]
|
The arguments to the |
required |
Source code in src/sk_transformers/string_transformer.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
|
transform(X)
¶
Slices the strings of specified features in the dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Original dataframe with sliced strings in specified features. |
Source code in src/sk_transformers/string_transformer.py
StringSplitterTransformer
¶
Bases: BaseTransformer
Uses the pandas str.split
method to split a column of strings into
multiple columns.
Example:
import pandas as pd
from sk_transformers import StringSplitterTransformer
X = pd.DataFrame({"foo": ["a_b", "c_d", "e_f"], "bar": ["g*h*i", "j*k*l", "m*n*o"]})
transformer = StringSplitterTransformer([("foo", "_", 2), ("bar", "*", 3)])
transformer.fit_transform(X)
foo bar foo_part_1 foo_part_2 bar_part_1 bar_part_2 bar_part_3
0 a_b g*h*i a b g h i
1 c_d j*k*l c d j k l
2 e_f m*n*o e f m n o
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
List[Tuple[str, str, Optional[int]]]
|
A list of tuples where the first element is the name of the feature, the second element is the string separator, and a third optional element is the desired number of splits. If the third element is not provided or is equal to 0 or -1, maximum number of splits are made. |
required |
Source code in src/sk_transformers/string_transformer.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
|
transform(X)
¶
Splits the strings based on a separator character.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
pandas.DataFrame
|
DataFrame to transform. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
pandas.DataFrame: Dataframe containing additional columns containing each split part of the string. |