DataFrame Parser
The DataFrame parser allows you to parse structured data from CSV, TSV, Excel, and other tabular formats into TypedLogic theories. It uses pandas for data loading and automatically infers predicate names from filenames.
Bases: Parser
A parser for tabular data files using pandas.
This parser reads CSV, TSV, Excel and other tabular formats and converts rows to logical facts (ground terms).
Source code in src/typedlogic/parsers/dataframe_parser.py
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 128 129 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
__init__(**pandas_kwargs)
Initialize the parser.
Args: **pandas_kwargs: Additional keyword arguments passed to pandas read functions
Source code in src/typedlogic/parsers/dataframe_parser.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
parse(source, **kwargs)
Parse tabular data into a Theory containing only ground terms.
Since this parser only handles facts (not rules), it returns a Theory with empty sentences but populated ground_terms.
Args: source: Path to file, file content as string, or file-like object **kwargs: Additional arguments passed to pandas
Returns: Theory object with ground terms from the tabular data
Source code in src/typedlogic/parsers/dataframe_parser.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
parse_ground_terms(source, **kwargs)
Parse tabular data and return a list of ground terms (facts).
Args: source: Path to file, file content as string, or file-like object **kwargs: Additional arguments passed to pandas
Returns: List of Term objects representing the rows as facts
Source code in src/typedlogic/parsers/dataframe_parser.py
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 | |
_extract_predicate_name(path)
Extract predicate name from file path.
Takes the stem (filename without extension) and uses it as predicate name. Handles compound names like "Link.01" -> "Link"
Args: path: Path object
Returns: Predicate name extracted from filename
Source code in src/typedlogic/parsers/dataframe_parser.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
_read_dataframe(source, **kwargs)
Read data into a pandas DataFrame, auto-detecting format.
Args: source: Data source **kwargs: Additional pandas arguments
Returns: pandas DataFrame with the loaded data
Source code in src/typedlogic/parsers/dataframe_parser.py
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 | |
_read_from_path(path, **kwargs)
Read DataFrame from a file path, auto-detecting format based on extension.
Args: path: Path to the data file **kwargs: Additional pandas arguments
Returns: pandas DataFrame with the loaded data
Source code in src/typedlogic/parsers/dataframe_parser.py
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 | |
validate_iter(source, **kwargs)
Validate the tabular data file.
Checks that the file can be read by pandas and contains valid tabular data.
Args: source: Data source to validate **kwargs: Additional arguments
Yields: ValidationMessage objects for any validation issues
Source code in src/typedlogic/parsers/dataframe_parser.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
Usage Examples
Basic CSV Parsing
from typedlogic.parsers.dataframe_parser import DataFrameParser
parser = DataFrameParser()
# Parse CSV file - predicate name inferred from filename
theory = parser.parse("people.csv") # Creates predicates like people(name, age, city)
# Parse with explicit predicate name
terms = parser.parse_ground_terms("data.csv", predicate="person")
Supported Formats
The DataFrame parser automatically detects and handles:
- CSV files (
.csv) - TSV files (
.tsv,.tab) - Excel files (
.xlsx,.xls)
Predicate Name Inference
The parser automatically infers predicate names from filenames:
people.csv→people(...)predicatesLink.csv→Link(...)predicatesdata.tsv→data(...)predicates
Integration with CLI
The DataFrame parser is automatically used when processing CSV/TSV files via the CLI:
# Convert CSV to other formats
typedlogic dump people.csv -t yaml
# Combine multiple CSV files
typedlogic dump people.csv companies.csv -t prolog
# Use in catalog files
typedlogic dump my_dataset.catalog.yaml
Configuration
The parser accepts standard pandas parameters for customization:
# Custom separator and headers
terms = parser.parse_ground_terms("data.txt", sep="|", header=0)
# Skip rows and handle missing values
terms = parser.parse_ground_terms("messy_data.csv", skiprows=2, na_values=["N/A"])