complexifier 0.2.1__tar.gz → 0.2.3__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.
@@ -4,4 +4,5 @@ dist
4
4
  *.txt
5
5
  *.sh
6
6
  __pycache__
7
- testing.py
7
+ testing.py
8
+ .coverage
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: complexifier
3
- Version: 0.2.1
3
+ Version: 0.2.3
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
@@ -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.1"
6
+ version = "0.2.3"
7
7
  authors = [
8
8
  { name="Ruy Zambrano", email="ruyzambrano@gmail.com" },
9
9
  ]
@@ -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
 
@@ -18,19 +19,19 @@ def create_spag_error(word: str) -> str:
18
19
  weight = random.randint(1, 100)
19
20
  match weight:
20
21
  case 1:
21
- return error_object.missing_char()
22
+ return error_object.missing_char().result
22
23
  case 2:
23
- return error_object.char_swap()
24
+ return error_object.char_swap().result
24
25
  case 3:
25
- return error_object.extra_char()
26
+ return error_object.extra_char().result
26
27
  case 4:
27
- return error_object.nearby_char()
28
+ return error_object.nearby_char().result
28
29
  case 5:
29
- return error_object.similar_char()
30
+ return error_object.similar_char().result
30
31
  case 6:
31
- return error_object.random_space()
32
+ return error_object.random_space().result
32
33
  case 7:
33
- return error_object.repeated_char()
34
+ return error_object.repeated_char().result
34
35
  case 8:
35
36
  return word.lower()
36
37
  case 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:
@@ -135,7 +152,7 @@ def duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame:
135
152
  pd.DataFrame: The DataFrame with duplicate rows added.
136
153
  """
137
154
  if not sample_size:
138
- sample_size = random.randint(len(df[col]) // 100, len(df[col]) // 10)
155
+ sample_size = random.randint(len(df) // 100, len(df) // 10)
139
156
  new_rows = df.sample(sample_size)
140
157
  return pd.concat([df, new_rows])
141
158
 
@@ -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
- """Inserts null values into specified DataFrame columns.
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,16 @@ 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
- indices_to_none = df.sample(
161
- len(df) * random.randint(min_percent, max_percent) // 100
162
- ).index
183
+ chosen_percent = random.randint(min_percent, max_percent) / 100
184
+ sample_size = round(len(df)*chosen_percent)
185
+ if sample_size == 0:
186
+ sample_size = 1
187
+ indices_to_none = df.sample(sample_size).index
163
188
  df.loc[indices_to_none, col] = None
164
189
  return df
165
-
@@ -0,0 +1,81 @@
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))
@@ -1 +0,0 @@
1
- from main import *
File without changes
File without changes