pycraftbox-dataframe 4.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycraftbox_dataframe-4.0.0/.gitignore +131 -0
- pycraftbox_dataframe-4.0.0/PKG-INFO +10 -0
- pycraftbox_dataframe-4.0.0/pyproject.toml +24 -0
- pycraftbox_dataframe-4.0.0/src/pycraftbox/dataframe/__init__.py +16 -0
- pycraftbox_dataframe-4.0.0/src/pycraftbox/dataframe/cleaners.py +143 -0
- pycraftbox_dataframe-4.0.0/src/pycraftbox/dataframe/converters.py +503 -0
- pycraftbox_dataframe-4.0.0/src/pycraftbox/dataframe/helpers.py +30 -0
- pycraftbox_dataframe-4.0.0/src/pycraftbox/dataframe/mapper.py +929 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
.vscode/*
|
|
6
|
+
!.vscode/extensions.json
|
|
7
|
+
!.vscode/tasks.json
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
pip-wheel-metadata/
|
|
26
|
+
share/python-wheels/
|
|
27
|
+
*.egg-info/
|
|
28
|
+
.installed.cfg
|
|
29
|
+
*.egg
|
|
30
|
+
MANIFEST
|
|
31
|
+
|
|
32
|
+
# PyInstaller
|
|
33
|
+
# Usually these files are written by a python script from a template
|
|
34
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
35
|
+
*.manifest
|
|
36
|
+
*.spec
|
|
37
|
+
|
|
38
|
+
# Installer logs
|
|
39
|
+
pip-log.txt
|
|
40
|
+
pip-delete-this-directory.txt
|
|
41
|
+
|
|
42
|
+
# Unit test / coverage reports
|
|
43
|
+
htmlcov/
|
|
44
|
+
.tox/
|
|
45
|
+
.nox/
|
|
46
|
+
.coverage
|
|
47
|
+
.coverage.*
|
|
48
|
+
.cache
|
|
49
|
+
nosetests.xml
|
|
50
|
+
coverage.xml
|
|
51
|
+
*.cover
|
|
52
|
+
*.py,cover
|
|
53
|
+
.hypothesis/
|
|
54
|
+
.pytest_cache/
|
|
55
|
+
|
|
56
|
+
# Translations
|
|
57
|
+
*.mo
|
|
58
|
+
*.pot
|
|
59
|
+
|
|
60
|
+
# Django stuff:
|
|
61
|
+
*.log
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
.python-version
|
|
88
|
+
|
|
89
|
+
# pipenv
|
|
90
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
91
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
92
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
93
|
+
# install all needed dependencies.
|
|
94
|
+
#Pipfile.lock
|
|
95
|
+
|
|
96
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
97
|
+
__pypackages__/
|
|
98
|
+
|
|
99
|
+
# Celery stuff
|
|
100
|
+
celerybeat-schedule
|
|
101
|
+
celerybeat.pid
|
|
102
|
+
|
|
103
|
+
# SageMath parsed files
|
|
104
|
+
*.sage.py
|
|
105
|
+
|
|
106
|
+
# Environments
|
|
107
|
+
.env
|
|
108
|
+
.venv
|
|
109
|
+
env/
|
|
110
|
+
venv/
|
|
111
|
+
ENV/
|
|
112
|
+
env.bak/
|
|
113
|
+
venv.bak/
|
|
114
|
+
|
|
115
|
+
# Spyder project settings
|
|
116
|
+
.spyderproject
|
|
117
|
+
.spyproject
|
|
118
|
+
|
|
119
|
+
# Rope project settings
|
|
120
|
+
.ropeproject
|
|
121
|
+
|
|
122
|
+
# mkdocs documentation
|
|
123
|
+
/site
|
|
124
|
+
|
|
125
|
+
# mypy
|
|
126
|
+
.mypy_cache/
|
|
127
|
+
.dmypy.json
|
|
128
|
+
dmypy.json
|
|
129
|
+
|
|
130
|
+
# Pyre type checker
|
|
131
|
+
.pyre/
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pycraftbox-dataframe
|
|
3
|
+
Version: 4.0.0
|
|
4
|
+
Summary: Pandas, CSV and tabular conversion utilities for pycraftbox
|
|
5
|
+
Project-URL: Homepage, https://gitlab.com/erch/pycraftbox
|
|
6
|
+
Project-URL: Repository, https://gitlab.com/erch/pycraftbox
|
|
7
|
+
Project-URL: Issues, https://gitlab.com/erch/pycraftbox/-/issues
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Requires-Dist: numpy>=2.2.1
|
|
10
|
+
Requires-Dist: pandas>=3.0.3
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pycraftbox-dataframe"
|
|
3
|
+
version = "4.0.0"
|
|
4
|
+
description = "Pandas, CSV and tabular conversion utilities for pycraftbox"
|
|
5
|
+
requires-python = ">=3.12"
|
|
6
|
+
dependencies = [
|
|
7
|
+
"numpy>=2.2.1",
|
|
8
|
+
"pandas>=3.0.3",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
[project.urls]
|
|
12
|
+
Homepage = "https://gitlab.com/erch/pycraftbox"
|
|
13
|
+
Repository = "https://gitlab.com/erch/pycraftbox"
|
|
14
|
+
Issues = "https://gitlab.com/erch/pycraftbox/-/issues"
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["hatchling"]
|
|
18
|
+
build-backend = "hatchling.build"
|
|
19
|
+
|
|
20
|
+
[tool.hatch.build]
|
|
21
|
+
dev-mode-dirs = ["src"]
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.wheel]
|
|
24
|
+
packages = ["src/pycraftbox"]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Pandas and CSV utilities."""
|
|
2
|
+
|
|
3
|
+
from .cleaners import Cleaners
|
|
4
|
+
from .converters import Converters
|
|
5
|
+
from .helpers import PandasHelpers
|
|
6
|
+
from .mapper import ColDefinition, ColMapping, CompiledMapping, DFFileMapper
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"Cleaners",
|
|
10
|
+
"ColDefinition",
|
|
11
|
+
"ColMapping",
|
|
12
|
+
"CompiledMapping",
|
|
13
|
+
"Converters",
|
|
14
|
+
"DFFileMapper",
|
|
15
|
+
"PandasHelpers",
|
|
16
|
+
]
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""Utilities for cleaning and repairing malformed tabular data.
|
|
2
|
+
|
|
3
|
+
This module hosts the :class:`Cleaners` class, which provides helpers used
|
|
4
|
+
while reading CSV files, such as repairing lines with unescaped quotes.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import logging
|
|
10
|
+
import uuid
|
|
11
|
+
from typing import Callable
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Cleaners:
|
|
15
|
+
"""Utility class providing methods for cleaning and repairing malformed data."""
|
|
16
|
+
|
|
17
|
+
_logger: logging.Logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
@classmethod
|
|
20
|
+
def pick_separator(cls, fields: list[str]) -> str:
|
|
21
|
+
"""Pick a rare separator character not present in any of the fields.
|
|
22
|
+
|
|
23
|
+
This method tries a list of rare control characters first, then falls
|
|
24
|
+
back to generating a unique token if none of the control characters
|
|
25
|
+
are suitable.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
fields: List of field strings to check for separator conflicts.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
A separator string that doesn't appear in any field.
|
|
32
|
+
|
|
33
|
+
Example::
|
|
34
|
+
|
|
35
|
+
fields = ["Hello, world", "Test data"]
|
|
36
|
+
sep = Cleaners.pick_separator(fields)
|
|
37
|
+
"""
|
|
38
|
+
candidates = ["\x1f", "\x1e", "\x1d", "\x1c", "\x00"] # rare control chars
|
|
39
|
+
for c in candidates:
|
|
40
|
+
if all(c not in f for f in fields):
|
|
41
|
+
return c
|
|
42
|
+
|
|
43
|
+
# fallback: token long et unique
|
|
44
|
+
while True:
|
|
45
|
+
token = f"__SEP_{uuid.uuid4().hex}__"
|
|
46
|
+
if all(token not in f for f in fields):
|
|
47
|
+
return token
|
|
48
|
+
|
|
49
|
+
@classmethod
|
|
50
|
+
def repair_bad_line(
|
|
51
|
+
cls, expected_cols: int
|
|
52
|
+
) -> Callable[[list[str]], list[str] | None]:
|
|
53
|
+
"""Return a callback for pandas on_bad_lines that repairs malformed CSV lines.
|
|
54
|
+
|
|
55
|
+
This method creates a callback function that can be passed to pandas
|
|
56
|
+
``read_csv()`` via the ``on_bad_lines`` parameter. The callback attempts
|
|
57
|
+
to repair lines with unescaped quotes inside quoted fields by properly
|
|
58
|
+
escaping them.
|
|
59
|
+
|
|
60
|
+
Args:
|
|
61
|
+
expected_cols: The expected number of columns in the CSV file.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
A callable that accepts a list of field strings and returns either
|
|
65
|
+
a repaired list of the expected length, or ``None`` if the line
|
|
66
|
+
cannot be repaired.
|
|
67
|
+
|
|
68
|
+
Example::
|
|
69
|
+
|
|
70
|
+
# Use with pandas read_csv
|
|
71
|
+
repair_fn = Cleaners.repair_bad_line(expected_cols=12)
|
|
72
|
+
df = pd.read_csv(
|
|
73
|
+
"data.csv",
|
|
74
|
+
engine="python",
|
|
75
|
+
on_bad_lines=repair_fn,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
Note:
|
|
79
|
+
This callback is designed for use with the Python engine only
|
|
80
|
+
(``engine='python'``). The C engine does not support custom
|
|
81
|
+
``on_bad_lines`` callbacks.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def _repair(fields: list[str]) -> list[str] | None:
|
|
85
|
+
"""Internal callback that repairs a malformed line.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
fields: List of field strings from the malformed line.
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
A list of exactly expected_cols fields if repair succeeds,
|
|
92
|
+
None otherwise.
|
|
93
|
+
"""
|
|
94
|
+
sep = cls.pick_separator(fields)
|
|
95
|
+
quote = '"'
|
|
96
|
+
out = []
|
|
97
|
+
in_quoted_segment = False
|
|
98
|
+
i = 0
|
|
99
|
+
line = sep.join(fields)
|
|
100
|
+
n = len(line)
|
|
101
|
+
|
|
102
|
+
while i < n:
|
|
103
|
+
ch = line[i]
|
|
104
|
+
if ch != quote:
|
|
105
|
+
out.append(ch)
|
|
106
|
+
i += 1
|
|
107
|
+
continue
|
|
108
|
+
|
|
109
|
+
# if not in quoted segment we are on an opening quote, we just add it and move on
|
|
110
|
+
if not in_quoted_segment:
|
|
111
|
+
in_quoted_segment = True
|
|
112
|
+
out.append(ch)
|
|
113
|
+
i += 1
|
|
114
|
+
continue
|
|
115
|
+
|
|
116
|
+
# we are on a quote and inside a quoted segment, next char can tell us if it's a closing quote,
|
|
117
|
+
# an escaped quote OR a quote that need to be escaped.
|
|
118
|
+
nxt = line[i + 1] if i + 1 < n else ""
|
|
119
|
+
|
|
120
|
+
# Test if it's an escaped quote, if yes we add it and move on
|
|
121
|
+
if nxt == quote:
|
|
122
|
+
out.append(quote)
|
|
123
|
+
out.append(quote)
|
|
124
|
+
i += 2
|
|
125
|
+
continue
|
|
126
|
+
|
|
127
|
+
# Test if it's a closing quote, if yes we add it and move on
|
|
128
|
+
if nxt in (sep, "\n", "\r", ""):
|
|
129
|
+
in_quoted_segment = False
|
|
130
|
+
out.append(ch)
|
|
131
|
+
i += 1
|
|
132
|
+
continue
|
|
133
|
+
|
|
134
|
+
# This is an unquoted quote inside a quoted segment, we need to escape it by doubling it
|
|
135
|
+
out.append(quote)
|
|
136
|
+
out.append(quote)
|
|
137
|
+
i += 1
|
|
138
|
+
|
|
139
|
+
res = "".join(out)
|
|
140
|
+
splits = res.split(sep)
|
|
141
|
+
return splits if len(splits) == expected_cols else None
|
|
142
|
+
|
|
143
|
+
return _repair
|