Quality Assessments Methods
A class for performing quality assessment functions on social media advertising data.
Examples:
- Check the percentage of null or nan values for certain channels of data, e.g. Germany TikTok has 5% of the post messages as null values
- Store the historic sum totals of columns such as likes, which should increase linerarly over time as more adverts are run. If a sharp increase or a decrease is detected then raise an error.
- Check the percentage of duplicates in a dataframe of advertising data. Automatic detection of whether the data is paid or organic and what attributes of those datasets would normally constitute a duplicate or not.
It outputs results to Google Sheets and can raise exceptions or send notifications when issues are detected.
Attributes:
| Name | Type | Description |
|---|---|---|
util |
A utility object (from utility_functions in this library) for writing results to google sheets and databases. |
Source code in veetility/quality_assessments.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
boosted_function_qa(paid_df, organic_df, gsheet_name, tab_name='OrganicWithBigImpressions', impressions_threshold=100000)
Takes in organic data and paid data and reports how many paid adverts are mislabelled as boostes and how many organic posts are incorrectly said to not have been boosted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paid_df |
pandas.DataFrame
|
Daily ad spend, boosted posts identified in the 'workstream' column |
required |
organic_df |
pandas.DataFrame
|
Organic Post performance Data |
required |
impressions_threshold |
pandas.DataFrame
|
The number of impressions above which it is unlikely the post is purely organic |
100000
|
Returns:
| Name | Type | Description |
|---|---|---|
error_message |
str
|
A string detailing what the error is so that it can be passed to a notification service like slack |
Source code in veetility/quality_assessments.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
check_data_recency(df, cols_to_group, gsheet_name, tab_name='DataRecency', three_days_for_monday=True, date_col='date', dayfirst='EnterValue', yearfirst='EnterValue', format=None, errors='raise')
Post a google sheet showing how many days since different channels have been active. Also return a list of channels that have been inactive for more than 2 days which might be indicative of an error
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
pandas.DataFrame
|
Dataframe of data containing a 'date' column |
required |
cols_to_group |
list or str
|
Columns to groupby effectively creating the 'channels' |
required |
gsheet_name |
str
|
Name of the google sheet to write to |
required |
tab_name |
str
|
Name of the tab in the Google sheet |
'DataRecency'
|
three_days_for_monday |
bool
|
If the check is run on a Monday, give 3 days before declaring a channel as inactive because of the weekend. |
True
|
date_col |
str
|
Column name for the date to find the maximum value for based on grouping by |
'date'
|
dayfirst |
bool
|
If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. If False, parses dates with the month first, eg 10/11/12 is parsed as 2010-11-12. If None, this is set to True if the day is in the first position in the format string, False otherwise. If dayfirst is set to True, parsing will be faster, but will fail for ambiguous dates, such as 01/02/03. |
'EnterValue'
|
yearfirst |
bool
|
If True parses dates with the year first, eg 10/11/12 is parsed as 2010-11-12. If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil). If False, parses dates with the month first, eg 10/11/12 is parsed as 2012-11-10. If None, this defaults to False. Setting yearfirst to True is not recommended, as it can result in ambiguous dates. |
'EnterValue'
|
format |
str
|
Format to use for strptime. If None, the format is inferred from the first non-NaN element of the column. If the format is inferred, it will be used in subsequent parsing, even if the format changes. To specify a format string that will be used in parsing regardless of the inferred format, use pd.to_datetime with format. |
None
|
errors |
str
|
If 'raise', then invalid parsing will raise an exception. If 'coerce', then invalid parsing will be set as NaT. If 'ignore', then invalid parsing will return the input. |
'raise'
|
Returns:
| Name | Type | Description |
|---|---|---|
error_message |
str
|
String error message describing which channels are recently inactive. This message can then be sent to a slack function |
Source code in veetility/quality_assessments.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
check_impressions_no_engagements(df, gsheet_name, tab_name='NoImpressionsButEngagements', raise_exceptions=False)
Function to check if a row item has engagements but no impressions and no video views. This shouldn't happen and is indicative of an error with Tracer but can be valid as some platforms count viral engagements differently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
Input dataframe of advertising data with columns 'impressions' or 'video_views' |
required |
gsheet_name |
str
|
name of the google sheet |
required |
tab_name |
str
|
name of the tab. Defaults to 'NoImpressionsButEngagements'. |
'NoImpressionsButEngagements'
|
raise_exceptions |
bool
|
Boolean flag if set to true will raise an exception if an offending row item is discovered. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
error_message |
str
|
String detailing what the error is so that it can be passed to a notification service like slack. |
Source code in veetility/quality_assessments.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | |
comparison_with_previous_data(df, name_of_df, cols_to_check=None, perc_increase_threshold=20, perc_decrease_threshold=0.5, check_cols_set=True, unique_id_cols=None, cols_to_group=None, raise_exceptions=True, manual_override=False, date_col='date', dayfirst=True, yearfirst=False)
This function allows you to compare the column totals of a dataframe with the totals calculated on a previous time to detect any changes that could be indicative of an error.
The historic column totals are stored in a datatable for reference and the function will check the current totals with the most recent previous totals and raise an exception if the totals have changed by more than the specified thresholds.
The function will also check if the columns in the dataframe have changed from the previous time and raise an exception if they have.
If the previous totals were wrong because of an error and the latest values in the dataframe are correct then you can set manual_override to True and the function will add a new row to the historic db with the new values. This manual override can only be done once in a row to stop someone forgetting they put manual override on and leaving it running in the script which would mean the function would never pick up any errors
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
pd.DataFrame
|
Input dataframe that the historic checks are going to be performed on. |
required |
name_of_df |
str
|
Name of the dataframe, this will be used to name a file to save for future comparison. |
required |
cols_to_check |
List[str]
|
A list of strings that detail the columns to be totaled which will then be compared with previous data. |
None
|
perc_increase_threshold |
float
|
The percentage increase threshold above which it is deemed that the totals have raised too rapidly and an error has occured. |
20
|
perc_decrease_threshold |
float
|
The percentage decrease threshold below which it is deemed that the totals decreased and an error has occured. |
0.5
|
check_cols_set |
bool
|
If true, store the set of columns present for comparison to see if any new columns have been added or removed next time, in which case it is deemed an error has occured. |
True
|
unique_id_cols |
List[str]
|
A list of the columns that are unique identifiers in order to do a unique ID count to help identify any cause of the change in totals of the cols_to_check. |
None
|
cols_to_group |
List[str]
|
A list the columns that are to be grouped by, and the cols_to_check will be summed for each group. This will be stored as a string in the data table which can be used as a reference to see which groups have caused the change in totals. |
None
|
raise_exceptions |
bool
|
If true, then raise an exception if an error has occured instead of just returning an error message. smanual_override (bool): If true, then add a new row to the historic db with the new values with the new value which is outside the tolerance bounds but is now not considered an error |
True
|
date_col |
str
|
The name of the date column in the dataframe |
'date'
|
dayfirst |
bool
|
If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10. If False, parses dates with the month first, eg 10/11/12 is parsed as 2010-11-12. If None, this is set to True if the day is in the first position in the format string, False otherwise. If dayfirst is set to True, parsing will be faster, but will fail for ambiguous dates, such as 01/02/03. |
True
|
yearfirst |
bool
|
If True parses dates with the year first, eg 10/11/12 is parsed as 2010-11-12. If both dayfirst and yearfirst are True, yearfirst is preceded (same as dateutil). If False, parses dates with the month first, eg 10/11/12 is parsed as 2012-11-10. If None, this defaults to False. Setting yearfirst to True is not recommended, as it can result in ambiguous dates. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
error_message |
str
|
String detailing what the error is so that it can be passed to a notification service like slack. |
Raises:
| Type | Description |
|---|---|
Exception
|
If raise_exceptions = True and an error has occured. |
Source code in veetility/quality_assessments.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
duplicates_qa(df, name_of_df, perc_dupes_thresh=3, cols_to_check=None, cols_to_add=None, return_type='duplicates', raise_exceptions=True)
Checks for duplicates in a dataframe and returns the duplicates or the dataframe without duplicates.
The function first checks to see whether the input dataframe is paid or organic data. If it is paid data then the columns to check for duplicates are ['date','platform','country','media_type','cohort','message','ad_name','spend'].
For organic data the standard columns it will check for are ['platform', 'country','media_type' ,'message','url']
You can specify other columns to check for duplicates by passing a list to the cols_to_check argument. You can also add to the standard columns by passing a list to the cols_to_add argument.
You can specify whether to return the original df, the df with duplicates removed or just the duplicates or nothing by passing 'original', 'duplicates' or 'duplicates_removed' or 'nothing' to the return_type argument.
If the return type is duplicates_removed, an error message will also be returned, with the error message being blank if no duplicates are found. This can be passed to a notification function for example.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
pd.DataFrame
|
The Dataframe to be checked for duplicates |
required |
name_of_df |
str
|
The name of the dataframe to be used for logging purposes |
required |
perc_dupes_thresh |
int
|
The percentage of duplicates that are allowed before an error is raised. Defaults to 3. |
3
|
cols_to_check |
Optional[list]
|
The columns to check for duplicates. Defaults to None. |
None
|
cols_to_add |
Optional[list]
|
The columns to add to the standard cols_to_check to duplicates check. Defaults to None. |
None
|
return_type |
str
|
The type of return. Either 'duplicates' or 'duplicates_removed'. Defaults to 'duplicates'. |
'duplicates'
|
raise_exceptions |
bool
|
If true raise an exception if duplicates are found. Defaults to True. |
True
|
Raises:
| Type | Description |
|---|---|
Exception
|
If raise_exceptions = True and duplicates are found |
Returns:
| Type | Description |
|---|---|
pd.DataFrame: Returns the duplicates,the df without duplicates or the original df depending on the return_type |
Source code in veetility/quality_assessments.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
naming_convention_checker(df, gsheet_name, naming_convention, campaignname_dict=None, adgroupname_dict=None, adname_dict=None, campaign_col='campaign_name', adgroup_col='group_name', adname_col='ad_name', spend_col='spend_usd', start_char='_', middle_char=':', end_char='_', platform_col='platform', check_meta_platform=True)
Checks for naming convention errors in a given DataFrame and outputs the errors to a Google Sheet.
The function takes in a DataFrame containing paid data with columns for campaign name, ad group name, and ad name, as well as a DataFrame containing the accepted tags and values for each level of naming (campaign, ad group, ad name). Dictionarys for each level fo checking are required this specifies what tags should be checked for, e.g. should a check for the correct values in 'platform' be performed. If a dictionary for a certain level is not provided that level will not be checked. The function will output a table in a Google Sheet showing all errors in naming conventions, with a tab for each level of errors.
The Google Sheet should be set up with a tab name for each level of errors to check for, i.e. 'CampaignNameErrors', 'AdGroupNameErrors', 'AdNameErrors'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The DataFrame containing paid data with campaign name, ad group name, and ad name columns. |
required |
naming_convention |
DataFrame
|
The DataFrame containing the accepted tags and values for each level of naming. |
required |
campaignname_dict |
dict
|
A dictionary of the tags to be checked for the campaign name, with the column label as the key and the shortcode as the value. |
None
|
adgroupname_dict |
dict
|
A dictionary of the tags to be checked for the ad group name, with the column label as the key and the shortcode as the value. |
None
|
adname_dict |
dict
|
A dictionary of the tags to be checked for the ad name, with the column label as the key and the shortcode as the value. |
None
|
campaign_col |
str
|
The column in the input DataFrame that corresponds to the campaign name. Defaults to 'campaign_name'. |
'campaign_name'
|
adgroup_col |
str
|
The column in the input DataFrame that corresponds to the ad group name. Defaults to 'group_name'. |
'group_name'
|
adname_col |
str
|
The column in the input DataFrame that corresponds to the ad name. Defaults to 'name'. |
'ad_name'
|
start_char |
str
|
The starting character for the tag in the naming convention. Defaults to '_'. |
'_'
|
middle_char |
str
|
The character that separates the tag from the value in the naming convention. Defaults to ':'. |
':'
|
end_char |
str
|
The ending character for the tag in the naming convention. Defaults to '_'. |
'_'
|
Returns:
| Type | Description |
|---|---|
Writes to a Google Sheet a table with the index being a unique instance of the campaign. |
Source code in veetility/quality_assessments.py
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
null_values_checker(df, cols_to_group, gsheet_name, tab_name, cols_to_ignore=None, null_definitions=None, output_method='gsheet')
Takes in a dataframe and columns to groupby and checks how many null values or null equivalents there are in the rest of the columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
pandas.DataFrame
|
The Input Dataframe of any type where you want to check for nulls. |
required |
cols_to_group |
list of str
|
The list of columns to perform a groupby operation with the null percentage counts will be a percentage of nulls in these groupbys. |
required |
cols_to_ignore |
list of str
|
The list of columns to ignore when checking for nulls. Default is None. |
None
|
gsheet_name |
str
|
The name of the google sheet workbook to pass to the google sheet function. |
required |
tab_name |
str
|
The name of the tab in the google sheet workbook to pass to the google sheet function. The google sheet must be setup with this tab already created. |
required |
null_definitions |
list
|
A list containing elements to be defined as a null value. |
None
|
output_method |
str
|
A string identifying whether the output is to be sent to a Google sheet ('gsheet') or returned as a dataframe ('Dataframe'). |
'gsheet'
|
Returns:
| Name | Type | Description |
|---|---|---|
null_count_df |
pandas.DataFrame
|
Dataframe showing the percentage of nulls in each column grouped by the 'cols_to_group'. |
Source code in veetility/quality_assessments.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |