complexifier 0.2.0__py3-none-any.whl → 0.2.2__py3-none-any.whl
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/__init__.py +1 -1
- complexifier/main.py +81 -6
- complexifier/test_main.py +70 -0
- {complexifier-0.2.0.dist-info → complexifier-0.2.2.dist-info}/METADATA +1 -1
- complexifier-0.2.2.dist-info/RECORD +7 -0
- complexifier-0.2.0.dist-info/RECORD +0 -6
- {complexifier-0.2.0.dist-info → complexifier-0.2.2.dist-info}/WHEEL +0 -0
- {complexifier-0.2.0.dist-info → complexifier-0.2.2.dist-info}/licenses/LICENSE +0 -0
complexifier/__init__.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
from main import *
|
|
1
|
+
from .main import *
|
complexifier/main.py
CHANGED
|
@@ -3,8 +3,16 @@ import random
|
|
|
3
3
|
from typo import StrErrer
|
|
4
4
|
import pandas as pd
|
|
5
5
|
|
|
6
|
+
|
|
6
7
|
def create_spag_error(word: str) -> str:
|
|
7
|
-
"""Gives a 10% chance to introduce a
|
|
8
|
+
"""Gives a 10% chance to introduce a spelling error to a word.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
word (str): The original word to potentially alter.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
str: The original word or a word with a random spelling error.
|
|
15
|
+
"""
|
|
8
16
|
if len(word) < 3:
|
|
9
17
|
return word
|
|
10
18
|
error_object = StrErrer(word)
|
|
@@ -35,14 +43,24 @@ def create_spag_error(word: str) -> str:
|
|
|
35
43
|
case _:
|
|
36
44
|
return word
|
|
37
45
|
|
|
46
|
+
|
|
38
47
|
def introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
48
|
+
"""Applies random spelling errors to specified columns in a DataFrame.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
df (pd.DataFrame): The DataFrame to alter.
|
|
52
|
+
columns (list or str, optional): Column names to apply errors to. If not provided, defaults to all string columns.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
pd.DataFrame: The DataFrame with potential spelling errors introduced.
|
|
56
|
+
"""
|
|
39
57
|
if not columns:
|
|
40
58
|
columns = df.select_dtypes(include=["string", "object"]).columns
|
|
41
59
|
elif isinstance(columns, str):
|
|
42
60
|
columns = [columns]
|
|
43
|
-
|
|
44
61
|
elif not isinstance(columns, list):
|
|
45
62
|
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
63
|
+
|
|
46
64
|
for col in columns:
|
|
47
65
|
if col not in df.columns:
|
|
48
66
|
raise ValueError(f"{col} not in DataFrame")
|
|
@@ -51,10 +69,29 @@ def introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
|
51
69
|
df[col] = df[col].apply(create_spag_error)
|
|
52
70
|
return df
|
|
53
71
|
|
|
72
|
+
|
|
54
73
|
def add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
55
|
-
"""Adds or subtracts a random integer in
|
|
74
|
+
"""Adds or subtracts a random integer in columns of between 1% and 10% of the rows.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
df (pd.DataFrame): The DataFrame to modify.
|
|
78
|
+
columns (list or str, optional): Column names to adjust. Defaults to numeric columns if not specified.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
pd.DataFrame: The DataFrame with outliers added.
|
|
82
|
+
"""
|
|
56
83
|
if not columns:
|
|
57
84
|
columns = df.select_dtypes(include="number").columns
|
|
85
|
+
elif isinstance(columns, str):
|
|
86
|
+
columns = [columns]
|
|
87
|
+
elif not isinstance(columns, list):
|
|
88
|
+
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
89
|
+
|
|
90
|
+
elif isinstance(columns, str):
|
|
91
|
+
columns = [columns]
|
|
92
|
+
elif not isinstance(columns, list):
|
|
93
|
+
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
94
|
+
|
|
58
95
|
for col in columns:
|
|
59
96
|
if col not in df.columns:
|
|
60
97
|
raise ValueError(f"{col} is not a column.")
|
|
@@ -71,9 +108,23 @@ def add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
|
71
108
|
def add_standard_deviations(
|
|
72
109
|
df: pd.DataFrame, columns=None, min_std=1, max_std=5
|
|
73
110
|
) -> pd.DataFrame:
|
|
74
|
-
"""
|
|
111
|
+
"""Adds random deviations to entries in specified numeric columns to simulate data anomalies.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
df (pd.DataFrame): The DataFrame to manipulate.
|
|
115
|
+
columns (list or str, optional): Column names to modify. Defaults to numeric columns if not specified.
|
|
116
|
+
min_std (int): Minimum number of standard deviations to add.
|
|
117
|
+
max_std (int): Maximum number of standard deviations to add.
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
pd.DataFrame: The DataFrame with deviations added.
|
|
121
|
+
"""
|
|
75
122
|
if not columns:
|
|
76
123
|
columns = df.select_dtypes(include="number").columns
|
|
124
|
+
elif isinstance(columns, str):
|
|
125
|
+
columns = [columns]
|
|
126
|
+
elif not isinstance(columns, list):
|
|
127
|
+
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
77
128
|
|
|
78
129
|
for col in columns:
|
|
79
130
|
if col not in df.columns:
|
|
@@ -91,7 +142,15 @@ def add_standard_deviations(
|
|
|
91
142
|
|
|
92
143
|
|
|
93
144
|
def duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame:
|
|
94
|
-
"""Adds duplicate rows to a
|
|
145
|
+
"""Adds duplicate rows to a DataFrame.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
df (pd.DataFrame): DataFrame to which duplicates will be added.
|
|
149
|
+
sample_size (int, optional): Number of rows to duplicate. Randomly selected if not specified.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
pd.DataFrame: The DataFrame with duplicate rows added.
|
|
153
|
+
"""
|
|
95
154
|
if not sample_size:
|
|
96
155
|
sample_size = random.randint(len(df[col]) // 100, len(df[col]) // 10)
|
|
97
156
|
new_rows = df.sample(sample_size)
|
|
@@ -101,12 +160,28 @@ def duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame:
|
|
|
101
160
|
def add_nulls(
|
|
102
161
|
df: pd.DataFrame, columns=None, min_percent=1, max_percent=10
|
|
103
162
|
) -> pd.DataFrame:
|
|
163
|
+
"""
|
|
164
|
+
Inserts null values into specified DataFrame columns.
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
df (pd.DataFrame): The DataFrame to modify.
|
|
168
|
+
columns (list or str, optional): Specific columns to add nulls to. Defaults to all columns if not specified.
|
|
169
|
+
min_percent (int): Minimum percentage of null values to insert.
|
|
170
|
+
max_percent (int): Maximum percentage of null values to insert.
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
pd.DataFrame: The DataFrame with null values inserted.
|
|
174
|
+
"""
|
|
104
175
|
if not columns:
|
|
105
176
|
columns = df.columns
|
|
177
|
+
elif isinstance(columns, str):
|
|
178
|
+
columns = [columns]
|
|
179
|
+
elif not isinstance(columns, list):
|
|
180
|
+
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
181
|
+
|
|
106
182
|
for col in columns:
|
|
107
183
|
indices_to_none = df.sample(
|
|
108
184
|
len(df) * random.randint(min_percent, max_percent) // 100
|
|
109
185
|
).index
|
|
110
186
|
df.loc[indices_to_none, col] = None
|
|
111
187
|
return df
|
|
112
|
-
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from .main import (create_spag_error,
|
|
4
|
+
introduce_spag_error,
|
|
5
|
+
add_or_subtract_outliers,
|
|
6
|
+
add_standard_deviations,
|
|
7
|
+
duplicate_rows,
|
|
8
|
+
add_nulls)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@pytest.fixture
|
|
12
|
+
def sample_df():
|
|
13
|
+
"""
|
|
14
|
+
A fixture for tests
|
|
15
|
+
"""
|
|
16
|
+
return pd.DataFrame({
|
|
17
|
+
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
|
|
18
|
+
'Age': [28, 34, 29, 42, 25],
|
|
19
|
+
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
|
|
20
|
+
'Salary': [70000, 80000, 72000, 95000, 67000]
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_create_spag_error():
|
|
25
|
+
"""
|
|
26
|
+
Test that for a word with sufficient length, the function must return a string of same length
|
|
27
|
+
"""
|
|
28
|
+
word = "hello"
|
|
29
|
+
error_word = create_spag_error(word)
|
|
30
|
+
assert isinstance(error_word, str)
|
|
31
|
+
assert len(error_word) == len(word)
|
|
32
|
+
|
|
33
|
+
def test_introduce_spag_error(sample_df):
|
|
34
|
+
"""
|
|
35
|
+
Test that DataFrame columns contain the potential spelling errors
|
|
36
|
+
"""
|
|
37
|
+
df_with_errors = introduce_spag_error(sample_df, columns='Name')
|
|
38
|
+
assert 'Name' in df_with_errors.columns
|
|
39
|
+
assert all([isinstance(name, str) for name in df_with_errors['Name'].values])
|
|
40
|
+
|
|
41
|
+
def test_add_or_subtract_outliers(sample_df):
|
|
42
|
+
"""
|
|
43
|
+
Test that outliers are added to the numerical columns
|
|
44
|
+
"""
|
|
45
|
+
df_with_outliers = add_or_subtract_outliers(sample_df, columns='Salary')
|
|
46
|
+
assert 'Salary' in df_with_outliers.columns
|
|
47
|
+
assert df_with_outliers['Salary'].dtype == float or df_with_outliers['Salary'].dtype == int
|
|
48
|
+
|
|
49
|
+
def test_add_standard_deviations(sample_df):
|
|
50
|
+
"""
|
|
51
|
+
Test that standard deviations are added to the Age column
|
|
52
|
+
"""
|
|
53
|
+
df_with_deviations = add_standard_deviations(sample_df, columns='Age')
|
|
54
|
+
assert 'Age' in df_with_deviations.columns
|
|
55
|
+
assert df_with_deviations['Age'].dtype == float or df_with_deviations['Age'].dtype == int
|
|
56
|
+
|
|
57
|
+
def test_duplicate_rows(sample_df):
|
|
58
|
+
"""
|
|
59
|
+
Test that duplicate rows are added
|
|
60
|
+
"""
|
|
61
|
+
df_with_duplicates = duplicate_rows(sample_df, sample_size=2)
|
|
62
|
+
assert len(df_with_duplicates) == len(sample_df) + 2
|
|
63
|
+
|
|
64
|
+
def test_add_nulls(sample_df):
|
|
65
|
+
"""
|
|
66
|
+
Test that nulls are added to the DataFrame
|
|
67
|
+
"""
|
|
68
|
+
df_with_nulls = add_nulls(sample_df, columns='City', min_percent=20, max_percent=30)
|
|
69
|
+
assert 'City' in df_with_nulls.columns
|
|
70
|
+
assert df_with_nulls['City'].isnull().sum() > 0
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
complexifier/__init__.py,sha256=zDr350exO14Ii8Pj3TXwPuRxIDrEjrg43CpjYABDqh4,19
|
|
2
|
+
complexifier/main.py,sha256=bi8XYFpfh05TgYDefBFz8Y5vFl0-Wx-dcuZBOAEJdyY,6551
|
|
3
|
+
complexifier/test_main.py,sha256=nDg03HGhi_QMox_GAkonkakgOcXDBsuro6AR5mbpyWw,2391
|
|
4
|
+
complexifier-0.2.2.dist-info/METADATA,sha256=yrdaeptIdP7SAOERGhVtgta9xLHIydiOg9Ucys3KeN4,2986
|
|
5
|
+
complexifier-0.2.2.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
6
|
+
complexifier-0.2.2.dist-info/licenses/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
|
|
7
|
+
complexifier-0.2.2.dist-info/RECORD,,
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
complexifier/__init__.py,sha256=5BbKQVb39kq2EZbTiPeOsMrtqdLGHeasWhCxn064NtU,18
|
|
2
|
-
complexifier/main.py,sha256=-6AOdr4QMfFGtZH09W6dvE4iSpGZwKmSKcUMk4IKzKM,3836
|
|
3
|
-
complexifier-0.2.0.dist-info/METADATA,sha256=WZ9l17YMlfWdYwoMAzNbgkILf-hXAeryT4nXInFvY6M,2986
|
|
4
|
-
complexifier-0.2.0.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
5
|
-
complexifier-0.2.0.dist-info/licenses/LICENSE,sha256=2bm9uFabQZ3Ykb_SaSU_uUbAj2-htc6WJQmS_65qD00,1073
|
|
6
|
-
complexifier-0.2.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|