complexifier 0.2.1__tar.gz → 0.2.2__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-0.2.1 → complexifier-0.2.2}/.gitignore +2 -1
- {complexifier-0.2.1 → complexifier-0.2.2}/PKG-INFO +1 -1
- {complexifier-0.2.1 → complexifier-0.2.2}/pyproject.toml +1 -1
- complexifier-0.2.2/src/complexifier/__init__.py +1 -0
- {complexifier-0.2.1 → complexifier-0.2.2}/src/complexifier/main.py +25 -3
- complexifier-0.2.2/src/complexifier/test_main.py +70 -0
- complexifier-0.2.1/src/complexifier/__init__.py +0 -1
- {complexifier-0.2.1 → complexifier-0.2.2}/LICENSE +0 -0
- {complexifier-0.2.1 → complexifier-0.2.2}/README.md +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .main import *
|
|
@@ -3,6 +3,7 @@ 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
8
|
"""Gives a 10% chance to introduce a spelling error to a word.
|
|
8
9
|
|
|
@@ -42,6 +43,7 @@ def create_spag_error(word: str) -> str:
|
|
|
42
43
|
case _:
|
|
43
44
|
return word
|
|
44
45
|
|
|
46
|
+
|
|
45
47
|
def introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
46
48
|
"""Applies random spelling errors to specified columns in a DataFrame.
|
|
47
49
|
|
|
@@ -56,9 +58,9 @@ def introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
|
56
58
|
columns = df.select_dtypes(include=["string", "object"]).columns
|
|
57
59
|
elif isinstance(columns, str):
|
|
58
60
|
columns = [columns]
|
|
59
|
-
|
|
60
61
|
elif not isinstance(columns, list):
|
|
61
62
|
raise TypeError(f"Columns is type {type(columns)} but expected str or list")
|
|
63
|
+
|
|
62
64
|
for col in columns:
|
|
63
65
|
if col not in df.columns:
|
|
64
66
|
raise ValueError(f"{col} not in DataFrame")
|
|
@@ -67,6 +69,7 @@ def introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
|
67
69
|
df[col] = df[col].apply(create_spag_error)
|
|
68
70
|
return df
|
|
69
71
|
|
|
72
|
+
|
|
70
73
|
def add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
71
74
|
"""Adds or subtracts a random integer in columns of between 1% and 10% of the rows.
|
|
72
75
|
|
|
@@ -79,6 +82,16 @@ def add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame:
|
|
|
79
82
|
"""
|
|
80
83
|
if not columns:
|
|
81
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
|
+
|
|
82
95
|
for col in columns:
|
|
83
96
|
if col not in df.columns:
|
|
84
97
|
raise ValueError(f"{col} is not a column.")
|
|
@@ -108,6 +121,10 @@ def add_standard_deviations(
|
|
|
108
121
|
"""
|
|
109
122
|
if not columns:
|
|
110
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")
|
|
111
128
|
|
|
112
129
|
for col in columns:
|
|
113
130
|
if col not in df.columns:
|
|
@@ -143,7 +160,8 @@ def duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame:
|
|
|
143
160
|
def add_nulls(
|
|
144
161
|
df: pd.DataFrame, columns=None, min_percent=1, max_percent=10
|
|
145
162
|
) -> pd.DataFrame:
|
|
146
|
-
|
|
163
|
+
"""
|
|
164
|
+
Inserts null values into specified DataFrame columns.
|
|
147
165
|
|
|
148
166
|
Args:
|
|
149
167
|
df (pd.DataFrame): The DataFrame to modify.
|
|
@@ -156,10 +174,14 @@ def add_nulls(
|
|
|
156
174
|
"""
|
|
157
175
|
if not columns:
|
|
158
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
|
+
|
|
159
182
|
for col in columns:
|
|
160
183
|
indices_to_none = df.sample(
|
|
161
184
|
len(df) * random.randint(min_percent, max_percent) // 100
|
|
162
185
|
).index
|
|
163
186
|
df.loc[indices_to_none, col] = None
|
|
164
187
|
return df
|
|
165
|
-
|
|
@@ -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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from main import *
|
|
File without changes
|
|
File without changes
|