Implementation:Sdv dev SDV Metadata Detect From Dataframes
| Knowledge Sources | |
|---|---|
| Domains | Data_Science, Schema_Inference |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Concrete tool for automatically detecting metadata schemas from pandas DataFrames, provided by the SDV library.
Description
The Metadata.detect_from_dataframes class method analyzes a dictionary of DataFrames and produces a Metadata object with inferred column sdtypes, primary keys, and optionally foreign key relationships. It iterates over each table, detects column types using heuristic rules, and then optionally runs relationship detection across tables using column name matching.
A companion method Metadata.detect_from_dataframe handles single-table detection.
Usage
Import the Metadata class and call detect_from_dataframes when you have one or more DataFrames and need to create a metadata schema automatically. Use detect_from_dataframe for single-table workflows.
Code Reference
Source Location
- Repository: SDV
- File: sdv/metadata/metadata.py
- Lines: L103-143 (detect_from_dataframes), L146-180 (detect_from_dataframe)
Signature
@classmethod
def detect_from_dataframes(
cls,
data,
infer_sdtypes=True,
infer_keys='primary_and_foreign',
foreign_key_inference_algorithm='column_name_match',
):
"""Detect the metadata for all tables in a dictionary of dataframes.
Args:
data (dict):
Dictionary of table names to dataframes.
infer_sdtypes (bool):
Whether to infer the sdtypes of each column. Defaults to True.
infer_keys (str):
Whether to infer primary and/or foreign keys. Options:
'primary_and_foreign', 'primary_only', None. Defaults to 'primary_and_foreign'.
foreign_key_inference_algorithm (str):
Algorithm for detecting foreign keys. Currently only 'column_name_match'.
Returns:
Metadata: A new metadata object with detected sdtypes.
"""
Import
from sdv.metadata import Metadata
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data | dict[str, pd.DataFrame] | Yes | Dictionary mapping table names to DataFrames |
| infer_sdtypes | bool | No | Auto-detect column types (default: True) |
| infer_keys | str or None | No | Key inference mode: 'primary_and_foreign', 'primary_only', or None |
| foreign_key_inference_algorithm | str | No | FK detection algorithm (default: 'column_name_match') |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | Metadata | Metadata instance with tables, columns, sdtypes, keys, and relationships |
Usage Examples
Multi Table Detection
from sdv.metadata import Metadata
import pandas as pd
# Prepare data as dict of DataFrames
data = {
'users': pd.DataFrame({
'user_id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35]
}),
'orders': pd.DataFrame({
'order_id': [101, 102, 103],
'user_id': [1, 2, 1],
'amount': [50.0, 75.0, 100.0]
})
}
# Auto-detect metadata including relationships
metadata = Metadata.detect_from_dataframes(data)
print(metadata)
Single Table Detection
from sdv.metadata import Metadata
import pandas as pd
df = pd.DataFrame({
'id': [1, 2, 3],
'category': ['A', 'B', 'A'],
'value': [10.5, 20.3, 30.1]
})
metadata = Metadata.detect_from_dataframe(df)
print(metadata)