complexifier 0.2.3__tar.gz → 0.3.1__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.
@@ -0,0 +1,105 @@
1
+ Metadata-Version: 2.3
2
+ Name: complexifier
3
+ Version: 0.3.1
4
+ Summary: A library to make data worse
5
+ Project-URL: Homepage, https://github.com/ruyzambrano/complexifier
6
+ Project-URL: Issues, https://github.com/ruyzambrano/complexifier/issues
7
+ Author-email: Ruy Zambrano <ruyzambrano@gmail.com>
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+
14
+ # Complexifier
15
+
16
+ This makes your pandas dataframe even worse
17
+
18
+ ## Dependencies
19
+
20
+ - `pandas`
21
+ - `typo`
22
+ - `random`
23
+
24
+ ## Installation
25
+
26
+ `complexifier` can be installed using `pip`
27
+
28
+ ```sh
29
+ pip install complexifier
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ Once installed you can use `complexifier` to add mistakes and outliers to your data
35
+
36
+ This library has several methods available:
37
+
38
+ ### `create_spag_error(word: str) -> str`
39
+
40
+ **Parameters**:
41
+ - `word` (str): The original word to potentially alter.
42
+
43
+ Introduces a 10% chance of a random spelling error in a given word. This function is useful for simulating typos and spelling mistakes in text data.
44
+
45
+ ### `introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame`
46
+
47
+ Applies the create_spag_error function to each string entry in specified columns of a DataFrame, introducing random spelling errors with a 10% probability.
48
+
49
+ **Parameters**:
50
+ - `df` (pd.DataFrame): The DataFrame to alter.
51
+ - `columns` (list or str): Column names to apply errors to. Defaults to all string columns.
52
+
53
+ ### `add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame`
54
+
55
+ Randomly adds or subtracts values in specified numeric columns at random indices, simulating outliers between 1% and 10% of the rows.
56
+
57
+ **Parameters**:
58
+ - `df` (pd.DataFrame): The DataFrame to modify.
59
+ - `columns` (list or str): Column names to adjust. Defaults to all numeric columns if not specified.
60
+
61
+ ### `add_standard_deviations(df: pd.DataFrame, columns=None, min_std=1, max_std=5) -> pd.DataFrame`
62
+
63
+ Adds between 1 to 5 standard deviations to random entries in specified numeric columns to simulate data anomalies.
64
+
65
+ **Parameters**:
66
+ - `df` (pd.DataFrame): The DataFrame to manipulate.
67
+ - `columns` (list or str): Column names to modify. Defaults to numeric columns if not specified.
68
+ - `min_std` (int): Minimum number of standard deviations to add. Defaults to 1
69
+ - `max_std` (int): Maximum number of standard deviations to add. Defaults to 5
70
+
71
+ ### `duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame`
72
+
73
+ Introduces duplicate rows into a DataFrame. This function is useful for testing deduplication processes.
74
+
75
+ **Parameters**:
76
+ - `df` (pd.DataFrame): DataFrame to which duplicates will be added.
77
+ - `sample_size` (int): number of rows to duplicate. A random percentage between 1% and 10% if not specified.
78
+
79
+ ### `add_nulls(df: pd.DataFrame, columns=None, min_percent=1, max_percent=10) -> pd.DataFrame`
80
+
81
+ Inserts null values into specified DataFrame columns. This simulates missing data conditions.
82
+
83
+ **Parameters**:
84
+ - `df` (pd.DataFrame): The DataFrame to modify.
85
+ - `columns` (list or str, optional): Specific columns to add nulls to. Defaults to all columns if not specified.
86
+ - `min_percent` (int): Minimum percentage of null values to insert. Defaults to 1%
87
+ - `max_percent` (int): Maximum percentage of null values to insert. Defaults to 10%
88
+
89
+ ### `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`
90
+
91
+ Adds all (or some) of the above methods. Really messes it up.
92
+
93
+ **Parameters**:
94
+ - `df` (pd.DataFrame): The DataFrame to modify.
95
+ - `columns` (list or str, optional): Specific columns to add nulls to. Defaults to all columns if not specified.
96
+ - `min_std` (int): Minimum number of standard deviations to add. Defaults to 1
97
+ - `max_std` (int): Maximum number of standard deviations to add. Defaults to 5
98
+ - `sample_size` (int, optional): Number of rows to duplicate. Randomly selected if not specified.
99
+ - `min_percent` (int): Minimum percentage of null values to insert. Defaults to 1%
100
+ - `max_percent` (int): Maximum percentage of null values to insert. Defaults to 10%
101
+ - `introduce_spag` (bool): Adds spelling and grammar errors into string data. Defaults to True
102
+ - `add_outliers` (bool): Adds outliers to numerical data. Defaults to True
103
+ - `add_std` (bool): Adds standard deviations to the data. Defaults to True
104
+ - `duplicate` (bool): Adds duplicate rows to the data. Defaults to True
105
+ - `add_null` (bool): Adds null values to the dataset. Defaults to True
@@ -0,0 +1,92 @@
1
+ # Complexifier
2
+
3
+ This makes your pandas dataframe even worse
4
+
5
+ ## Dependencies
6
+
7
+ - `pandas`
8
+ - `typo`
9
+ - `random`
10
+
11
+ ## Installation
12
+
13
+ `complexifier` can be installed using `pip`
14
+
15
+ ```sh
16
+ pip install complexifier
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Once installed you can use `complexifier` to add mistakes and outliers to your data
22
+
23
+ This library has several methods available:
24
+
25
+ ### `create_spag_error(word: str) -> str`
26
+
27
+ **Parameters**:
28
+ - `word` (str): The original word to potentially alter.
29
+
30
+ Introduces a 10% chance of a random spelling error in a given word. This function is useful for simulating typos and spelling mistakes in text data.
31
+
32
+ ### `introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame`
33
+
34
+ Applies the create_spag_error function to each string entry in specified columns of a DataFrame, introducing random spelling errors with a 10% probability.
35
+
36
+ **Parameters**:
37
+ - `df` (pd.DataFrame): The DataFrame to alter.
38
+ - `columns` (list or str): Column names to apply errors to. Defaults to all string columns.
39
+
40
+ ### `add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame`
41
+
42
+ Randomly adds or subtracts values in specified numeric columns at random indices, simulating outliers between 1% and 10% of the rows.
43
+
44
+ **Parameters**:
45
+ - `df` (pd.DataFrame): The DataFrame to modify.
46
+ - `columns` (list or str): Column names to adjust. Defaults to all numeric columns if not specified.
47
+
48
+ ### `add_standard_deviations(df: pd.DataFrame, columns=None, min_std=1, max_std=5) -> pd.DataFrame`
49
+
50
+ Adds between 1 to 5 standard deviations to random entries in specified numeric columns to simulate data anomalies.
51
+
52
+ **Parameters**:
53
+ - `df` (pd.DataFrame): The DataFrame to manipulate.
54
+ - `columns` (list or str): Column names to modify. Defaults to numeric columns if not specified.
55
+ - `min_std` (int): Minimum number of standard deviations to add. Defaults to 1
56
+ - `max_std` (int): Maximum number of standard deviations to add. Defaults to 5
57
+
58
+ ### `duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame`
59
+
60
+ Introduces duplicate rows into a DataFrame. This function is useful for testing deduplication processes.
61
+
62
+ **Parameters**:
63
+ - `df` (pd.DataFrame): DataFrame to which duplicates will be added.
64
+ - `sample_size` (int): number of rows to duplicate. A random percentage between 1% and 10% if not specified.
65
+
66
+ ### `add_nulls(df: pd.DataFrame, columns=None, min_percent=1, max_percent=10) -> pd.DataFrame`
67
+
68
+ Inserts null values into specified DataFrame columns. This simulates missing data conditions.
69
+
70
+ **Parameters**:
71
+ - `df` (pd.DataFrame): The DataFrame to modify.
72
+ - `columns` (list or str, optional): Specific columns to add nulls to. Defaults to all columns if not specified.
73
+ - `min_percent` (int): Minimum percentage of null values to insert. Defaults to 1%
74
+ - `max_percent` (int): Maximum percentage of null values to insert. Defaults to 10%
75
+
76
+ ### `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`
77
+
78
+ Adds all (or some) of the above methods. Really messes it up.
79
+
80
+ **Parameters**:
81
+ - `df` (pd.DataFrame): The DataFrame to modify.
82
+ - `columns` (list or str, optional): Specific columns to add nulls to. Defaults to all columns if not specified.
83
+ - `min_std` (int): Minimum number of standard deviations to add. Defaults to 1
84
+ - `max_std` (int): Maximum number of standard deviations to add. Defaults to 5
85
+ - `sample_size` (int, optional): Number of rows to duplicate. Randomly selected if not specified.
86
+ - `min_percent` (int): Minimum percentage of null values to insert. Defaults to 1%
87
+ - `max_percent` (int): Maximum percentage of null values to insert. Defaults to 10%
88
+ - `introduce_spag` (bool): Adds spelling and grammar errors into string data. Defaults to True
89
+ - `add_outliers` (bool): Adds outliers to numerical data. Defaults to True
90
+ - `add_std` (bool): Adds standard deviations to the data. Defaults to True
91
+ - `duplicate` (bool): Adds duplicate rows to the data. Defaults to True
92
+ - `add_null` (bool): Adds null values to the dataset. Defaults to True
@@ -3,7 +3,7 @@ requires = ["hatchling", "pandas", "shutup", "typo"]
3
3
  build-backend = "hatchling.build"
4
4
  [project]
5
5
  name = "complexifier"
6
- version = "0.2.3"
6
+ version = "0.3.1"
7
7
  authors = [
8
8
  { name="Ruy Zambrano", email="ruyzambrano@gmail.com" },
9
9
  ]
@@ -0,0 +1 @@
1
+ from .main import *
@@ -49,11 +49,15 @@ def introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame:
49
49
 
50
50
  Args:
51
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.
52
+ columns (list or str): Column names to apply errors to. Defaults to all string columns.
53
53
 
54
54
  Returns:
55
55
  pd.DataFrame: The DataFrame with potential spelling errors introduced.
56
56
  """
57
+ if not isinstance(df, pd.DataFrame):
58
+ raise TypeError("Must be a pandas")
59
+ if df.empty:
60
+ return df
57
61
  if not columns:
58
62
  columns = df.select_dtypes(include=["string", "object"]).columns
59
63
  elif isinstance(columns, str):
@@ -75,11 +79,15 @@ def add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame:
75
79
 
76
80
  Args:
77
81
  df (pd.DataFrame): The DataFrame to modify.
78
- columns (list or str, optional): Column names to adjust. Defaults to numeric columns if not specified.
82
+ columns (list or str): Column names to adjust. Defaults to all numeric columns if not specified.
79
83
 
80
84
  Returns:
81
85
  pd.DataFrame: The DataFrame with outliers added.
82
86
  """
87
+ if not isinstance(df, pd.DataFrame):
88
+ raise TypeError("Must be a pandas")
89
+ if df.empty:
90
+ return df
83
91
  if not columns:
84
92
  columns = df.select_dtypes(include="number").columns
85
93
  elif isinstance(columns, str):
@@ -91,7 +99,7 @@ def add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame:
91
99
  columns = [columns]
92
100
  elif not isinstance(columns, list):
93
101
  raise TypeError(f"Columns is type {type(columns)} but expected str or list")
94
-
102
+
95
103
  for col in columns:
96
104
  if col not in df.columns:
97
105
  raise ValueError(f"{col} is not a column.")
@@ -112,13 +120,17 @@ def add_standard_deviations(
112
120
 
113
121
  Args:
114
122
  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.
123
+ columns (list or str): Column names to modify. Defaults to numeric columns if not specified.
124
+ min_std (int): Minimum number of standard deviations to add. Defaults to 1
125
+ max_std (int): Maximum number of standard deviations to add. Defaults to 5
118
126
 
119
127
  Returns:
120
128
  pd.DataFrame: The DataFrame with deviations added.
121
129
  """
130
+ if not isinstance(df, pd.DataFrame):
131
+ raise TypeError("Must be a pandas")
132
+ if df.empty:
133
+ return df
122
134
  if not columns:
123
135
  columns = df.select_dtypes(include="number").columns
124
136
  elif isinstance(columns, str):
@@ -146,11 +158,15 @@ def duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame:
146
158
 
147
159
  Args:
148
160
  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.
161
+ sample_size (int): number of rows to duplicate. A random percentage between 1% and 10% if not specified.
150
162
 
151
163
  Returns:
152
164
  pd.DataFrame: The DataFrame with duplicate rows added.
153
165
  """
166
+ if not isinstance(df, pd.DataFrame):
167
+ raise TypeError("Must be a pandas")
168
+ if df.empty:
169
+ return df
154
170
  if not sample_size:
155
171
  sample_size = random.randint(len(df) // 100, len(df) // 10)
156
172
  new_rows = df.sample(sample_size)
@@ -166,12 +182,18 @@ def add_nulls(
166
182
  Args:
167
183
  df (pd.DataFrame): The DataFrame to modify.
168
184
  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.
185
+ min_percent (int): Minimum percentage of null values to insert. Defaults to 1%
186
+ max_percent (int): Maximum percentage of null values to insert. Defaults to 10%
171
187
 
172
188
  Returns:
173
189
  pd.DataFrame: The DataFrame with null values inserted.
174
190
  """
191
+ if not isinstance(df, pd.DataFrame):
192
+ raise TypeError("Must be a pandas")
193
+ if df.empty:
194
+ return df
195
+ if columns == []:
196
+ columns = df.columns.to_list()
175
197
  if not columns:
176
198
  columns = df.columns
177
199
  elif isinstance(columns, str):
@@ -181,9 +203,83 @@ def add_nulls(
181
203
 
182
204
  for col in columns:
183
205
  chosen_percent = random.randint(min_percent, max_percent) / 100
184
- sample_size = round(len(df)*chosen_percent)
206
+ sample_size = round(len(df) * chosen_percent)
185
207
  if sample_size == 0:
186
208
  sample_size = 1
187
209
  indices_to_none = df.sample(sample_size).index
188
210
  df.loc[indices_to_none, col] = None
189
211
  return df
212
+
213
+
214
+ def mess_it_up(
215
+ df: pd.DataFrame,
216
+ columns=None,
217
+ min_std=1,
218
+ max_std=5,
219
+ sample_size=None,
220
+ min_percent=1,
221
+ max_percent=10,
222
+ introduce_spag=True,
223
+ add_outliers=True,
224
+ add_std=True,
225
+ duplicate=True,
226
+ add_null=True
227
+ ) -> pd.DataFrame:
228
+ """
229
+ Applies several functions to add outliers, spelling errors and null values
230
+
231
+ Args:
232
+ df (pd.DataFrame): The DataFrame to modify.
233
+ columns (list or str, optional): Specific columns to add nulls to. Defaults to all columns if not specified.
234
+ min_std (int): Minimum number of standard deviations to add. Defaults to 1
235
+ max_std (int): Maximum number of standard deviations to add. Defaults to 5
236
+ sample_size (int, optional): Number of rows to duplicate. Randomly selected if not specified.
237
+ min_percent (int): Minimum percentage of null values to insert. Defaults to 1%
238
+ max_percent (int): Maximum percentage of null values to insert. Defaults to 10%
239
+ introduce_spag (bool): Adds spelling and grammar errors into string data. Defaults to True
240
+ add_outliers (bool): Adds outliers to numerical data. Defaults to True
241
+ add_std (bool): Adds standard deviations to the data. Defaults to True
242
+ duplicate (bool): Adds duplicate rows to the data. Defaults to True
243
+ add_null (bool): Adds null values to the dataset. Defaults to True
244
+
245
+ """
246
+ if not isinstance(df, pd.DataFrame):
247
+ raise TypeError("Must be a pandas")
248
+ if df.empty:
249
+ return df
250
+ if columns == []:
251
+ columns = df.columns.to_list()
252
+ if not columns:
253
+ columns = df.columns.to_list()
254
+ elif isinstance(columns, str):
255
+ columns = [columns]
256
+ elif not isinstance(columns, list):
257
+ raise TypeError(f"Columns is type {type(columns)} but expected str or list")
258
+
259
+ string_cols = df[columns].select_dtypes(include="string").columns.to_list()
260
+ numeric_cols = df[columns].select_dtypes(include="number").columns.to_list()
261
+ if string_cols:
262
+ if introduce_spag:
263
+ df = introduce_spag_error(df, string_cols)
264
+ if numeric_cols:
265
+ if add_outliers:
266
+ df = add_or_subtract_outliers(df, numeric_cols)
267
+ if add_std:
268
+ df = add_standard_deviations(
269
+ df, numeric_cols, min_std, max_std
270
+ )
271
+ if duplicate:
272
+ df = duplicate_rows(df, sample_size)
273
+ if add_null:
274
+ df = add_nulls(df, columns, min_percent, max_percent)
275
+ return df
276
+
277
+
278
+ data = pd.DataFrame(
279
+ {
280
+ "Name": ["Alice", "Bob", "Charlie", "David", "Eva"],
281
+ "Age": [28, 34, 29, 42, 25],
282
+ "City": ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"],
283
+ "Salary": [70000, 80000, 72000, 95000, 67000],
284
+ }
285
+ )
@@ -0,0 +1,283 @@
1
+ import pytest
2
+ from unittest.mock import patch
3
+ import pandas as pd
4
+ from math import floor
5
+ from .main import (
6
+ create_spag_error,
7
+ introduce_spag_error,
8
+ add_or_subtract_outliers,
9
+ add_standard_deviations,
10
+ duplicate_rows,
11
+ add_nulls,
12
+ mess_it_up,
13
+ )
14
+
15
+
16
+ @pytest.fixture
17
+ def sample_df():
18
+ """
19
+ A fixture for tests
20
+ """
21
+ return pd.DataFrame(
22
+ {
23
+ "Name": ["Alice", "Bob", "Charlie", "David", "Eva"],
24
+ "Age": [28, 34, 29, 42, 25],
25
+ "City": ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"],
26
+ "Salary": [70000, 80000, 72000, 95000, 67000],
27
+ }
28
+ )
29
+
30
+
31
+ @pytest.mark.parametrize("word", ["hello", "world", "testword"])
32
+ def test_create_spag_error(word):
33
+ """
34
+ Tests that the word returned is still a string
35
+ """
36
+ error_word = create_spag_error(word)
37
+ assert isinstance(error_word, str)
38
+
39
+
40
+ @pytest.mark.parametrize("column_name", ["Name", "City"])
41
+ def test_introduce_spag_error(sample_df, column_name):
42
+ """
43
+ Test that DataFrame columns contain the potential spelling errors
44
+ """
45
+ df_with_errors = introduce_spag_error(sample_df, columns=column_name)
46
+ assert column_name in df_with_errors.columns
47
+ assert all([isinstance(name, str) for name in df_with_errors[column_name].values])
48
+
49
+
50
+ @pytest.mark.parametrize("column_name", ["Salary"])
51
+ def test_add_or_subtract_outliers(sample_df, column_name):
52
+ """
53
+ Test that outliers are added to the numerical columns
54
+ """
55
+ df_with_outliers = add_or_subtract_outliers(sample_df, columns=column_name)
56
+ assert column_name in df_with_outliers.columns
57
+ assert pd.api.types.is_numeric_dtype(df_with_outliers[column_name])
58
+
59
+
60
+ @pytest.mark.parametrize("column_name", ["Age", "Salary"])
61
+ def test_add_standard_deviations(sample_df, column_name):
62
+ """
63
+ Test that standard deviations are added to the specified column
64
+ """
65
+ df_with_deviations = add_standard_deviations(sample_df, columns=column_name)
66
+ assert column_name in df_with_deviations.columns
67
+ assert pd.api.types.is_numeric_dtype(df_with_deviations[column_name])
68
+
69
+
70
+ @pytest.mark.parametrize("sample_size", [0, 2, 5])
71
+ def test_duplicate_rows(sample_df, sample_size):
72
+ """
73
+ Test that duplicate rows are added
74
+ """
75
+ df_with_duplicates = duplicate_rows(sample_df, sample_size=sample_size)
76
+ assert len(df_with_duplicates) == len(sample_df) + sample_size
77
+
78
+
79
+ @pytest.mark.parametrize(
80
+ "column_name,min_percent,max_percent", [("City", 10, 20), ("Name", 20, 30)]
81
+ )
82
+ def test_add_nulls(sample_df, column_name, min_percent, max_percent):
83
+ """
84
+ Test that nulls are added to the DataFrame
85
+ """
86
+ df_with_nulls = add_nulls(
87
+ sample_df, columns=column_name, min_percent=min_percent, max_percent=max_percent
88
+ )
89
+ assert column_name in df_with_nulls.columns
90
+ null_count = df_with_nulls[column_name].isnull().sum()
91
+ print(df_with_nulls)
92
+ assert float(null_count) >= 0.0
93
+ assert (
94
+ (min_percent * 0.01 * len(sample_df))
95
+ <= null_count
96
+ <= round(max_percent * 0.01 * len(sample_df))
97
+ )
98
+
99
+
100
+ def test_mess_it_up(sample_df):
101
+ """
102
+ Tests the mess_it_up_function
103
+ """
104
+ df_messed_up = mess_it_up(
105
+ sample_df,
106
+ columns=["Name", "Age", "City", "Salary"],
107
+ min_std=1,
108
+ max_std=2,
109
+ sample_size=2,
110
+ min_percent=10,
111
+ max_percent=20,
112
+ )
113
+
114
+ # Check that the specified number of duplicate rows has been added
115
+ assert (
116
+ len(df_messed_up) == len(sample_df) + 2
117
+ ), "Unexpected number of duplicate rows"
118
+
119
+ # Check for potential null values
120
+ for col in ["Name", "Age", "City", "Salary"]:
121
+ null_count = df_messed_up[col].isnull().sum()
122
+ assert null_count > 0, f"No nulls introduced in {col} column"
123
+
124
+
125
+ @pytest.mark.parametrize(
126
+ "columns,min_std,max_std,sample_size,min_percent,max_percent",
127
+ [
128
+ (["Name", "Age"], 1, 2, 2, 10, 20),
129
+ (["City", "Salary"], 2, 4, 3, 15, 25),
130
+ (["Name", "City", "Age"], 1, 3, 1, 5, 10),
131
+ ],
132
+ )
133
+ def test_parameterised_mess_it_up(
134
+ sample_df, columns, min_std, max_std, sample_size, min_percent, max_percent
135
+ ):
136
+ """
137
+ Tests the mess_it_up function with different configurations
138
+ """
139
+ df_messed_up = mess_it_up(
140
+ sample_df,
141
+ columns=columns,
142
+ min_std=min_std,
143
+ max_std=max_std,
144
+ sample_size=sample_size,
145
+ min_percent=min_percent,
146
+ max_percent=max_percent,
147
+ )
148
+
149
+ # Check that the specified number of duplicate rows has been added
150
+ assert (
151
+ len(df_messed_up) == len(sample_df) + sample_size
152
+ ), "Unexpected number of duplicate rows"
153
+
154
+ # Check for potential null values
155
+ for col in columns:
156
+ null_count = df_messed_up[col].isnull().sum()
157
+ assert null_count > 0, f"No nulls introduced in {col} column"
158
+
159
+
160
+ def test_empty_dataframe_introduce_spag_error():
161
+ """
162
+ Test introduce_spag_error with an empty DataFrame
163
+ """
164
+ empty_df = pd.DataFrame(columns=["Name"])
165
+ df_spag = introduce_spag_error(empty_df, columns="Name")
166
+ assert df_spag.empty
167
+
168
+
169
+ def test_empty_dataframe_add_or_subtract_outliers():
170
+ """
171
+ Test add_or_subtract_outliers with an empty DataFrame
172
+ """
173
+ empty_df = pd.DataFrame(columns=["Salary"])
174
+ df_outliers = add_or_subtract_outliers(empty_df, columns="Salary")
175
+ assert df_outliers.empty
176
+
177
+
178
+ def test_empty_dataframe_add_standard_deviations():
179
+ """
180
+ Test add_standard_deviations with an empty DataFrame
181
+ """
182
+ empty_df = pd.DataFrame(columns=["Age"])
183
+ df_deviations = add_standard_deviations(empty_df, columns="Age")
184
+ assert df_deviations.empty
185
+
186
+
187
+ def test_empty_dataframe_duplicate_rows():
188
+ """
189
+ Test duplicate_rows with an empty DataFrame
190
+ """
191
+ empty_df = pd.DataFrame(columns=["Name", "Age"])
192
+ df_duplicates = duplicate_rows(empty_df, sample_size=2)
193
+ assert df_duplicates.empty
194
+
195
+
196
+ def test_empty_dataframe_add_nulls():
197
+ """
198
+ Test add_nulls with an empty DataFrame
199
+ """
200
+ empty_df = pd.DataFrame(columns=["City"])
201
+ df_nulls = add_nulls(empty_df, columns="City", min_percent=10, max_percent=20)
202
+ assert df_nulls.empty
203
+
204
+
205
+ def test_empty_dataframe_mess_it_up():
206
+ """
207
+ Test mess_it_up with an empty DataFrame
208
+ """
209
+ empty_df = pd.DataFrame(columns=["Name", "Age", "City", "Salary"])
210
+ df_messed = mess_it_up(empty_df, columns=["Name", "Age", "City", "Salary"])
211
+ assert df_messed.empty
212
+
213
+
214
+ def test_nonexistent_column_introduce_spag_error(sample_df):
215
+ """
216
+ Test introduce_spag_error with a nonexistent column
217
+ """
218
+ with pytest.raises(ValueError):
219
+ introduce_spag_error(sample_df, columns="Nonexistent")
220
+
221
+
222
+ def test_nonexistent_column_add_or_subtract_outliers(sample_df):
223
+ """
224
+ Test add_or_subtract_outliers with a nonexistent column
225
+ """
226
+ with pytest.raises(ValueError):
227
+ add_or_subtract_outliers(sample_df, columns="Nonexistent")
228
+
229
+
230
+ def test_nonexistent_column_add_standard_deviations(sample_df):
231
+ """
232
+ Test add_standard_deviations with a nonexistent column
233
+ """
234
+ with pytest.raises(ValueError):
235
+ add_standard_deviations(sample_df, columns="Nonexistent")
236
+
237
+
238
+ def test_mixed_data_types_introduce_spag_error():
239
+ """
240
+ Test introduce_spag_error with mixed data types
241
+ """
242
+ mixed_df = pd.DataFrame(
243
+ {
244
+ "String": ["A", "B", "C"],
245
+ "Integer": [1, 2, 3],
246
+ "Float": [0.1, 0.2, 0.3],
247
+ "DateTime": pd.to_datetime(["2024-01-01", "2024-01-02", "2024-01-03"]),
248
+ }
249
+ )
250
+ df_spag = introduce_spag_error(mixed_df, columns="String")
251
+ assert "String" in df_spag.columns
252
+
253
+
254
+ def test_mixed_data_types_add_or_subtract_outliers():
255
+ """
256
+ Test add_or_subtract_outliers with mixed data types
257
+ """
258
+ mixed_df = pd.DataFrame(
259
+ {
260
+ "String": ["A", "B", "C"],
261
+ "Integer": [1, 2, 3],
262
+ "Float": [0.1, 0.2, 0.3],
263
+ "DateTime": pd.to_datetime(["2024-01-01", "2024-01-02", "2024-01-03"]),
264
+ }
265
+ )
266
+ df_outliers = add_or_subtract_outliers(mixed_df, columns="Integer")
267
+ assert "Integer" in df_outliers.columns
268
+
269
+
270
+ def test_mixed_data_types_add_standard_deviations():
271
+ """
272
+ Test add_standard_deviations with mixed data types
273
+ """
274
+ mixed_df = pd.DataFrame(
275
+ {
276
+ "String": ["A", "B", "C"],
277
+ "Integer": [1, 2, 3],
278
+ "Float": [0.1, 0.2, 0.3],
279
+ "DateTime": pd.to_datetime(["2024-01-01", "2024-01-02", "2024-01-03"]),
280
+ }
281
+ )
282
+ df_deviations = add_standard_deviations(mixed_df, columns="Float")
283
+ assert "Float" in df_deviations.columns
@@ -1,84 +0,0 @@
1
- Metadata-Version: 2.3
2
- Name: complexifier
3
- Version: 0.2.3
4
- Summary: A library to make data worse
5
- Project-URL: Homepage, https://github.com/ruyzambrano/complexifier
6
- Project-URL: Issues, https://github.com/ruyzambrano/complexifier/issues
7
- Author-email: Ruy Zambrano <ruyzambrano@gmail.com>
8
- Classifier: License :: OSI Approved :: MIT License
9
- Classifier: Operating System :: OS Independent
10
- Classifier: Programming Language :: Python :: 3
11
- Requires-Python: >=3.8
12
- Description-Content-Type: text/markdown
13
-
14
- # Complexifier
15
-
16
- This makes your pandas dataframe even worse
17
-
18
- ## Dependencies
19
-
20
- - `pandas`
21
- - `typo`
22
- - `random`
23
-
24
- ## Installation
25
-
26
- `complexifier` can be installed using `pip`
27
-
28
- ```sh
29
- pip install complexifier
30
- ```
31
-
32
- ## Usage
33
-
34
- Once installed you can use `complexifier` to add mistakes and outliers to your data
35
-
36
- This library has several methods available:
37
-
38
- ### `create_spag_error(word: str) -> str`
39
-
40
- Introduces a 10% chance of a random spelling error in a given word. This function is useful for simulating typos and spelling mistakes in text data.
41
-
42
- ### `introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame`
43
-
44
- Applies the create_spag_error function to each string entry in specified columns of a DataFrame, introducing random spelling errors with a 10% probability.
45
-
46
- **Parameters**:
47
- - `df`: The DataFrame to be altered.
48
- - `columns`: Optional; specify column names to apply errors to. If not provided, it defaults to all string columns.
49
-
50
- ### `add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame`
51
-
52
- Randomly adds or subtracts values in specified numeric columns at random indices, simulating outliers between 1% and 10% of the rows.
53
-
54
- **Parameters**:
55
- - `df`: DataFrame to be modified.
56
- - `columns`: Optional; specify columns to adjust.
57
-
58
- ### `add_standard_deviations(df: pd.DataFrame, columns=None, min_std=1, max_std=5) -> pd.DataFrame`
59
-
60
- Adds between 1 to 5 standard deviations to random entries in specified numeric columns to simulate data anomalies.
61
-
62
- **Parameters**:
63
- - `df`: The DataFrame to manipulate.
64
- - `columns`: Optional; specify columns to modify.
65
- - `min_std`: Minimum number of standard deviations to add.
66
- - `max_std`: Maximum number of standard deviations to add.
67
-
68
- ### `duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame`
69
-
70
- Introduces duplicate rows into a DataFrame. This function is useful for testing deduplication processes.
71
-
72
- **Parameters**:
73
- - `df`: DataFrame where duplicates will be introduced.
74
- - `sample_size`: Optional; number of rows to duplicate. A random percentage between 1% and 10% if not specified.
75
-
76
- ### `add_nulls(df: pd.DataFrame, columns=None, min_percent=1, max_percent=10) -> pd.DataFrame`
77
-
78
- Inserts null values into specified DataFrame columns. This simulates missing data conditions.
79
-
80
- **Parameters**:
81
- - `df`: The DataFrame to modify.
82
- - `columns`: Optional; specific columns to add `nulls` to.
83
- - `min_percent`: Minimum percentage of `null` values to insert.
84
- - `max_percent`: Maximum percentage of `null` values to insert.
@@ -1,71 +0,0 @@
1
- # Complexifier
2
-
3
- This makes your pandas dataframe even worse
4
-
5
- ## Dependencies
6
-
7
- - `pandas`
8
- - `typo`
9
- - `random`
10
-
11
- ## Installation
12
-
13
- `complexifier` can be installed using `pip`
14
-
15
- ```sh
16
- pip install complexifier
17
- ```
18
-
19
- ## Usage
20
-
21
- Once installed you can use `complexifier` to add mistakes and outliers to your data
22
-
23
- This library has several methods available:
24
-
25
- ### `create_spag_error(word: str) -> str`
26
-
27
- Introduces a 10% chance of a random spelling error in a given word. This function is useful for simulating typos and spelling mistakes in text data.
28
-
29
- ### `introduce_spag_error(df: pd.DataFrame, columns=None) -> pd.DataFrame`
30
-
31
- Applies the create_spag_error function to each string entry in specified columns of a DataFrame, introducing random spelling errors with a 10% probability.
32
-
33
- **Parameters**:
34
- - `df`: The DataFrame to be altered.
35
- - `columns`: Optional; specify column names to apply errors to. If not provided, it defaults to all string columns.
36
-
37
- ### `add_or_subtract_outliers(df: pd.DataFrame, columns=None) -> pd.DataFrame`
38
-
39
- Randomly adds or subtracts values in specified numeric columns at random indices, simulating outliers between 1% and 10% of the rows.
40
-
41
- **Parameters**:
42
- - `df`: DataFrame to be modified.
43
- - `columns`: Optional; specify columns to adjust.
44
-
45
- ### `add_standard_deviations(df: pd.DataFrame, columns=None, min_std=1, max_std=5) -> pd.DataFrame`
46
-
47
- Adds between 1 to 5 standard deviations to random entries in specified numeric columns to simulate data anomalies.
48
-
49
- **Parameters**:
50
- - `df`: The DataFrame to manipulate.
51
- - `columns`: Optional; specify columns to modify.
52
- - `min_std`: Minimum number of standard deviations to add.
53
- - `max_std`: Maximum number of standard deviations to add.
54
-
55
- ### `duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame`
56
-
57
- Introduces duplicate rows into a DataFrame. This function is useful for testing deduplication processes.
58
-
59
- **Parameters**:
60
- - `df`: DataFrame where duplicates will be introduced.
61
- - `sample_size`: Optional; number of rows to duplicate. A random percentage between 1% and 10% if not specified.
62
-
63
- ### `add_nulls(df: pd.DataFrame, columns=None, min_percent=1, max_percent=10) -> pd.DataFrame`
64
-
65
- Inserts null values into specified DataFrame columns. This simulates missing data conditions.
66
-
67
- **Parameters**:
68
- - `df`: The DataFrame to modify.
69
- - `columns`: Optional; specific columns to add `nulls` to.
70
- - `min_percent`: Minimum percentage of `null` values to insert.
71
- - `max_percent`: Maximum percentage of `null` values to insert.
@@ -1 +0,0 @@
1
- from .main import *
@@ -1,81 +0,0 @@
1
- import pytest
2
- import pandas as pd
3
- from math import floor
4
- from .main import (
5
- create_spag_error,
6
- introduce_spag_error,
7
- add_or_subtract_outliers,
8
- add_standard_deviations,
9
- duplicate_rows,
10
- add_nulls
11
- )
12
-
13
- @pytest.fixture
14
- def sample_df():
15
- """
16
- A fixture for tests
17
- """
18
- return pd.DataFrame({
19
- 'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],
20
- 'Age': [28, 34, 29, 42, 25],
21
- 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
22
- 'Salary': [70000, 80000, 72000, 95000, 67000]
23
- })
24
-
25
- @pytest.mark.parametrize("word", ["hello", "world", "testword"])
26
- def test_create_spag_error(word):
27
- """
28
- Tests that the word returned is still a string
29
- """
30
- error_word = create_spag_error(word)
31
- assert isinstance(error_word, str)
32
-
33
- @pytest.mark.parametrize("column_name", ["Name", "City"])
34
- def test_introduce_spag_error(sample_df, column_name):
35
- """
36
- Test that DataFrame columns contain the potential spelling errors
37
- """
38
- df_with_errors = introduce_spag_error(sample_df, columns=column_name)
39
- assert column_name in df_with_errors.columns
40
- assert all([isinstance(name, str) for name in df_with_errors[column_name].values])
41
-
42
- @pytest.mark.parametrize("column_name", ["Salary"])
43
- def test_add_or_subtract_outliers(sample_df, column_name):
44
- """
45
- Test that outliers are added to the numerical columns
46
- """
47
- df_with_outliers = add_or_subtract_outliers(sample_df, columns=column_name)
48
- assert column_name in df_with_outliers.columns
49
- assert pd.api.types.is_numeric_dtype(df_with_outliers[column_name])
50
-
51
- @pytest.mark.parametrize("column_name", ["Age", "Salary"])
52
- def test_add_standard_deviations(sample_df, column_name):
53
- """
54
- Test that standard deviations are added to the specified column
55
- """
56
- df_with_deviations = add_standard_deviations(sample_df, columns=column_name)
57
- assert column_name in df_with_deviations.columns
58
- assert pd.api.types.is_numeric_dtype(df_with_deviations[column_name])
59
-
60
- @pytest.mark.parametrize("sample_size", [0, 2, 5])
61
- def test_duplicate_rows(sample_df, sample_size):
62
- """
63
- Test that duplicate rows are added
64
- """
65
- df_with_duplicates = duplicate_rows(sample_df, sample_size=sample_size)
66
- assert len(df_with_duplicates) == len(sample_df) + sample_size
67
-
68
- @pytest.mark.parametrize("column_name,min_percent,max_percent", [
69
- ("City", 10, 20),
70
- ("Name", 20, 30)
71
- ])
72
- def test_add_nulls(sample_df, column_name, min_percent, max_percent):
73
- """
74
- Test that nulls are added to the DataFrame
75
- """
76
- df_with_nulls = add_nulls(sample_df, columns=column_name, min_percent=min_percent, max_percent=max_percent)
77
- assert column_name in df_with_nulls.columns
78
- null_count = df_with_nulls[column_name].isnull().sum()
79
- print(df_with_nulls)
80
- assert float(null_count) >= 0.0
81
- assert (min_percent*0.01*len(sample_df)) <= null_count <= round(max_percent*0.01*len(sample_df))
File without changes
File without changes