complexifier 1.0.0__tar.gz → 1.3.5__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.
- complexifier-1.3.5/.gitignore +238 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/PKG-INFO +15 -3
- {complexifier-1.0.0 → complexifier-1.3.5}/README.md +13 -1
- {complexifier-1.0.0 → complexifier-1.3.5}/README.rst +10 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/docs/source/index.rst +10 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/pyproject.toml +1 -1
- {complexifier-1.0.0 → complexifier-1.3.5}/src/complexifier/__init__.py +2 -0
- complexifier-1.3.5/src/complexifier/conftest.py +4 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/src/complexifier/main.py +136 -2
- complexifier-1.3.5/src/complexifier/test_add_sequential_anomaly.py +309 -0
- complexifier-1.3.5/src/complexifier/test_main.py +556 -0
- complexifier-1.3.5/src/complexifier/test_swap_dates.py +233 -0
- complexifier-1.0.0/.gitignore +0 -10
- complexifier-1.0.0/src/complexifier/test_main.py +0 -283
- {complexifier-1.0.0 → complexifier-1.3.5}/.github/workflows/main.yml +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/.github/workflows/sphinx.yml +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/.readthedocs.yaml +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/LICENSE +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/docs/.nojekyll +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/docs/Makefile +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/docs/make.bat +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/docs/source/assets/complexifier.jpeg +0 -0
- {complexifier-1.0.0 → complexifier-1.3.5}/docs/source/conf.py +0 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
.venv
|
|
2
|
+
venv
|
|
3
|
+
dist
|
|
4
|
+
*.txt
|
|
5
|
+
*.sh
|
|
6
|
+
__pycache__
|
|
7
|
+
testing.py
|
|
8
|
+
.coverage
|
|
9
|
+
.pytest_cache
|
|
10
|
+
docs/build
|
|
11
|
+
|
|
12
|
+
# Byte-compiled / optimized / DLL files
|
|
13
|
+
__pycache__/
|
|
14
|
+
*.py[codz]
|
|
15
|
+
*$py.class
|
|
16
|
+
|
|
17
|
+
# C extensions
|
|
18
|
+
*.so
|
|
19
|
+
|
|
20
|
+
# Distribution / packaging
|
|
21
|
+
.Python
|
|
22
|
+
build/
|
|
23
|
+
develop-eggs/
|
|
24
|
+
dist/
|
|
25
|
+
downloads/
|
|
26
|
+
eggs/
|
|
27
|
+
.eggs/
|
|
28
|
+
lib/
|
|
29
|
+
lib64/
|
|
30
|
+
parts/
|
|
31
|
+
sdist/
|
|
32
|
+
var/
|
|
33
|
+
wheels/
|
|
34
|
+
share/python-wheels/
|
|
35
|
+
*.egg-info/
|
|
36
|
+
.installed.cfg
|
|
37
|
+
*.egg
|
|
38
|
+
MANIFEST
|
|
39
|
+
|
|
40
|
+
# PyInstaller
|
|
41
|
+
# Usually these files are written by a python script from a template
|
|
42
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
43
|
+
*.manifest
|
|
44
|
+
*.spec
|
|
45
|
+
|
|
46
|
+
# Installer logs
|
|
47
|
+
pip-log.txt
|
|
48
|
+
pip-delete-this-directory.txt
|
|
49
|
+
|
|
50
|
+
# Unit test / coverage reports
|
|
51
|
+
htmlcov/
|
|
52
|
+
.tox/
|
|
53
|
+
.nox/
|
|
54
|
+
.coverage
|
|
55
|
+
.coverage.*
|
|
56
|
+
.cache
|
|
57
|
+
nosetests.xml
|
|
58
|
+
coverage.xml
|
|
59
|
+
*.cover
|
|
60
|
+
*.py.cover
|
|
61
|
+
*.lcov
|
|
62
|
+
.hypothesis/
|
|
63
|
+
.pytest_cache/
|
|
64
|
+
cover/
|
|
65
|
+
|
|
66
|
+
# Translations
|
|
67
|
+
*.mo
|
|
68
|
+
*.pot
|
|
69
|
+
|
|
70
|
+
# Django stuff:
|
|
71
|
+
*.log
|
|
72
|
+
local_settings.py
|
|
73
|
+
db.sqlite3
|
|
74
|
+
db.sqlite3-journal
|
|
75
|
+
db.sqlite3-wal
|
|
76
|
+
db.sqlite3-shm
|
|
77
|
+
|
|
78
|
+
# Flask stuff:
|
|
79
|
+
instance/
|
|
80
|
+
.webassets-cache
|
|
81
|
+
|
|
82
|
+
# Scrapy stuff:
|
|
83
|
+
.scrapy
|
|
84
|
+
|
|
85
|
+
# Sphinx documentation
|
|
86
|
+
docs/_build/
|
|
87
|
+
|
|
88
|
+
# PyBuilder
|
|
89
|
+
.pybuilder/
|
|
90
|
+
target/
|
|
91
|
+
|
|
92
|
+
# Jupyter Notebook
|
|
93
|
+
.ipynb_checkpoints
|
|
94
|
+
|
|
95
|
+
# IPython
|
|
96
|
+
profile_default/
|
|
97
|
+
ipython_config.py
|
|
98
|
+
|
|
99
|
+
# pyenv
|
|
100
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
101
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
102
|
+
# .python-version
|
|
103
|
+
|
|
104
|
+
# pipenv
|
|
105
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
106
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
107
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
108
|
+
# install all needed dependencies.
|
|
109
|
+
# Pipfile.lock
|
|
110
|
+
|
|
111
|
+
# uv
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
113
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
114
|
+
# commonly ignored for libraries.
|
|
115
|
+
# uv.lock
|
|
116
|
+
|
|
117
|
+
# poetry
|
|
118
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
119
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
120
|
+
# commonly ignored for libraries.
|
|
121
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
122
|
+
# poetry.lock
|
|
123
|
+
# poetry.toml
|
|
124
|
+
|
|
125
|
+
# pdm
|
|
126
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
127
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
128
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
129
|
+
# pdm.lock
|
|
130
|
+
# pdm.toml
|
|
131
|
+
.pdm-python
|
|
132
|
+
.pdm-build/
|
|
133
|
+
|
|
134
|
+
# pixi
|
|
135
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
136
|
+
# pixi.lock
|
|
137
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
138
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
139
|
+
.pixi/*
|
|
140
|
+
!.pixi/config.toml
|
|
141
|
+
|
|
142
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
143
|
+
__pypackages__/
|
|
144
|
+
|
|
145
|
+
# Celery stuff
|
|
146
|
+
celerybeat-schedule*
|
|
147
|
+
celerybeat.pid
|
|
148
|
+
|
|
149
|
+
# Redis
|
|
150
|
+
*.rdb
|
|
151
|
+
*.aof
|
|
152
|
+
*.pid
|
|
153
|
+
|
|
154
|
+
# RabbitMQ
|
|
155
|
+
mnesia/
|
|
156
|
+
rabbitmq/
|
|
157
|
+
rabbitmq-data/
|
|
158
|
+
|
|
159
|
+
# ActiveMQ
|
|
160
|
+
activemq-data/
|
|
161
|
+
|
|
162
|
+
# SageMath parsed files
|
|
163
|
+
*.sage.py
|
|
164
|
+
|
|
165
|
+
# Environments
|
|
166
|
+
.env
|
|
167
|
+
.envrc
|
|
168
|
+
.venv
|
|
169
|
+
env/
|
|
170
|
+
venv/
|
|
171
|
+
ENV/
|
|
172
|
+
env.bak/
|
|
173
|
+
venv.bak/
|
|
174
|
+
|
|
175
|
+
# Conda environments
|
|
176
|
+
conda-meta/
|
|
177
|
+
|
|
178
|
+
# Spyder project settings
|
|
179
|
+
.spyderproject
|
|
180
|
+
.spyproject
|
|
181
|
+
|
|
182
|
+
# Rope project settings
|
|
183
|
+
.ropeproject
|
|
184
|
+
|
|
185
|
+
# mkdocs/Zensical documentation
|
|
186
|
+
/site
|
|
187
|
+
|
|
188
|
+
# mypy
|
|
189
|
+
.mypy_cache/
|
|
190
|
+
.dmypy.json
|
|
191
|
+
dmypy.json
|
|
192
|
+
|
|
193
|
+
# Pyre type checker
|
|
194
|
+
.pyre/
|
|
195
|
+
|
|
196
|
+
# pytype static type analyzer
|
|
197
|
+
.pytype/
|
|
198
|
+
|
|
199
|
+
# Cython debug symbols
|
|
200
|
+
cython_debug/
|
|
201
|
+
|
|
202
|
+
# PyCharm
|
|
203
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
204
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
205
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
206
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
207
|
+
# .idea/
|
|
208
|
+
|
|
209
|
+
# Abstra
|
|
210
|
+
# Abstra is an AI-powered process automation framework.
|
|
211
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
212
|
+
# Learn more at https://abstra.io/docs
|
|
213
|
+
.abstra/
|
|
214
|
+
|
|
215
|
+
# Visual Studio Code
|
|
216
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
|
|
217
|
+
# can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
218
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer, you
|
|
219
|
+
# could uncomment the following to ignore the entire vscode folder
|
|
220
|
+
# .vscode/
|
|
221
|
+
# Temporary file for partial code execution
|
|
222
|
+
tempCodeRunnerFile.py
|
|
223
|
+
|
|
224
|
+
# Ruff stuff:
|
|
225
|
+
.ruff_cache/
|
|
226
|
+
|
|
227
|
+
# PyPI configuration file
|
|
228
|
+
.pypirc
|
|
229
|
+
|
|
230
|
+
# Marimo
|
|
231
|
+
marimo/_static/
|
|
232
|
+
marimo/_lsp/
|
|
233
|
+
__marimo__/
|
|
234
|
+
|
|
235
|
+
# Streamlit
|
|
236
|
+
.streamlit/secrets.toml
|
|
237
|
+
|
|
238
|
+
**/.DS_Store
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: complexifier
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.5
|
|
4
4
|
Summary: A library to make data worse
|
|
5
5
|
Project-URL: Homepage, https://github.com/ruyzambrano/complexifier
|
|
6
6
|
Project-URL: Issues, https://github.com/ruyzambrano/complexifier/issues
|
|
@@ -88,8 +88,20 @@ Introduces duplicate rows into a DataFrame. This function is useful for testing
|
|
|
88
88
|
|
|
89
89
|
Inserts null values into specified DataFrame columns. This simulates missing data conditions.
|
|
90
90
|
|
|
91
|
+
### swap_dates
|
|
92
|
+
|
|
93
|
+
`swap_dates(df: pd.DataFrame, columns=None, sample_size: int = None) -> pd.DataFrame`
|
|
94
|
+
|
|
95
|
+
Introduces date errors by swapping month and day positions in datetime columns. Randomly selects rows and swaps the month and day for those where the day value is <= 12 (e.g., 2024-01-03 becomes 2024-03-01).
|
|
96
|
+
|
|
97
|
+
### add_sequential_anomaly
|
|
98
|
+
|
|
99
|
+
`add_sequential_anomaly(df: pd.DataFrame, sequence_size: int = 10, magnitude: int = 3, columns=None, direction: str = 'positive') -> pd.DataFrame`
|
|
100
|
+
|
|
101
|
+
Introduces a sequential anomaly into the DataFrame by uniformly adjusting a series of consecutive data points in numeric columns by the column's standard deviation multiplied by the magnitude.
|
|
102
|
+
|
|
91
103
|
### mess_it_up
|
|
92
104
|
|
|
93
|
-
`mess_it_up(df: pd.DataFrame, columns=None, min_std=1, max_std=5, sample_size=None,min_percent=1, max_percent=10, introduce_spag=True, add_outliers=True, add_std=True, duplicate=True, add_null=True) -> pd.DataFrame`
|
|
105
|
+
`mess_it_up(df: pd.DataFrame, columns=None, min_std=1, max_std=5, sample_size=None, min_percent=1, max_percent=10, introduce_spag=True, add_outliers=True, add_std=True, duplicate=True, add_null=True, swap_date=True, sequential_anomaly=True, sequence_size=10, magnitude=3, direction='positive') -> pd.DataFrame`
|
|
94
106
|
|
|
95
107
|
Adds all (or some) of the above methods. Really messes it up.
|
|
@@ -74,8 +74,20 @@ Introduces duplicate rows into a DataFrame. This function is useful for testing
|
|
|
74
74
|
|
|
75
75
|
Inserts null values into specified DataFrame columns. This simulates missing data conditions.
|
|
76
76
|
|
|
77
|
+
### swap_dates
|
|
78
|
+
|
|
79
|
+
`swap_dates(df: pd.DataFrame, columns=None, sample_size: int = None) -> pd.DataFrame`
|
|
80
|
+
|
|
81
|
+
Introduces date errors by swapping month and day positions in datetime columns. Randomly selects rows and swaps the month and day for those where the day value is <= 12 (e.g., 2024-01-03 becomes 2024-03-01).
|
|
82
|
+
|
|
83
|
+
### add_sequential_anomaly
|
|
84
|
+
|
|
85
|
+
`add_sequential_anomaly(df: pd.DataFrame, sequence_size: int = 10, magnitude: int = 3, columns=None, direction: str = 'positive') -> pd.DataFrame`
|
|
86
|
+
|
|
87
|
+
Introduces a sequential anomaly into the DataFrame by uniformly adjusting a series of consecutive data points in numeric columns by the column's standard deviation multiplied by the magnitude.
|
|
88
|
+
|
|
77
89
|
### mess_it_up
|
|
78
90
|
|
|
79
|
-
`mess_it_up(df: pd.DataFrame, columns=None, min_std=1, max_std=5, sample_size=None,min_percent=1, max_percent=10, introduce_spag=True, add_outliers=True, add_std=True, duplicate=True, add_null=True) -> pd.DataFrame`
|
|
91
|
+
`mess_it_up(df: pd.DataFrame, columns=None, min_std=1, max_std=5, sample_size=None, min_percent=1, max_percent=10, introduce_spag=True, add_outliers=True, add_std=True, duplicate=True, add_null=True, swap_date=True, sequential_anomaly=True, sequence_size=10, magnitude=3, direction='positive') -> pd.DataFrame`
|
|
80
92
|
|
|
81
93
|
Adds all (or some) of the above methods. Really messes it up.
|
|
@@ -75,6 +75,16 @@ add_nulls
|
|
|
75
75
|
|
|
76
76
|
.. autofunction:: complexifier.add_nulls
|
|
77
77
|
|
|
78
|
+
swap_dates
|
|
79
|
+
~~~~~~~~~~
|
|
80
|
+
|
|
81
|
+
.. autofunction:: complexifier.swap_dates
|
|
82
|
+
|
|
83
|
+
add_sequential_anomaly
|
|
84
|
+
~~~~~~~~~~~~~~~~~~~~~~
|
|
85
|
+
|
|
86
|
+
.. autofunction:: complexifier.add_sequential_anomaly
|
|
87
|
+
|
|
78
88
|
mess_it_up
|
|
79
89
|
~~~~~~~~~~
|
|
80
90
|
|
|
@@ -75,6 +75,16 @@ add_nulls
|
|
|
75
75
|
|
|
76
76
|
.. autofunction:: complexifier.add_nulls
|
|
77
77
|
|
|
78
|
+
swap_dates
|
|
79
|
+
~~~~~~~~~~
|
|
80
|
+
|
|
81
|
+
.. autofunction:: complexifier.swap_dates
|
|
82
|
+
|
|
83
|
+
add_sequential_anomaly
|
|
84
|
+
~~~~~~~~~~~~~~~~~~~~~~
|
|
85
|
+
|
|
86
|
+
.. autofunction:: complexifier.add_sequential_anomaly
|
|
87
|
+
|
|
78
88
|
mess_it_up
|
|
79
89
|
~~~~~~~~~~
|
|
80
90
|
|
|
@@ -139,14 +139,18 @@ def add_standard_deviations(
|
|
|
139
139
|
for col in columns:
|
|
140
140
|
if col not in df.columns:
|
|
141
141
|
raise ValueError(f"{col} is not a column.")
|
|
142
|
+
|
|
143
|
+
# Ensure the column can hold float values before modifying it
|
|
144
|
+
df[col] = df[col].astype(float)
|
|
145
|
+
|
|
142
146
|
sample_size = random.randint(len(df[col]) // 100, len(df[col]) // 10)
|
|
143
147
|
standard_deviation = df[col].std()
|
|
144
148
|
random_indices = df.sample(sample_size).index
|
|
145
149
|
df.loc[random_indices, col] = df.loc[random_indices, col].apply(
|
|
146
150
|
lambda row: row
|
|
147
151
|
+ random.randint(min_std, max_std)
|
|
148
|
-
|
|
149
|
-
|
|
152
|
+
- standard_deviation
|
|
153
|
+
- random.choice([1, -1])
|
|
150
154
|
)
|
|
151
155
|
return df
|
|
152
156
|
|
|
@@ -212,6 +216,115 @@ def add_nulls(
|
|
|
212
216
|
return df
|
|
213
217
|
|
|
214
218
|
|
|
219
|
+
def swap_dates(df: pd.DataFrame, columns=None, sample_size: int = None) -> pd.DataFrame:
|
|
220
|
+
"""
|
|
221
|
+
Introduces date errors by swapping month and day positions in datetime columns.
|
|
222
|
+
|
|
223
|
+
Randomly selects sample_size rows, then swaps the month and day for those
|
|
224
|
+
where the day value is <= 12 (e.g., 2024-01-03 becomes 2024-03-01).
|
|
225
|
+
|
|
226
|
+
:param df: The DataFrame to modify.
|
|
227
|
+
:type df: pd.DataFrame
|
|
228
|
+
:param columns: Column names to apply date swaps to. Defaults to all datetime columns.
|
|
229
|
+
:type columns: list or str, optional
|
|
230
|
+
:param sample_size: Number of rows to randomly select for potential swapping. Defaults to a random number between 1% and 10% of rows.
|
|
231
|
+
:type sample_size: int, optional
|
|
232
|
+
:return: The DataFrame with swapped dates.
|
|
233
|
+
:rtype: pd.DataFrame
|
|
234
|
+
"""
|
|
235
|
+
if not isinstance(df, pd.DataFrame):
|
|
236
|
+
raise TypeError("Must be a pandas")
|
|
237
|
+
if df.empty:
|
|
238
|
+
return df
|
|
239
|
+
if not columns:
|
|
240
|
+
columns = df.select_dtypes(include=["datetime", "datetime64", "datetimetz"]).columns.to_list()
|
|
241
|
+
elif isinstance(columns, str):
|
|
242
|
+
columns = [columns]
|
|
243
|
+
elif not isinstance(columns, list):
|
|
244
|
+
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
245
|
+
|
|
246
|
+
for col in columns:
|
|
247
|
+
if col not in df.columns:
|
|
248
|
+
raise ValueError(f"{col} not in DataFrame")
|
|
249
|
+
if not pd.api.types.is_datetime64_any_dtype(df[col]):
|
|
250
|
+
raise TypeError(f"{col} is {df[col].dtype}, not a datetime type")
|
|
251
|
+
|
|
252
|
+
if sample_size is None:
|
|
253
|
+
sample_size = random.randint(len(df) // 100, len(df) // 10)
|
|
254
|
+
|
|
255
|
+
selected_indices = df.sample(min(sample_size, len(df))).index
|
|
256
|
+
|
|
257
|
+
for col in columns:
|
|
258
|
+
for idx in selected_indices:
|
|
259
|
+
dt = df.loc[idx, col]
|
|
260
|
+
if pd.isna(dt):
|
|
261
|
+
continue
|
|
262
|
+
if dt.day <= 12:
|
|
263
|
+
try:
|
|
264
|
+
df.loc[idx, col] = dt.replace(month=dt.day, day=dt.month)
|
|
265
|
+
except ValueError:
|
|
266
|
+
pass
|
|
267
|
+
return df
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def add_sequential_anomaly(
|
|
271
|
+
df: pd.DataFrame, sequence_size: int = 10, magnitude: int = 3,
|
|
272
|
+
columns=None, direction: str = "positive"
|
|
273
|
+
) -> pd.DataFrame:
|
|
274
|
+
"""
|
|
275
|
+
Introduces a sequential anomaly into the DataFrame by uniformly adjusting
|
|
276
|
+
a series of consecutive data points in numeric columns.
|
|
277
|
+
|
|
278
|
+
The adjustment is calculated as the column's standard deviation multiplied
|
|
279
|
+
by the magnitude, added or subtracted based on the direction parameter.
|
|
280
|
+
|
|
281
|
+
:param df: The DataFrame to modify.
|
|
282
|
+
:type df: pd.DataFrame
|
|
283
|
+
:param sequence_size: Number of consecutive data points to adjust. Capped at 10% of DataFrame length. Defaults to 10.
|
|
284
|
+
:type sequence_size: int, optional
|
|
285
|
+
:param magnitude: Number of standard deviations to adjust by. Defaults to 3.
|
|
286
|
+
:type magnitude: int, optional
|
|
287
|
+
:param columns: Column names to modify. Defaults to all numeric columns if not specified.
|
|
288
|
+
:type columns: list or str, optional
|
|
289
|
+
:param direction: Whether the sequence is adjusted up ('positive') or down ('negative'). Defaults to 'positive'.
|
|
290
|
+
:type direction: str, optional
|
|
291
|
+
:return: The DataFrame with sequential anomaly introduced.
|
|
292
|
+
:rtype: pd.DataFrame
|
|
293
|
+
"""
|
|
294
|
+
if not isinstance(df, pd.DataFrame):
|
|
295
|
+
raise TypeError("Must be a pandas")
|
|
296
|
+
if df.empty:
|
|
297
|
+
return df
|
|
298
|
+
if direction not in ("positive", "negative"):
|
|
299
|
+
raise ValueError("direction must be 'positive' or 'negative'")
|
|
300
|
+
if not columns:
|
|
301
|
+
columns = df.select_dtypes(include="number").columns.to_list()
|
|
302
|
+
elif isinstance(columns, str):
|
|
303
|
+
columns = [columns]
|
|
304
|
+
elif not isinstance(columns, list):
|
|
305
|
+
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
306
|
+
|
|
307
|
+
for col in columns:
|
|
308
|
+
if col not in df.columns:
|
|
309
|
+
raise ValueError(f"{col} is not a column.")
|
|
310
|
+
|
|
311
|
+
max_sequence = max(1, len(df) // 10)
|
|
312
|
+
sequence_size = min(sequence_size, max_sequence)
|
|
313
|
+
|
|
314
|
+
start_idx = random.randint(0, len(df) - sequence_size)
|
|
315
|
+
|
|
316
|
+
for col in columns:
|
|
317
|
+
if pd.api.types.is_integer_dtype(df[col]):
|
|
318
|
+
df[col] = df[col].astype(float)
|
|
319
|
+
std = df[col].std()
|
|
320
|
+
adjustment = std * magnitude
|
|
321
|
+
if direction == "negative":
|
|
322
|
+
adjustment = -adjustment
|
|
323
|
+
df.iloc[start_idx:start_idx + sequence_size, df.columns.get_loc(col)] += adjustment
|
|
324
|
+
|
|
325
|
+
return df
|
|
326
|
+
|
|
327
|
+
|
|
215
328
|
def mess_it_up(
|
|
216
329
|
df: pd.DataFrame,
|
|
217
330
|
columns=None,
|
|
@@ -225,6 +338,11 @@ def mess_it_up(
|
|
|
225
338
|
add_std=True,
|
|
226
339
|
duplicate=True,
|
|
227
340
|
add_null=True,
|
|
341
|
+
swap_date=True,
|
|
342
|
+
sequential_anomaly=True,
|
|
343
|
+
sequence_size=10,
|
|
344
|
+
magnitude=3,
|
|
345
|
+
direction="positive",
|
|
228
346
|
) -> pd.DataFrame:
|
|
229
347
|
"""
|
|
230
348
|
Applies several functions to add outliers, spelling errors and null values
|
|
@@ -253,6 +371,16 @@ def mess_it_up(
|
|
|
253
371
|
:type duplicate: bool, optional
|
|
254
372
|
:param add_null: Adds null values to the dataset. Defaults to True
|
|
255
373
|
:type add_null: bool, optional
|
|
374
|
+
:param swap_date: Swaps month and day in datetime columns. Defaults to True
|
|
375
|
+
:type swap_date: bool, optional
|
|
376
|
+
:param sequential_anomaly: Adds a sequential anomaly to numeric columns. Defaults to True
|
|
377
|
+
:type sequential_anomaly: bool, optional
|
|
378
|
+
:param sequence_size: Number of consecutive data points for sequential anomaly. Defaults to 10.
|
|
379
|
+
:type sequence_size: int, optional
|
|
380
|
+
:param magnitude: Number of standard deviations for sequential anomaly. Defaults to 3.
|
|
381
|
+
:type magnitude: int, optional
|
|
382
|
+
:param direction: Direction of sequential anomaly adjustment ('positive' or 'negative'). Defaults to 'positive'.
|
|
383
|
+
:type direction: str, optional
|
|
256
384
|
:return: The modified DataFrame.
|
|
257
385
|
:rtype: pd.DataFrame
|
|
258
386
|
"""
|
|
@@ -271,6 +399,7 @@ def mess_it_up(
|
|
|
271
399
|
|
|
272
400
|
string_cols = df[columns].select_dtypes(include="string").columns.to_list()
|
|
273
401
|
numeric_cols = df[columns].select_dtypes(include="number").columns.to_list()
|
|
402
|
+
datetime_cols = df[columns].select_dtypes(include=["datetime", "datetime64", "datetimetz"]).columns.to_list()
|
|
274
403
|
if string_cols:
|
|
275
404
|
if introduce_spag:
|
|
276
405
|
df = introduce_spag_error(df, string_cols)
|
|
@@ -279,6 +408,11 @@ def mess_it_up(
|
|
|
279
408
|
df = add_or_subtract_outliers(df, numeric_cols)
|
|
280
409
|
if add_std:
|
|
281
410
|
df = add_standard_deviations(df, numeric_cols, min_std, max_std)
|
|
411
|
+
if sequential_anomaly:
|
|
412
|
+
df = add_sequential_anomaly(df, sequence_size, magnitude, numeric_cols, direction)
|
|
413
|
+
if datetime_cols:
|
|
414
|
+
if swap_date:
|
|
415
|
+
df = swap_dates(df, datetime_cols)
|
|
282
416
|
if duplicate:
|
|
283
417
|
df = duplicate_rows(df, sample_size)
|
|
284
418
|
if add_null:
|