A Python toolkit for simplifying data workflows.
>=3.11 · MIT license · version 0.6.6
Overview · Installation · Quick start · Usage · Advanced · Reference · Notes · Development
What is bynat?
bynat is a Python toolkit for the small, fiddly jobs around tabular data. It works on pandas DataFrame objects (a DataFrame is pandas' name for a table of rows and columns) and includes SQLite-backed caching, so slow lookups are not repeated.
What you can do with it
- Tidy columns: rename, drop, reorder, move, and reshape columns, and split list values into separate columns.
- Work with text: extract keywords, truncate text at a delimiter, and categorize rows by reference keywords.
- Profile data quality: check completeness, duplicate rows, column types, and per-type statistics without changing your data.
- Handle outliers and gaps: find outliers and fill missing values by comparing each row with similar rows (grouped by a hierarchy of columns).
- Enrich data: translate values, geocode missing coordinates, convert amounts to USD, and extract entities with spaCy.
- Read and write files: load CSV and JSON files, and save data to JSON or a SQL database.
- Cache results: store lookup results in a SQLite database and reuse them between runs.
Ways to use it
You can mix these styles in the same project:
- Class-based API:
BaseData,JsonFile, andCsvFile. This is the primary API. - pandas accessor: the optional
df.bynataccessor adds the same cleaning operations to ordinarypandas.DataFrameobjects. - Caching API:
CacheManagerstores per-function dictionaries in a SQLite database.
New here? Install bynat, then go to the Quick start.
Project status
0.6.6 is the version declared in pyproject.toml. See CHANGELOG.md for behavior changes since 0.1.1, and ROADMAP.md for planned work.
Installation
Install from PyPI:
python -m pip install bynat
To work from a repository checkout, install in editable mode:
python -m pip install -e .
Requirements
- Python
>=3.11 - Required packages:
numpyandpandas
Optional integrations
Features that call outside libraries are grouped into optional extras in pyproject.toml:
| Extra | Installs | Used for |
|---|---|---|
translation |
deep-translator, langdetect |
Translation and language detection |
geocoding |
geopy |
Geocoding |
currency |
Babel, pycountry, yfinance |
Currency conversion |
categorization |
pyahocorasick (imported as ahocorasick) |
Keyword matching |
nlp |
spacy |
Entity extraction |
sql |
SQLAlchemy |
Saving to SQL |
Install an extra when you need it:
python -m pip install "bynat[translation]"
Optional integrations are imported only when a method uses them. Basic imports and core DataFrame operations work without them. If a method needs a missing integration, it raises an ImportError that names the extra to install.
Not bundled:
extract_entities_nlpalso needs a compatible spaCy model (default:en_core_web_sm), andsave_to_sqlneeds the database driver required by your SQLAlchemy URL. Neither model files nor database drivers are included.
Quick start
import bynat registers the df.bynat accessor on every pandas.DataFrame.
import bynat
import pandas as pd
records = pd.DataFrame({
"user.display_name": ["Ada", "Grace"],
"user_id": [1, 2],
"description": ["Blue shoes", "Red shirt"],
})
cleaned = records.bynat.rename_all_DOTS().bynat.drop_by_substring("user")
This takes two steps:
rename_all_DOTSreplaces dots in column names (with_by default).drop_by_substring("user")drops every column whose name containsuser.
Only the description column is left.
Prefer a class over the accessor? BaseData is a pandas.DataFrame subclass that does the same cleanup:
from bynat.cleaning import BaseData
cleaned = BaseData(records).rename_all_DOTS().drop_by_substring("user")
Both versions return a new object and leave records unchanged. Accessor methods always return new DataFrames. Class methods also return a new object by default.
Using bynat
bynat has two parts that work together:
- Cleaning (
bynat.cleaning): column, text, statistics, enrichment, and file methods. - Caching (
bynat.caching): SQLite-backed storage for lookup results.
Cleaning methods that call outside services (translation, geocoding, NLP extraction, and exchange-rate lookup) use a shared CacheManager. See Caching.
Import styles
There are three supported ways to import bynat. None is required; use whichever reads best in your code.
| Style | Example | Good when |
|---|---|---|
| Module imports | import bynat.cleaning as climport bynat.caching as ch |
You prefer a module namespace, such as cl.BaseData or ch.CacheManager. |
| Direct imports | from bynat.cleaning import BaseData |
You want specific objects under their own names. |
| pandas accessor | import pandas as pdimport bynat as by |
You already have a plain DataFrame and want to call df.bynat.<method>(...). |
The accessor is always called bynat (df.bynat...), whatever alias you give the module. Importing bynat or any of its submodules registers it.
Supported imports
import bynat # registers the df.bynat accessor
from bynat import ProfileReport, profile, read_csv, read_json
from bynat.caching import CacheManager, shared_cache_manager
from bynat.cleaning import BaseData, CsvFile, JsonFile
from bynat.cleaning.enrich import Geocoder, RateSource, Translator
What each module provides:
| Module | Public objects |
|---|---|
bynat |
read_json, read_csv, BynatAccessor, profile, ProfileReport |
bynat.caching |
CacheManager, shared_cache_manager, CACHE_DIR_ENV_VAR, DEFAULT_CACHE_DIR |
bynat.cleaning |
BaseData, JsonFile, CsvFile, profile, ProfileReport, DatasetProfile, ColumnProfile, plus BynatAccessor, read_json, read_csv |
bynat.cleaning.enrich |
Provider protocols Translator, Geocoder, RateSource, and their defaults GoogleTranslatorProvider, NominatimGeocoder, YFinanceRateSource |
Import submodules explicitly. The top-level
bynatnamespace exposes onlyread_json,read_csv,BynatAccessor,profile, andProfileReport. Import the class API frombynat.cleaningand the cache API frombynat.caching.
Class-based API and accessor: key differences
| Class-based API | pandas accessor | |
|---|---|---|
| Works on | BaseData, JsonFile, CsvFile |
Any pandas.DataFrame |
| Return value | A new object by default | Always a new DataFrame |
inplace argument |
Accepted where in-place operation is meaningful | Not accepted |
| Cache directory | cache_dir constructor argument |
df.bynat.with_cache(...) |
The class-based API
BaseData, JsonFile, and CsvFile are supported public classes. BaseData is a pandas.DataFrame subclass. All three use the same cleaning code as the accessor.
| Class | Constructor parameters | Equivalent accessor or function form |
|---|---|---|
BaseData |
data=None, cache_dir=None, *args, **kwargs |
pandas.DataFrame(...) with df.bynat; use df.bynat.with_cache(cache_dir) to set a cache directory |
JsonFile |
data=None, json_file_path=None, key=None, cache_dir=None, *args, **kwargs |
bynat.read_json(json_file_path, key=None), then df.bynat methods |
CsvFile |
data=None, csv_file_path=None, cache_dir=None, read_kwargs=None, *args, **kwargs |
bynat.read_csv(csv_file_path, **read_kwargs), then df.bynat methods |
How inplace works:
- Every
BaseDatamethod in the method reference acceptsinplace=Falsewhere in-place operation is meaningful. - With
inplace=True, the method changes the original object and returnsNone(or the documented statistics result). This is supported and is not scheduled for removal. - Without it, every method returns a new object, including early exits that previously returned the original object.
BaseData creates its CacheManager on first use and shares it with every frame pandas derives from it.
The pandas accessor
The accessor is an optional extra way to use bynat on ordinary DataFrames. It does not replace the class-based API. It exposes the same cleaning operations through df.bynat.
- Accessor methods return new DataFrames and do not accept
inplace. df.bynat.with_cache(...)returns an accessor that caches in another directory or in an explicitCacheManager.bynat.read_csvandbynat.read_jsonreturn plain DataFrames that you can use the accessor on right away.
The same job in both styles
# Class-based style
from bynat.cleaning import JsonFile
jobs = JsonFile(json_file_path="jobs.json", key="items", cache_dir="cache")
jobs = jobs.rename_all_DOTS()
jobs.translate_categorical_column("status", inplace=True)
# Accessor style
import bynat
jobs = bynat.read_json("jobs.json", key="items")
jobs = jobs.bynat.rename_all_DOTS()
jobs = jobs.bynat.with_cache("cache").translate_categorical_column("status")
The class-based version uses JsonFile and changes the object in place with inplace=True. The accessor version works on a plain DataFrame and reassigns the result.
Common tasks
Extract keywords from text. This continues with the records DataFrame from the quick start:
labeled = records.bynat.extract_keywords(
"description",
colors=["blue", "red"],
)
Profile a DataFrame. This also uses records from the quick start. See Data profiling for the full report:
report = records.bynat.profile()
print(report)
# ProfileReport(rows=2, columns=3, missing=0 (0.0%), duplicate_rows=0)
Read and write files. The loader helpers return plain DataFrames:
import bynat
from_csv = bynat.read_csv("input.csv", sep=";")
from_json = bynat.read_json("input.json", key="items")
from_csv.bynat.save_to_json("output.json", orient="records")
How the cleaning code is organized
Each function in the columns, text, stats, enrich, and io modules takes a DataFrame as its first argument and returns a new one by default. The classes and the accessor are thin layers over these functions. See the module layout.
Data profiling
Profiling gives you a quick quality check of a table before you clean or model it. A profile reports completeness, duplicate rows, memory use, column types, and statistics for each column. It only reads your data. The input DataFrame, its index, and its column names are left unchanged.
import bynat
import pandas as pd
df = pd.DataFrame({
"customer_id": [1001, 1002, 1003, 1004, 1004],
"name": ["Alice", "Bob", "Charlie", "David", "David"],
"age": [28.0, 34.0, None, 45.0, 45.0],
"active": [True, True, False, True, True],
"joined": pd.to_datetime(["2021-01-15", "2022-03-20", "2023-05-12", "2024-07-01", "2024-07-01"]),
})
report = df.bynat.profile()
print(report)
# ProfileReport(rows=5, columns=5, missing=1 (4.0%), duplicate_rows=1)
report.dataset.duplicate_rows # 1
report.columns["age"].kind # "numeric"
report.columns["age"].missing_count # 1
report.columns["age"].stats["mean"] # 38.0
Ways to call it
All four forms return the same report:
| Form | Call |
|---|---|
| pandas accessor | df.bynat.profile(*columns, output_type=None) |
| Class-based API | BaseData(df).profile(*columns, output_type=None) |
| Top-level function | bynat.profile(df, *columns, output_type=None) |
| Cleaning function | bynat.cleaning.profile(df, *columns, output_type=None) |
Passing something other than a pandas.DataFrame to the function forms raises a TypeError.
Choosing columns
By default, every column is profiled. To profile only some columns, pass their names as separate arguments or as one list, tuple, or set. A set works, but its order is arbitrary.
df.bynat.profile() # all columns
df.bynat.profile("age", "name") # names as arguments
df.bynat.profile(["age", "name"]) # names as one list
A name that is not in the DataFrame raises a KeyError.
Output types
Use output_type to choose what you get back:
output_type |
Returns | Use it for |
|---|---|---|
None (default) or ProfileReport |
ProfileReport |
Working with the report in Python. |
dict |
dict |
Logging or sending the report as JSON. |
pandas.DataFrame |
pandas.DataFrame |
A table with one row per profiled column. |
A BaseData subclass |
An object of that class | The same table, wrapped in your class. |
Any other value raises a TypeError.
import json
import pandas as pd
from bynat.cleaning import BaseData, profile
report = profile(df) # ProfileReport
as_dict = profile(df, output_type=dict) # plain dictionary
summary = profile(df, output_type=pd.DataFrame) # one row per column
wrapped = profile(df, output_type=BaseData) # table as BaseData
json_text = json.dumps(as_dict, indent=2)
Working with a report
A ProfileReport has two parts: report.dataset (a DatasetProfile) and report.columns (a dictionary of ColumnProfile objects keyed by column name). You can read them as attributes or like a dictionary:
report.dataset.row_count
report["dataset"]["row_count"]
report.columns["age"].missing_proportion
report.column_listis the same set ofColumnProfileobjects as an ordered list. Use it if your DataFrame has repeated column names, becausereport.columnsholds one entry per name.report.to_dict()returns the dataset and column profiles as plain Python values.report.to_dataframe()returns one row per column. Column statistics become columns with astat_prefix, such asstat_mean. A statistic that does not apply to a column's kind is left empty (NaN) in that row.
For example, to list columns with missing values or candidate keys:
with_missing = [c.name for c in report.column_list if c.missing_count > 0]
candidate_keys = [c.name for c in report.column_list if c.is_unique and c.missing_count == 0]
table = report.to_dataframe()
table[["column", "kind", "missing_proportion", "distinct_count", "stat_mean"]]
report.to_dict() and profile(..., output_type=dict) use only plain Python values, with dates written as ISO 8601 strings. They work with json.dumps as long as your column names are plain strings or numbers.
What a report contains
Dataset fields
report.dataset is a DatasetProfile:
| Field | Type | Meaning |
|---|---|---|
row_count |
int |
Number of rows. |
column_count |
int |
Number of profiled columns. |
total_cells |
int |
row_count * column_count. |
missing_count |
int |
Number of missing cells. |
missing_proportion |
float |
Missing cells divided by total_cells. |
duplicate_rows |
int |
Number of repeated rows. The first copy of a row is not counted. |
duplicate_proportion |
float |
duplicate_rows divided by row_count. |
empty_column_count |
int |
Number of columns with no non-null values. |
memory_bytes |
int |
Memory use in bytes, from memory_usage(deep=True) (includes the index). 0 for an empty frame. |
dtype_counts |
dict[str, int] |
Number of columns for each pandas dtype name. |
Column fields
Each entry in report.columns is a ColumnProfile:
| Field | Type | Meaning |
|---|---|---|
name |
any | The column name. |
dtype |
str |
The pandas dtype name. |
inferred_type |
str |
The more specific type pandas infers from the values, such as integer or string. |
kind |
str |
The broad group: numeric, categorical, boolean, datetime, or other. |
count |
int |
Number of non-null values. |
missing_count |
int |
Number of missing values. |
missing_proportion |
float |
Missing values divided by the row count. |
distinct_count |
int |
Number of different non-null values. |
distinct_proportion |
float |
distinct_count divided by count. |
is_unique |
bool |
True when there is at least one non-null value and all of them are different. |
stats |
dict |
Statistics that depend on kind. See below. |
Statistics by kind
Every column of the same kind has the same keys in stats. A value that cannot be computed is None, not NaN.
| Kind | Keys in stats |
|---|---|
numeric |
min, max, mean, std, median, q25, q75, iqr, zeros_count, zeros_proportion, outliers_count |
categorical |
top, top_frequency, top_proportion, min_length, max_length, mean_length |
boolean |
true_count, false_count, true_proportion |
datetime |
min, max, range_days |
other |
top, top_frequency, top_proportion |
Notes on these statistics:
- Numeric:
stdis the sample standard deviation and needs at least 2 non-null values.outliers_countcounts values belowq25 - 1.5 * iqror aboveq75 + 1.5 * iqr, and needs at least 5 non-null values. When there are no non-null values,zeros_countis0,zeros_proportionis0.0, and the other statistics areNone. - Categorical and other:
topis the most frequent value, with its count intop_frequencyand its share of non-null values intop_proportion. These areNonewhen there are no non-null values. - Categorical lengths:
min_length,max_length, andmean_lengthare set only when every non-null value is a string. Otherwise they areNone. - Boolean:
true_countandfalse_countare0when there are no non-null values, andtrue_proportionisNone. - Datetime:
minandmaxare ISO 8601 strings.range_daysis the time between them in days, including fractions.
How columns are classified
bynat assigns each column a kind:
| Kind | Columns |
|---|---|
boolean |
Boolean dtypes, including pandas boolean, and columns whose values are all booleans. |
datetime |
Datetime dtypes, both naive and timezone-aware, and columns of date or datetime objects. |
numeric |
Numeric dtypes, including pandas Int64 and Float64, and columns of integers, floats, or decimals. |
categorical |
pandas Categorical, the string dtype, and columns whose non-null values are all strings. |
other |
Everything else, such as mixed types, arbitrary Python objects, and timedeltas. |
Empty and all-null columns do not raise errors. When a column has no non-null values, its kind follows its dtype. An all-null object column becomes other.
How values are counted
- Missing values are found with pandas
isna(), soNone,NaN,NaT, andpd.NAall count. - Duplicate rows are counted across the profiled columns, keeping the first copy of each row. Cells that hold lists or dicts are compared by value. An empty DataFrame, or one with no columns, has
0duplicates. - Distinct values exclude nulls. Unhashable values, such as lists and dicts, are compared by value.
- Proportions at the dataset and column level, and
zeros_proportion, are0.0when the denominator is zero.top_proportionandtrue_proportionareNonewhen there are no non-null values.
Caching
Caching lets bynat remember results between runs instead of repeating slow lookups. Cleaning methods use it automatically. You can also use CacheManager directly.
bynat.caching provides:
CacheManagershared_cache_managerCACHE_DIR_ENV_VARDEFAULT_CACHE_DIR
Methods that translate, geocode, extract entities, or look up exchange rates use one shared CacheManager per cache directory. That is the BYNAT_CACHE_DIR directory or the default, unless df.bynat.with_cache(...) selects another directory or an explicit CacheManager.
Where the cache is stored
CacheManager stores each function's cache as rows in a SQLite database.
- The database file is named
cache.sqlite3and lives undercache_dir. - If
cache_diris omitted (here or inBaseData,JsonFile, andCsvFile), bynat uses theBYNAT_CACHE_DIRenvironment variable when set. Otherwise it usesSettings/Cache Data/Automated. - Relative cache paths are resolved from the current working directory.
~and environment variables are expanded. - The directory and database are created lazily. Constructing a manager creates neither. The SQLite backend is created on first cache access or on the first save of in-memory changes.
Using CacheManager directly
from pathlib import Path
from bynat.caching import CacheManager
cache = CacheManager(Path(".cache") / "bynat")
translations = cache.get_column_cache("translate_categorical_column", "status")
translations["pending"] = "Pending"
cache.save_function_cache("translate_categorical_column")
# Load the same function cache later.
loaded = cache.load_function_cache("translate_categorical_column")
assert loaded["status"]["pending"] == "Pending"
Cache rules
- Cache keys must be non-empty strings and cannot contain path separators.
- Values are stored as JSON inside SQLite rows, so use JSON-compatible values: strings, numbers,
None, lists, and dictionaries.
Saving and failures
save_function_cache writes added, changed, and removed entries in one SQLite transaction. If serialization, locking, or the commit fails:
- The failure is logged, not raised.
- Rows that were already committed stay intact.
- The pending in-memory changes are kept, and the next save retries them.
Migrating legacy JSON caches
Older versions stored one <func_name>_cache.json file per function. If such a file exists, it is read only to migrate it into SQLite, at most once:
- Existing SQLite values take precedence over stale JSON values.
- Malformed or non-object files are renamed with a
.corruptsuffix so you can recover them by hand. - Unsupported nested structures are left in place, unmarked, for manual recovery.
CacheManager reference
| Method | Parameters | What it does |
|---|---|---|
load_function_cache(func_name) |
func_name: str |
Returns the function's in-memory cache, loaded from SQLite. Creates the backend on first access and migrates any legacy JSON once. Returns an empty dictionary if the function has no entries. |
get_column_cache(func_name, col_name) |
func_name: str, col_name: str |
Returns the mutable nested dictionary under col_name, creating it if needed. |
save_function_cache(func_name) |
func_name: str |
Writes added, changed, and removed entries to SQLite in one transaction. Logs failures without raising and keeps pending changes for retry. |
delete_column_cache(func_name, col_name) |
func_name: str, col_name: str |
Deletes a nested bucket and saves. It removes only rows the manager already knew about, so concurrent additions and unrelated buckets or functions are preserved. Logs an INFO message whether the bucket was absent or deleted. |
_validate_cache_key and _get_file_path are implementation helpers and not part of the public API.
Advanced features
Hierarchical statistics
Hierarchical statistics give you a typical value for each group at several levels of detail at once, for example per country and job title, then per country. bynat computes one statistic per group at every level of the hierarchy, from the full hierarchy down to its first column.
Pass one strategy or a list: median, mean, and mode (mod is accepted as an alias).
import bynat
import pandas as pd
salaries = pd.DataFrame({
"country": ["EG", "EG", "EG", "US", "US"],
"title": ["Analyst", "Analyst", "Engineer", "Analyst", "Analyst"],
"salary": [10.0, 14.0, 30.0, 70.0, 90.0],
})
stats = salaries.bynat.get_hierarchical_stats(
"salary",
strategy=["median", "mode"],
hierarchy_cols=["country", "title"],
)
stats is returned as the caller's class (here pandas.DataFrame) with these columns:
hierarchy_levelhierarchy_columnsgroup_keytarget_columnstrategystatistic
The median rows are:
| hierarchy_level | group_key | strategy | statistic |
|---|---|---|---|
| 2 | ("EG", "Analyst") |
median | 12.0 |
| 2 | ("EG", "Engineer") |
median | 30.0 |
| 2 | ("US", "Analyst") |
median | 80.0 |
| 1 | ("EG",) |
median | 14.0 |
| 1 | ("US",) |
median | 80.0 |
Details:
group_keyis always a tuple with one value per hierarchy column, including single-column levels such as("EG",).- Null group keys are kept.
meanandmedianneed numeric targets and raise pandas'TypeErrorotherwise.modeworks for any hashable values and breaks ties by first occurrence.- Target columns cannot also be hierarchy columns.
Choosing the output type
Use output_type to choose the result container:
as_frame = salaries.bynat.get_hierarchical_stats("salary", hierarchy_cols="country", output_type=pd.DataFrame)
as_dict = salaries.bynat.get_hierarchical_stats("salary", hierarchy_cols="country", output_type=dict)
# as_dict["group_key"] == [("EG",), ("US",)]; as_dict["statistic"] == [14.0, 80.0]
output_type accepts:
None(default): the caller's classpandas.DataFramedict: column name to list of values- A
BaseDatasubclass
Enrichment with external services
Translation, geocoding, and currency conversion call external services, so use an appropriate cache directory and any service credentials or configuration that apply.
import bynat
import pandas as pd
products = pd.DataFrame({"category": ["حذاء", "قميص", "حذاء"]})
translated = products.bynat.with_cache(".cache/bynat").translate_categorical_column(
"category",
target_lang="en",
save_every=100,
)
Providers
These services are accessed through small provider protocols in bynat.cleaning.enrich. bynat uses a default provider unless a method receives provider=.
| Protocol | Method a provider defines | Default provider | Accepted by |
|---|---|---|---|
Translator |
translate(text, target_lang) -> str | None, optionally translate_batch(texts, target_lang) -> list |
GoogleTranslatorProvider (deep-translator) |
translate_categorical_column, translate_conditional_column |
Geocoder |
geocode(query) -> (latitude, longitude) | None |
NominatimGeocoder (geopy, one request per second) |
fill_missing_coordinates |
RateSource |
yearly_rates(currency, start_year, end_year) -> {year: rate} |
YFinanceRateSource (yfinance) |
convert_to_usd |
A provider is any object with the right method. No subclassing or monkeypatching is needed:
import bynat
import pandas as pd
class UpperTranslator:
def translate(self, text, target_lang):
return text.upper()
df = pd.DataFrame({"status": ["pending", "done", "pending"]})
df = df.bynat.translate_categorical_column("status", provider=UpperTranslator())
How translation providers are called
- If the translator has
translate_batch, it receives values in chunks ofsave_every. - Otherwise
translateruns in a thread pool. Its size comes from the provider's optionalmax_workersattribute (default 1). Request starts are spaced by its optionalmin_intervalattribute, in seconds (default 0). GoogleTranslatorProvideruses four workers and at most five requests per second.- If a translation is falsy or raises an exception, that value stays untranslated and is not cached.
How currency rates are fetched
convert_to_usd makes one yearly_rates call per currency. The call covers every year the look-back might need.
The shared lookup engine
The four cached enrichment methods and extract_entities_nlp all run through one engine:
- Unique non-null values are looked up in the cache.
- Cache misses are fetched.
- The cache is checkpointed every
save_everyvalues. - Failures are logged and left uncached, so the next run retries them.
Cleaning method reference
The accessor and the supported classes expose the same cleaning methods.
- Accessor methods take no
inplaceargument. - Class methods accept
inplacewhere in-place operation is meaningful. get_hierarchical_statsandprofilereturn statistics or reports and have no in-place mode.
Always use the method's return value. Most methods skip missing source or target columns with a logged warning and return an unchanged copy. Invalid arguments that would otherwise give silently wrong results raise instead: an unknown
strategy, an emptyfile_path, invalidsave_to_sqlconnection settings, or an unusableextract_entities_nlpconfiguration.
Methods are grouped by the kind of job they do:
- Columns
- Text and language
- Enrichment
- Outliers, missing values, and statistics
- Data profiling
- Saving data
- Cache and object helpers
Columns
Rename, drop, reorder, and reshape columns.
| Method | Parameters | Purpose |
|---|---|---|
drop_by_substring |
substring |
Drops every column whose name contains substring. |
drop_CLASS_columns |
none | Drops columns containing the literal substring __CLASS__. |
rename_by_substring |
old_substring, new_substring |
Replaces occurrences of one substring in column names. |
rename_all_DOTS |
unified_separator="_" |
Replaces dots in all matching column names. |
reorder_columns |
*first_cols |
Moves the named columns to the front and keeps the order of the others. Pass column names as positional arguments. |
move_column |
col_to_move, col_target, position="after" |
Moves one column before or after another. position must be "before" or "after". |
align_lat_lng |
none | Moves the exact, case-sensitive longitude column right after latitude. |
melt_transform_pivot |
id_vars, value_vars=None, transform_func=None, aggfunc="first", var_name="variable_name", value_name="variable_value", pivot_index=None, pivot_columns=None, **kwargs |
Melts selected columns, optionally transforms the melted values, and pivots back. transform_func must return the same number of values it receives. |
split_list_objects |
source_col, remove_source_col=False, explode_rows=False, cartesian_explode=False, **target_cols_and_indices |
Extracts list elements by integer index, joins selected values, or explodes extracted lists in parallel or cartesian mode. Exploding rows changes the row count and resets the index. |
Text and language
| Method | Parameters | Purpose |
|---|---|---|
extract_keywords |
*source_cols, remove_extracted=False, use_regex=False, **target_cols_and_keywords |
Searches the combined source text and writes matched keywords or regex results to named target columns. |
truncate_after_substring |
regex=True, **target_cols_and_substrings |
Splits named columns at one or more substring or regex delimiters and keeps the text before the first delimiter. |
categorize_by_reference |
*source_cols, keyword-only target_col, reference, default_category="Other" |
Searches the combined text with an Aho-Corasick automaton and assigns the category of the longest matching reference keyword. |
impute_by_language |
reference_col, *target_cols, target_lang="en" |
Picks existing values by exact normalized match or language detection, then falls back to title-cased reference values. Uses deterministic langdetect seeding. |
Notes on extract_keywords:
- Keyword lists are passed as keyword arguments, one per target column.
- Literal matches are case-insensitive and title-cased.
- Each unique text is matched once, with a single combined pattern. For ASCII text, Aho-Corasick is used when
pyahocorasickis installed.
Enrichment
Translation, geocoding, currency conversion, and NLP. See Enrichment with external services for providers and the shared lookup engine.
| Method | Parameters | Purpose |
|---|---|---|
translate_categorical_column |
target_col, target_lang="en", save_every=1000, provider=None |
Translates each unique non-null category once, caches the results, and maps them back. |
translate_conditional_column |
partition_col, detect_col, target_col, target_lang="en", sample_size=10, number_of_samples=3, threshold=0.5, save_every=1000, random_state=0, provider=None |
Samples each partition for language detection and translates the target values of partitions below the threshold. |
fill_missing_coordinates |
*fallback_cols, keyword-only lat_col, long_col, save_every=1000, user_agent="data_cleaning_core_geocoder", retry_not_found=False, provider=None |
Builds location queries from the fallback columns and geocodes missing coordinates with geopy and Nominatim. |
convert_to_usd |
*target_cols, keyword-only country_col, year_col, new_cols=True, round_decimals=None, provider=None |
Infers each country's currency with pycountry, fetches yearly mean exchange rates, and divides the target values by the rate. |
extract_entities_nlp |
*source_cols, keyword-only lexicon_source, spacy_model="en_core_web_sm", exclude_pos=None, disable=("parser", "ner"), save_every=1000, noisy_terms=None, **target_cols_and_json_keys |
Loads lexicon data, processes text with spaCy, and writes entity lists to the target columns. |
translate_conditional_column
- Uses the translation provider and cached results.
- Partitions are processed with a single
groupby. - Sampling is reproducible for a given
random_state. PassNonefor unseeded sampling. random_stateaccepts an integer seed or a NumPyGeneratororRandomState. One generator is drawn from for every sample, so repeated samples differ, while repeated calls with the same seed give the same results.
fill_missing_coordinates
- A failed query is retried with its leftmost part dropped:
"street, city, country"→"city, country"→"country". - Results and not-found queries are cached. Pass
retry_not_found=Trueto query the misses again. - Set
user_agentto identify your application, as Nominatim's usage policy requires.
convert_to_usd
- Rates come from the rate provider (Yahoo Finance by default), with one download per currency.
- If a year has no data, bynat looks back to earlier years.
- Lookups are cached. Unknown countries and past years without data are cached as misses. Network errors are not cached.
extract_entities_nlp
- Entity lists are cached per target, model, and pipeline configuration.
disableacceptsNone, one component name, or an iterable. Components required for lemmatization are rejected, and unknown names produce a warning.- The misspelled
noisey_termsname remains as a separate, deprecated compatibility alias fornoisy_terms.
Outliers, missing values, and statistics
| Method | Parameters | Purpose |
|---|---|---|
handle_outliers |
*target_cols, keyword-only hierarchy_cols, min_samples=5, get_hierarchical_stats=False |
Replaces hierarchical-IQR outliers with NaN, relaxing the grouping hierarchy step by step. |
drop_outliers |
*target_cols, keyword-only hierarchy_cols, min_samples=5, get_hierarchical_stats=False |
Drops rows that contain hierarchical-IQR outliers. Rows are dropped by position, so duplicated index labels are safe. |
fill_missing |
*target_cols, strategy="median", hierarchy_cols=None, min_samples=1, fallback_global=False, get_hierarchical_stats=False |
Fills missing values step by step through a grouping hierarchy, using median, mean, mode, or the alias mod. |
fill_missing_from |
dataset, *target_cols, match_cols=None, targets=None, strategy="median", hierarchy_cols=None, min_samples=1, fallback_global=False, get_hierarchical_stats=False |
Imputes missing values using statistics calculated exclusively from a reference DataFrame. |
get_hierarchical_stats |
*target_cols, strategy="median", hierarchy_cols=None, output_type=None |
Returns one statistic row per group, hierarchy level, target, and strategy. See Hierarchical statistics for the output schema and output_type values. |
Hierarchical missing value imputation
min_samples=1: a group only supplies an imputation statistic if it has at least this many non-null observations; otherwise missing rows fall through to the next (coarser) hierarchy level.fallback_global=False: whenTrue, any missing values that remain after the hierarchy is exhausted are filled using the column-wide statistic (same strategy).- Calling
fill_missingwithhierarchy_cols=Noneandfallback_global=Trueperforms plain column-wide imputation. - Unfilled values remain
NaNby default.
Reference-based missing value imputation (fill_missing_from)
In machine learning and analytical pipelines involving Train/Test splits, computing imputation statistics from the evaluation or test dataset causes data leakage. fill_missing_from calculates statistics exclusively from a separate reference DataFrame (dataset, such as training data) and applies them to the target DataFrame (test_df):
- Statistical leakage prevention: Target DataFrame values are never read when calculating group or column-wide statistics. Outliers, missingness, or distributions in the test dataset have zero influence on the learned statistics.
- Reference immutability: The reference
datasetis never mutated or altered. - Hierarchical fallbacks: If a peer group is absent or has fewer than
min_samplesobservations in the reference dataset, rows progressively fall through to coarser hierarchy levels, and finally tofallback_global. - Column name mapping (
match_cols): When reference and target DataFrames use different schemas, pass a dictionary mapping{target_col: reference_col}. This mapping applies seamlessly to both target columns and hierarchical grouping columns.
import pandas as pd
# Training data (reference dataset)
train_df = pd.DataFrame({
"country": ["EG", "EG", "US", "US"],
"title": ["dev", "dev", "mgr", "mgr"],
"salary": [10.0, 14.0, 70.0, 90.0],
})
# Testing data (target to impute)
test_df = pd.DataFrame({
"country": ["EG", "US", "DE"],
"title": ["dev", "mgr", "dev"],
"salary": [None, None, None],
})
# 1. Impute test data using training statistics (zero test-set leakage)
imputed_test = test_df.bynat.fill_missing_from(
train_df,
"salary",
strategy="median",
hierarchy_cols=["country", "title"],
fallback_global=True,
)
# EG/dev -> 12.0 (from train EG/dev)
# US/mgr -> 80.0 (from train US/mgr)
# DE/dev -> 42.0 (DE not in train -> falls through hierarchy to global train median)
# 2. Schema mapping with match_cols ({target_col: reference_col})
train_schema = pd.DataFrame({
"nation": ["EG", "US"],
"wage": [12.0, 80.0],
})
test_schema = pd.DataFrame({
"country": ["EG", "US"],
"salary": [None, None],
})
imputed = test_schema.bynat.fill_missing_from(
train_schema,
"salary",
match_cols={"salary": "wage", "country": "nation"},
hierarchy_cols=["country"],
)
Returning statistics from cleaning methods
For handle_outliers, drop_outliers, fill_missing, and fill_missing_from, passing get_hierarchical_stats=True reuses get_hierarchical_stats(). The statistics are computed from the original data, before the operation changes any values or rows.
- The return value is
(result, stats). - With
inplace=True, the operation changes the original object and returnsstatsdirectly. - The outlier methods use the default hierarchical
medianstatistic.fill_missinguses its requestedstrategy.
Data profiling
| Method | Parameters | Purpose |
|---|---|---|
profile |
*columns, keyword-only output_type=None |
Returns a read-only data quality report for all columns or the ones you name. See Data profiling for the report structure and output_type values. |
Saving data
| Method | Parameters | Purpose |
|---|---|---|
save_to_json |
file_path, orient="records", append=False, force_ascii=False, indent=4 |
Writes JSON only when the content changed, or appends to and deduplicates existing JSON rows. |
save_to_sql |
table_name, database_type, database_user, database_password, database_host, database_port, database_name, database_driver=None, columns_types=None, chunk_size=None, fast_executemany=False, if_exists="append", index=False |
Builds a SQLAlchemy connection URL and writes the frame with DataFrame.to_sql. |
Notes:
save_to_jsondeduplicates rows with list or dictionary values by their JSON content. Datetime columns are formatted as%Y-%m-%d %H:%M:%S.save_to_sqlusesdatabase_nameas a file path for SQLite. SQL Server requiresdatabase_driver.
Cache and object helpers
| Method | Parameters | Purpose |
|---|---|---|
with_cache (accessor only) |
cache |
Returns an accessor that caches in another directory or CacheManager. |
clear_cache_for_column |
func_name, target_col |
Deletes one cache bucket through the attached CacheManager. |
view (BaseData only) |
none | Returns a plain pandas.DataFrame view or copy of the object. |
Module layout
Use this table to find where things live if you want to read the source or import something specific.
| Module | Purpose | Contents |
|---|---|---|
bynat.caching.manager |
Remembering results | CacheManager and shared_cache_manager |
bynat.cleaning.columns |
Tidying and reshaping columns | Column renaming, dropping, reordering, moving, melt_transform_pivot, split_list_objects |
bynat.cleaning.text |
Working with text | extract_keywords, truncate_after_substring, categorize_by_reference |
bynat.cleaning.stats |
Outliers, gaps, and group statistics | handle_outliers, drop_outliers, fill_missing, fill_missing_from, get_hierarchical_stats |
bynat.cleaning.profile |
Data quality profiling | profile, ProfileReport, DatasetProfile, ColumnProfile |
bynat.cleaning.enrich |
Adding information from outside the table | translate, geocode, currency, nlp, the shared cached-lookup engine, and providers |
bynat.cleaning.io |
Reading and writing | save_to_json, save_to_sql, read_json, read_csv |
bynat.cleaning.accessor |
The df.bynat entry point |
The df.bynat accessor |
bynat.cleaning.base |
The class-based API | BaseData, JsonFile, CsvFile |
The legacy module paths bynat.caching.Caching and bynat.cleaning.Cleaning still import. They resolve to bynat.caching.manager and bynat.cleaning.base.
Limitations and operational notes
Read these before you rely on bynat in a pipeline.
Optional integrations
Optional integrations are lazy-loaded, so basic bynat.cleaning imports and core DataFrame methods work with only numpy and pandas. A method that needs an absent integration raises a clear ImportError when called.
External services
- Translation uses
deep-translatorby default and may make external requests. - Geocoding uses Nominatim through
geopy, with a one-second rate limit. - Currency conversion uses Yahoo Finance history through
yfinance.
These services can fail, rate-limit requests, or change their results. Cached values persist locally in SQLite.
NLP extraction
extract_entities_nlpneeds a compatible spaCy model installed separately.disable=...acceptsNone, a component name, or an iterable. It defaults to disablingparserandner, and rejects components required for lemmatization.- If a requested component is not in the loaded pipeline, bynat warns and continues.
- A missing or unloadable model raises spaCy's
OSError.
SQL
save_to_sqllets database exceptions propagate to the caller and always disposes its engine.- It receives database credentials as ordinary arguments. Do not hard-code secrets in application code.
Cache behavior
- Cache entries live in a single SQLite database, not one JSON file per function.
- Saves run inside SQLite transactions and WAL checkpoints. A failed or interrupted save is logged and rolled back without raising. Rows from earlier successful saves stay intact, and pending in-memory changes are retried on the next save.
- Concurrent managers sharing one database merge disjoint key updates. If more than one manager writes the same key, the last committed write wins.
- Values must be JSON-serializable.
Logging
Messages go through the standard logging module under the bynat logger:
- Skipped columns and failed lookups are
WARNINGmessages and appear on stderr by default. - Progress and checkpoints are
INFOmessages.
import logging
logging.basicConfig(level=logging.INFO) # show progress
logging.getLogger("bynat").setLevel(logging.ERROR) # or hide warnings
Development and verification
The test suite uses pytest. It avoids network access, secrets, user files, and external services. Network-backed integrations (translation, geocoding, exchange rates, spaCy) are tested with fake modules.
python -m pip install -e ".[dev]"
python -m pytest -q
python -m compileall -q bynat
ruff check bynat tests benchmarks
python -m mypy
To also run the tests that use the real pyahocorasick and SQLAlchemy/SQLite, install ".[categorization,sql]". Those tests are skipped otherwise.
CI runs the suite on Ubuntu and Windows with Python 3.11 and 3.13, plus Ruff, mypy, and an extras job.
Type checking
Type checking is gradual. python -m mypy reads its scope from [tool.mypy] in pyproject.toml. That scope covers the cache manager and the cleaning helper modules (_common, accessor, columns, text, stats, io, profile, enrich), checked against pandas-stubs. The supported classes in bynat/cleaning/base.py remain a separate gradual-typing boundary.
To bring a module into scope:
- Add it to
filesin[tool.mypy]. - Fix what mypy reports with precise annotations, not
Anyor# type: ignore. - Keep the runtime behavior unchanged.
Benchmarks
benchmarks/ holds timing scripts that compare the current implementations with the 0.3.0 algorithms. See benchmarks/README.md.
Building packages
Building a wheel or source archive requires the configured setuptools build backend and wheel tooling.
License
MIT. See LICENSE.
Metadata
Release files for bynat 0.6.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bynat-0.6.6.tar.gz | 179.2 kB | With provenance | Details for bynat-0.6.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bynat-0.6.6-py3-none-any.whl | Python 3 | none | any | Details for bynat-0.6.6-py3-none-any.whl |
Total release size: 291.6 kB
Release files / bynat-0.6.6.tar.gz
| Download URL | bynat-0.6.6.tar.gz |
|---|---|
| Size | 179.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
feb725f9c6298d94608fc2df41006d59b7b6a65f1368fe5f2690c27fac9ea989
|
|
BLAKE2b-256 checksum How to use checksums |
a712ed560adc89dfa00ba73fbe9507307fd2acd68dd6955f05da503746d41cd9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logRelease files / bynat-0.6.6-py3-none-any.whl
| Download URL | bynat-0.6.6-py3-none-any.whl |
|---|---|
| Size | 112.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f893f145469375f064c5ff58f3a62ce1c0367952312b5529d50815ee02558cb8
|
|
BLAKE2b-256 checksum How to use checksums |
af30e027af76e2a4a73d88cf4a1ece4d2987a3951a96758bff2f5506e66d7291
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logSecurity for bynat 0.6.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bynat-0.6.6.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.