complexifier 0.2.0__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.
@@ -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.0
3
+ Version: 0.2.2
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.0"
6
+ version = "0.2.2"
7
7
  authors = [
8
8
  { name="Ruy Zambrano", email="ruyzambrano@gmail.com" },
9
9
  ]
@@ -0,0 +1 @@
1
+ from .main import *
@@ -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 spag error to a word"""
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 the columms of between 1% and 10% of the rows"""
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
- """Add 1-5 standard deviations for each column"""
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 dataframe"""
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
@@ -1 +0,0 @@
1
- from main import *
File without changes
File without changes