complexifier 0.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: complexifier
3
- Version: 0.2.2
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.2"
6
+ version = "0.2.3"
7
7
  authors = [
8
8
  { name="Ruy Zambrano", email="ruyzambrano@gmail.com" },
9
9
  ]
@@ -19,19 +19,19 @@ def create_spag_error(word: str) -> str:
19
19
  weight = random.randint(1, 100)
20
20
  match weight:
21
21
  case 1:
22
- return error_object.missing_char()
22
+ return error_object.missing_char().result
23
23
  case 2:
24
- return error_object.char_swap()
24
+ return error_object.char_swap().result
25
25
  case 3:
26
- return error_object.extra_char()
26
+ return error_object.extra_char().result
27
27
  case 4:
28
- return error_object.nearby_char()
28
+ return error_object.nearby_char().result
29
29
  case 5:
30
- return error_object.similar_char()
30
+ return error_object.similar_char().result
31
31
  case 6:
32
- return error_object.random_space()
32
+ return error_object.random_space().result
33
33
  case 7:
34
- return error_object.repeated_char()
34
+ return error_object.repeated_char().result
35
35
  case 8:
36
36
  return word.lower()
37
37
  case 9:
@@ -152,7 +152,7 @@ def duplicate_rows(df: pd.DataFrame, sample_size=None) -> pd.DataFrame:
152
152
  pd.DataFrame: The DataFrame with duplicate rows added.
153
153
  """
154
154
  if not sample_size:
155
- sample_size = random.randint(len(df[col]) // 100, len(df[col]) // 10)
155
+ sample_size = random.randint(len(df) // 100, len(df) // 10)
156
156
  new_rows = df.sample(sample_size)
157
157
  return pd.concat([df, new_rows])
158
158
 
@@ -180,8 +180,10 @@ def add_nulls(
180
180
  raise TypeError(f"Columns is type {type(columns)} but expected str or list")
181
181
 
182
182
  for col in columns:
183
- indices_to_none = df.sample(
184
- len(df) * random.randint(min_percent, max_percent) // 100
185
- ).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
186
188
  df.loc[indices_to_none, col] = None
187
189
  return df
@@ -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,70 +0,0 @@
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
File without changes
File without changes
File without changes