|
3 | 3 | from pytest import raises |
4 | 4 | import pandas as pd |
5 | 5 | from pandas._testing import assert_frame_equal |
| 6 | +import numpy as np |
| 7 | + |
6 | 8 |
|
7 | 9 |
|
8 | 10 | def test_version(): |
@@ -238,3 +240,77 @@ def test_scaler(): |
238 | 240 | assert pd.DataFrame.equals( |
239 | 241 | minmax_scaled_mock_df_2, mock_df_2_minmax |
240 | 242 | ), "The returned dataframe using constant imputer is not correct" |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | +def test_outlier_identifier(): |
| 247 | + test_df = pd.DataFrame({ |
| 248 | + 'SepalLengthCm': [5.1, 4.9, 4.7, 5.5, 5.1, 50, 5.4, 5.0, 5.2, 5.3, 5.1], |
| 249 | + 'SepalWidthCm': [1.4, 1.4, 20, 2.0, 0.7, 1.6, 1.2, 1.4, 1.8, 1.5, 2.1], |
| 250 | + 'PetalWidthCm' :[0.2, 0.2, 0.2, 0.3, 0.4, 0.5, 0.5, 0.6, 0.4, 0.2, 5], |
| 251 | + 'Species':['Iris-setosa', 'Iris-virginica', 'Iris-germanica', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa'] |
| 252 | + }) |
| 253 | + |
| 254 | + test_column = ['SepalLengthCm', 'SepalWidthCm', 'PetalWidthCm'] |
| 255 | + |
| 256 | + median_output = pd.DataFrame({ |
| 257 | + 'SepalLengthCm': [5.1, 4.9, 4.7, 5.5, 5.1, 5.1, 5.4, 5.0, 5.2, 5.3, 5.1], |
| 258 | + 'SepalWidthCm': [1.4, 1.4, 1.5, 2.0, 0.7, 1.6, 1.2, 1.4, 1.8, 1.5, 2.1], |
| 259 | + 'PetalWidthCm' :[0.2, 0.2, 0.2, 0.3, 0.4, 0.5, 0.5, 0.6, 0.4, 0.2, 0.4], |
| 260 | + 'Species':['Iris-setosa', 'Iris-virginica', 'Iris-germanica', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa'] |
| 261 | + }) |
| 262 | + |
| 263 | + trim_output = pd.DataFrame({ |
| 264 | + 'SepalLengthCm': [5.1, 4.9, 5.5, 5.1, 5.4, 5.0, 5.2, 5.3], |
| 265 | + 'SepalWidthCm': [1.4, 1.4, 2.0, 0.7, 1.2, 1.4, 1.8, 1.5], |
| 266 | + 'PetalWidthCm' :[0.2, 0.2, 0.3, 0.4, 0.5, 0.6, 0.4, 0.2], |
| 267 | + 'Species':['Iris-setosa', 'Iris-virginica', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa'] |
| 268 | + }) |
| 269 | + |
| 270 | + mean_output = pd.DataFrame({ |
| 271 | + 'SepalLengthCm': [5.1, 4.9, 4.7, 5.5, 5.1, 9.21, 5.4, 5.0, 5.2, 5.3, 5.1], |
| 272 | + 'SepalWidthCm': [1.4, 1.4, 3.19, 2.0, 0.7, 1.6, 1.2, 1.4, 1.8, 1.5, 2.1], |
| 273 | + 'PetalWidthCm' :[0.2, 0.2, 0.2, 0.3, 0.4, 0.5, 0.5, 0.6, 0.4, 0.2, 0.77], |
| 274 | + 'Species':['Iris-setosa', 'Iris-virginica', 'Iris-germanica', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa'] |
| 275 | + }) |
| 276 | + |
| 277 | + column_output= pd.DataFrame({ |
| 278 | + 'SepalLengthCm': [5.1, 4.9, 4.7, 5.5, 5.1, 9.21, 5.4, 5.0, 5.2, 5.3, 5.1], |
| 279 | + 'SepalWidthCm': [1.4, 1.4, 20, 2.0, 0.7, 1.6, 1.2, 1.4, 1.8, 1.5, 2.1], |
| 280 | + 'PetalWidthCm' :[0.2, 0.2, 0.2, 0.3, 0.4, 0.5, 0.5, 0.6, 0.4, 0.2, 5], |
| 281 | + 'Species':['Iris-setosa', 'Iris-virginica', 'Iris-germanica', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa', 'Iris-setosa'] |
| 282 | + }) |
| 283 | + |
| 284 | + # Test if the imput is not dataFrame |
| 285 | + with raises(TypeError): |
| 286 | + eda_utils_py.outlier_identifier("not dataframe") |
| 287 | + |
| 288 | + # Test if columns input is not list |
| 289 | + with raises(TypeError): |
| 290 | + eda_utils_py.outlier_identifier(test_df, columns=2) |
| 291 | + |
| 292 | + # Test if input column list is in the dataframe |
| 293 | + with raises(Exception): |
| 294 | + eda_utils_py.outlier_identifier(test_df, columns=["not in"]) |
| 295 | + |
| 296 | + # Test if method input is not one of three methods provided |
| 297 | + with raises(Exception): |
| 298 | + eda_utils_py.outlier_identifier(test_df, columns=["SepalLengthCm"], method = "no") |
| 299 | + |
| 300 | + # Test if column selected included non-numeric columns |
| 301 | + with raises(Exception): |
| 302 | + eda_utils_py.outlier_identifier(test_df, columns=["Species"]) |
| 303 | + |
| 304 | + assert pd.DataFrame.equals( |
| 305 | + eda_utils_py.outlier_identifier(test_df, test_column), trim_output |
| 306 | + ), "Default test not pass" |
| 307 | + assert pd.DataFrame.equals( |
| 308 | + eda_utils_py.outlier_identifier(test_df, test_column,method = "median"), median_output |
| 309 | + ), "The median method is not correct" |
| 310 | + assert pd.DataFrame.equals( |
| 311 | + eda_utils_py.outlier_identifier(test_df, test_column, method = "mean"), mean_output |
| 312 | + ), "The mean method is not correct" |
| 313 | + assert pd.DataFrame.equals( |
| 314 | + eda_utils_py.outlier_identifier(test_df, columns = ["SepalLengthCm"], method = "mean"), column_output |
| 315 | + ), "The selected column method is not correct" |
| 316 | + |
0 commit comments