Techniques that change what is in columns, usually before data is studied.
These are techniques of the "wrangler" stage of a project. Cleaners (see the
cleaners module) change which rows and columns there are. Mungers change
what is in the columns, or make new columns from them, without adding or
removing rows. Each one changes a whole column at once with the vectorized
methods of pandas (such as those of Series.str), so they are fast even
with a lot of data. Like cleaners, mungers learn nothing from the data that
could leak from the test rows into the training rows, so they are safe to use
before the data is split.
Many mungers search text for patterns. A pattern is a regular expression (see
Python's re module), and it is found anywhere in the text, so "revers" is
found in "Reversed and remanded". Characters with special meanings (such as
"." and "(") need a backslash to stand for themselves, as in "F.3d". Set the
"ignorecase" parameter to ignore the difference between capital and
lower-case letters, and start a pattern with an inline flag for other options
(such as "(?s)", which lets "." match line breaks). Missing text matches
nothing.
Contents
Munger: base class for techniques that change the values in columns. AutoCategorize: makes columns with few unique values categorical. Coalesce: takes the first value that is not missing from several columns. CombineFlags: combines flags into one. ConvertTypes: changes the data types of columns. CountPatterns: counts the matches of patterns in text. DeriveColumns: makes columns from expressions of other columns. ExtractAll: keeps every match of a pattern in text. ExtractPattern: keeps the first match of a pattern in text. FlagPatterns: makes columns that say whether text matches patterns. MapPatterns: turns text into values by the first pattern it matches. MapValues: replaces values with others. NormalizeText: collapses spaces in text, and optionally changes its case and removes accents and punctuation. ParseBooleans: turns text such as "yes" and "no" into booleans. ParseDates: turns text into dates. ParseNumbers: turns text such as "$1,234.50" into numbers. ReplaceText: replaces the matches of patterns in text. SplitText: splits text into columns where a pattern matches. StripText: trims spaces from (and optionally lowercases) text.
AutoCategorize(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Makes columns with few unique values categorical.
Text and number columns with threshold unique values or fewer become the
pandas "category" type, so they are encoded (rather than scaled) by the
analyst. Boolean columns are not changed.
munge(data, columns=None, threshold=options._CATEGORY_THRESHOLD, **kwargs)
¶
Makes columns with threshold unique values or fewer categorical.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to check. Defaults to |
None
|
threshold
|
int
|
most unique values that a categorical column can have.
Defaults to |
_CATEGORY_THRESHOLD
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with categorical columns. |
Source code in src/amos/mungers.py
Coalesce(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Takes the first value that is not missing from several columns.
This combines columns that hold the same information from different sources, such as a date from a case's own data and a date found in its text: the first column is used where it has a value, and the next column fills in where it does not, and so on.
munge(data, *, columns=None, name=None, **kwargs)
¶
Makes a column of the first value in columns that is not missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
the columns to take values from, in order of preference.
Defaults to |
None
|
name
|
str | None
|
name of the column to make. Defaults to |
None
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the combined column. It is missing only where every
one of |
Source code in src/amos/mungers.py
CombineFlags(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Combines flags into one: whether any or all are true, or how many are.
For example, a case is about any criminal issue if any of its flags of
criminal issues ("criminal_search", "criminal_sentence", and so on) are
true. Flags are boolean columns or numbers (where 0 is false), such as
those that flag_patterns makes. A missing value is false.
munge(data, *, columns=None, name=None, how='any', **kwargs)
¶
Makes a column that combines the flags in columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
the flags to combine. Defaults to |
None
|
name
|
str | None
|
name of the column to make. It is needed if there are
|
None
|
how
|
str
|
"any" (true if any flag is true), "all" (true if every flag is true), or "count" (the number of flags that are true). Defaults to "any". |
'any'
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
if a column is not a flag. |
ValueError
|
if |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the combined column: a boolean column, or whole numbers for "count". |
Source code in src/amos/mungers.py
ConvertTypes(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Changes the data types of columns.
Wraps
pandas.DataFrame.astype
from pandas.
munge(data, types=None, **kwargs)
¶
Changes the type of each column in types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
types
|
Mapping[str, Any] | None
|
|
None
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the new types. |
Source code in src/amos/mungers.py
CountPatterns(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Counts the matches of patterns in text.
Wraps
pandas.Series.str.count
from pandas.
munge(data, *, column=None, patterns=None, ignorecase=False, **kwargs)
¶
Makes a column with the number of matches of each pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
column
|
str | None
|
the column of text to search. |
None
|
patterns
|
Mapping[str, str | Sequence[str]] | None
|
|
None
|
ignorecase
|
bool
|
whether to ignore the difference between capital and
lower-case letters. Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with a column of counts (whole numbers) for each name in
|
Source code in src/amos/mungers.py
DeriveColumns(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Makes columns from expressions of other columns, such as "a / b".
Wraps
pandas.DataFrame.eval
from pandas.
munge(data, *, expressions=None, **kwargs)
¶
Makes a column from each expression, in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
expressions
|
Mapping[str, str] | None
|
|
None
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if an expression is an assignment (such as "a = b"), rather than an expression. |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the new columns. |
Source code in src/amos/mungers.py
ExtractAll(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Keeps every match of a pattern in text.
Wraps
pandas.Series.str.findall
from pandas.
munge(data, *, column=None, pattern=None, name=None, separator=', ', ignorecase=False, **kwargs)
¶
Keeps every match of pattern in column, joined together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
column
|
str | None
|
the column of text to search. |
None
|
pattern
|
str | None
|
the pattern to find. If it has a group, the text of the
group is kept rather than the whole match. Defaults to |
None
|
name
|
str | None
|
name of the column to make. Defaults to |
None
|
separator
|
str
|
text put between the matches. Defaults to ", ". |
', '
|
ignorecase
|
bool
|
whether to ignore the difference between capital and
lower-case letters. Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the matches. Text without a match is missing. |
Source code in src/amos/mungers.py
ExtractPattern(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Keeps the first match of a pattern in text (or the groups of a match).
A pattern with named groups makes a column for each group. For example,
"(?P
Wraps
pandas.Series.str.extract
from pandas.
munge(data, *, column=None, pattern=None, name=None, ignorecase=False, **kwargs)
¶
Keeps the first match of pattern in column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
column
|
str | None
|
the column of text to search. |
None
|
pattern
|
str | None
|
the pattern to find. If it has one unnamed group, the
text of the group is kept rather than the whole match. If its
groups are named, each group makes a column of its name.
Defaults to |
None
|
name
|
str | None
|
name of the column to make (unless the groups are named).
Defaults to |
None
|
ignorecase
|
bool
|
whether to ignore the difference between capital and
lower-case letters. Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the matches. Text without a match is missing. |
Source code in src/amos/mungers.py
FlagPatterns(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Makes columns that say whether text matches patterns.
For example, {"reversed": "revers|vacat"} makes a "reversed" column that is true for each row whose text mentions a reversal or vacatur.
Wraps
pandas.Series.str.contains
from pandas.
munge(data, *, column=None, patterns=None, ignorecase=False, **kwargs)
¶
Makes a boolean column for each pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
column
|
str | None
|
the column of text to search. |
None
|
patterns
|
Mapping[str, str | Sequence[str]] | None
|
|
None
|
ignorecase
|
bool
|
whether to ignore the difference between capital and
lower-case letters. Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with a boolean column for each name in |
Source code in src/amos/mungers.py
MapPatterns(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Turns text into values by the first pattern that it matches.
For example, {"first circuit": 1, "second circuit": 2} turns the names of courts into their numbers. The patterns are tried in order, so put the most specific first.
munge(data, *, column=None, patterns=None, name=None, default=None, ignorecase=False, **kwargs)
¶
Makes a column with the value of the first pattern that matches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
column
|
str | None
|
the column of text to search. |
None
|
patterns
|
Mapping[str, Any] | None
|
|
None
|
name
|
str | None
|
name of the column to make. Defaults to |
None
|
default
|
Any
|
the value of text that matches no pattern. Defaults to
|
None
|
ignorecase
|
bool
|
whether to ignore the difference between capital and
lower-case letters. Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the values. Their type is found from the values
(and |
Source code in src/amos/mungers.py
MapValues(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Replaces values with others, such as "N/A" with a missing value.
Only whole values that are the same as one in values are replaced (use
replace_text to change part of the text). Other values are kept. A
categorical column stays categorical.
munge(data, *, columns=None, values=None, **kwargs)
¶
Replaces the values in columns that are keys of values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to change. Defaults to |
None
|
values
|
Mapping[Any, Any] | None
|
|
None
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the new values. The type of a changed column is
found from its new values, as |
Source code in src/amos/mungers.py
Munger(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Operation, ABC
Base class for techniques that change the values in columns.
A subclass writes a munge method, which takes the data and returns it
with changed or new columns, and the same rows. The dataset's history
records the columns whose values or types changed ("changed") and the new
columns ("created").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name used to refer to the technique in a workflow. Defaults to
|
''
|
contents
|
str | Callable[..., Any] | None
|
not used by most mungers. Defaults to |
None
|
parameters
|
GenericDict
|
keyword arguments for |
dict()
|
implement(item, **kwargs)
¶
Changes the columns of item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Dataset
|
the dataset to change. |
required |
**kwargs
|
Any
|
parameters for |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Returns:
| Type | Description |
|---|---|
Dataset
|
The changed dataset. |
Source code in src/amos/mungers.py
munge(data, **kwargs)
abstractmethod
¶
Returns data with changed or new columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. It may be changed in place. |
required |
**kwargs
|
Any
|
parameters for munging. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The changed data, with the same rows. |
Source code in src/amos/mungers.py
NormalizeText(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Normalizes the spaces, case, accents, and punctuation of text.
Every run of spaces, tabs, and line breaks becomes one space, and spaces are trimmed from both ends. It can also change the case of the text, remove accents (so "é" becomes "e"), and remove punctuation (so "U.S." becomes "US" and "O'Brien" becomes "OBrien"). This makes text that was written differently the same, so it can be counted, compared, or encoded as one value.
munge(data, *, columns=None, case=None, remove_accents=False, remove_punctuation=False, **kwargs)
¶
Normalizes the text in columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to change. Defaults to |
None
|
case
|
str | None
|
"lower", "upper", or "title" (which capitalizes each word).
Defaults to |
None
|
remove_accents
|
bool
|
whether to remove accents from letters. Defaults to
|
False
|
remove_punctuation
|
bool
|
whether to remove punctuation. Defaults to
|
False
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with normalized text. Empty text becomes a missing value. |
Source code in src/amos/mungers.py
ParseBooleans(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Turns text such as "yes" and "no" into booleans.
Text is compared without regard to case or the spaces around it. Numbers
are true if they are 1 and false if they are 0. Anything else is missing,
so the columns have the pandas "boolean" type, which allows missing
values.
munge(data, *, columns=None, true_values=_TRUE, false_values=_FALSE, **kwargs)
¶
Turns the values in columns into booleans.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to change. Defaults to |
None
|
true_values
|
Sequence[Any]
|
words that mean true. Defaults to "true", "t", "yes", "y", and "1". |
_TRUE
|
false_values
|
Sequence[Any]
|
words that mean false. Defaults to "false", "f", "no", "n", and "0". |
_FALSE
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with booleans. Columns that are already boolean are not changed. |
Source code in src/amos/mungers.py
ParseDates(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Turns text into dates.
Text that is not a date becomes a missing value, rather than an error (as
it would with convert_types).
Wraps
pandas.to_datetime
from pandas.
munge(data, *, columns=None, date_format=None, dayfirst=False, **kwargs)
¶
Turns the text in columns into dates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to change. Defaults to |
None
|
date_format
|
str | None
|
the format of the dates, with the codes of Python's
|
None
|
dayfirst
|
bool
|
whether the day comes before the month in dates such as
"01/02/2020". Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with dates. |
Source code in src/amos/mungers.py
ParseNumbers(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Turns text such as "$1,234.50" into numbers.
The first number in the text is kept, so "$1,234.50" becomes 1234.5, "12%" becomes 12, and "about 3 years" becomes 3. Text without a number becomes a missing value.
Wraps
pandas.to_numeric
from pandas.
munge(data, *, columns=None, thousands=',', decimal='.', **kwargs)
¶
Turns the text in columns into numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to change. Defaults to |
None
|
thousands
|
str
|
the character that separates thousands. Defaults to
",". Set it to "." (and |
','
|
decimal
|
str
|
the character before the decimals. Defaults to ".". |
'.'
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with numbers. Columns that are already numbers are not changed. |
Source code in src/amos/mungers.py
ReplaceText(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Replaces the matches of patterns in text.
For example, {"\s(.?)": ""} removes everything in parentheses.
Wraps
pandas.Series.str.replace
from pandas.
munge(data, *, columns=None, patterns=None, ignorecase=False, **kwargs)
¶
Replaces every match of each pattern, in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to change. Defaults to |
None
|
patterns
|
Mapping[str, str | None] | None
|
|
None
|
ignorecase
|
bool
|
whether to ignore the difference between capital and
lower-case letters. Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with the text replaced. |
Source code in src/amos/mungers.py
SplitText(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Splits text into columns where a pattern matches.
For example, the pattern " v. " and the names "party1, party2" split a caption such as "Smith v. Jones" into "Smith" and "Jones". The text is split at the first matches, so the last column has the rest of the text. The original column is kept.
Wraps
pandas.Series.str.split
from pandas.
munge(data, *, column=None, pattern=None, names=None, ignorecase=False, **kwargs)
¶
Splits the text in column into the columns in names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
column
|
str | None
|
the column of text to split. |
None
|
pattern
|
str | None
|
the pattern to split at. It cannot have groups (use
"(?:...)" to group without one). Defaults to |
None
|
names
|
Sequence[str] | None
|
names of the columns to make, one for each piece of the text. There must be at least two. |
None
|
ignorecase
|
bool
|
whether to ignore the difference between capital and
lower-case letters. Defaults to |
False
|
**kwargs
|
Any
|
not used. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if there are fewer than two |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with a column for each piece, trimmed of spaces. A piece that is empty (or that the text does not have) is missing. |
Source code in src/amos/mungers.py
StripText(contents=None, name='', parameters=dict())
dataclass
¶
Bases: Munger
Trims spaces from text, and optionally makes it lowercase.
Wraps
pandas.Series.str.strip
from pandas.
munge(data, *, columns=None, lowercase=False, **kwargs)
¶
Trims spaces from the text in columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
the data to change. |
required |
columns
|
Sequence[str] | None
|
columns to change. Defaults to |
None
|
lowercase
|
bool
|
whether to make the text lowercase too. Defaults to
|
False
|
**kwargs
|
Any
|
not used. |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The data, with trimmed text. Empty text becomes a missing value. |