-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #474 from smog-root/data_processing
Enhance Outlier Detection Test Coverage and Edge Case Handling
- Loading branch information
Showing
13 changed files
with
535 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/home/runner/work/_temp/fcbe3165-95cc-4878-8e84-ef18a16fbf3a.sh: line 1: scripts/build_directory_md.py: Permission denied | ||
/home/runner/work/_temp/8260a9d1-eff9-42f4-b1a3-1bd90558f43e.sh: line 1: scripts/build_directory_md.py: Permission denied |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,61 @@ | ||
import pandas as pd | ||
|
||
def remove_duplicates(df: pd.DataFrame) -> pd.DataFrame: | ||
""" | ||
Remove duplicate rows from the DataFrame. | ||
Args: | ||
- df (pd.DataFrame): The input DataFrame. | ||
Returns: | ||
- pd.DataFrame: A new DataFrame with duplicates removed. | ||
""" | ||
if df.empty: | ||
print("Warning: The DataFrame is empty.") | ||
return df.drop_duplicates() | ||
|
||
def replace_missing_with_mean(df: pd.DataFrame, column: str) -> pd.DataFrame: | ||
mean_value = df[column].mean() | ||
df[column].fillna(mean_value, inplace=True) | ||
def replace_missing_with_mean(df: pd.DataFrame, column: str, default_value: float = None) -> pd.DataFrame: | ||
""" | ||
Replace missing values in a specified column with the column's mean or a provided default value. | ||
Args: | ||
- df (pd.DataFrame): The input DataFrame. | ||
- column (str): The column name where missing values need to be replaced. | ||
- default_value (float, optional): If provided, will replace missing values with this value. | ||
Returns: | ||
- pd.DataFrame: A new DataFrame with missing values replaced. | ||
Raises: | ||
- ValueError: If the column does not exist in the DataFrame. | ||
""" | ||
if column not in df.columns: | ||
raise ValueError(f"Column '{column}' does not exist in the DataFrame.") | ||
|
||
if default_value is not None: | ||
df[column].fillna(default_value, inplace=True) | ||
else: | ||
mean_value = df[column].mean() | ||
df[column].fillna(mean_value, inplace=True) | ||
|
||
return df | ||
|
||
def standardize_text(df: pd.DataFrame, column: str) -> pd.DataFrame: | ||
""" | ||
Standardize the text in a specified column by converting it to lowercase and stripping whitespace. | ||
Args: | ||
- df (pd.DataFrame): The input DataFrame. | ||
- column (str): The column to standardize. | ||
Returns: | ||
- pd.DataFrame: A new DataFrame with standardized text in the specified column. | ||
Raises: | ||
- ValueError: If the column does not exist in the DataFrame. | ||
""" | ||
if column not in df.columns: | ||
raise ValueError(f"Column '{column}' does not exist in the DataFrame.") | ||
|
||
df[column] = df[column].str.lower().str.strip() | ||
return df | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,48 @@ | ||
import pandas as pd | ||
from sklearn.preprocessing import OneHotEncoder, LabelEncoder | ||
|
||
def one_hot_encode(df: pd.DataFrame, column: str) -> pd.DataFrame: | ||
encoder = OneHotEncoder(sparse=False, drop='first') | ||
def one_hot_encode(df: pd.DataFrame, column: str, drop_first: bool = True) -> pd.DataFrame: | ||
""" | ||
Perform one-hot encoding on a specified column. | ||
Args: | ||
- df (pd.DataFrame): The input DataFrame. | ||
- column (str): The column name to encode. | ||
- drop_first (bool): Whether to drop the first category to avoid multicollinearity (default is True). | ||
Returns: | ||
- pd.DataFrame: A new DataFrame with the one-hot encoded column(s). | ||
Raises: | ||
- ValueError: If the column does not exist in the DataFrame. | ||
""" | ||
if column not in df.columns: | ||
raise ValueError(f"Column '{column}' does not exist in the DataFrame.") | ||
|
||
encoder = OneHotEncoder(sparse=False, drop='first' if drop_first else None) | ||
encoded = encoder.fit_transform(df[[column]]) | ||
encoded_df = pd.DataFrame(encoded, columns=encoder.get_feature_names_out([column])) | ||
|
||
# Concatenate the original dataframe without the encoded column and the encoded DataFrame | ||
return pd.concat([df.drop(column, axis=1), encoded_df], axis=1) | ||
|
||
def label_encode(df: pd.DataFrame, column: str) -> pd.DataFrame: | ||
""" | ||
Perform label encoding on a specified column (converting categories to integer labels). | ||
Args: | ||
- df (pd.DataFrame): The input DataFrame. | ||
- column (str): The column name to encode. | ||
Returns: | ||
- pd.DataFrame: A new DataFrame with the label encoded column. | ||
Raises: | ||
- ValueError: If the column does not exist in the DataFrame. | ||
""" | ||
if column not in df.columns: | ||
raise ValueError(f"Column '{column}' does not exist in the DataFrame.") | ||
|
||
encoder = LabelEncoder() | ||
df[column] = encoder.fit_transform(df[column]) | ||
return df | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.