Script 1439: Pacing New CPM Budget Model
Purpose
The Python script calculates and manages pacing metrics for digital advertising campaigns, ensuring they meet their budget and impression goals efficiently.
To Elaborate
The script is designed to manage and optimize the pacing of digital advertising campaigns by calculating various metrics related to budget allocation and impression delivery. It processes campaign data to determine the start and end dates of pacing cycles, calculates the total number of days for each campaign, and assesses the number of days elapsed and remaining. The script also computes daily targets and expected values to date, evaluates the pacing status, and determines whether campaigns are on track, under-delivering, or over-delivering. Additionally, it calculates the recommended daily budget and other metrics such as impressions to serve today and the cost per thousand impressions (CPM) over the last three days. The script is intended to help advertisers ensure their campaigns are delivered within budget constraints while achieving the desired reach and engagement.
Walking Through the Code
- Initialization and Setup
- The script begins by determining whether it is running on a server or locally. If running locally, it loads campaign data from a pickle file.
- It sets up necessary imports and configurations, including timezone settings and pandas options for data display.
- Data Preparation
- The script cleans and prepares the input data by converting monetary values to numeric types and date columns to datetime objects.
- It initializes columns for calculations, such as pacing cycle start and end dates, total days, and daily targets.
- Pacing Cycle Calculations
- Functions are defined to calculate the start and end dates of the auto pacing cycle based on campaign settings and current date.
- The script calculates the total number of days for each campaign and the number of days elapsed and remaining in the pacing cycle.
- Metric Calculations
- The script computes various metrics, including daily targets, expected values to date, and pacing percentages.
- It evaluates the pacing status to determine if campaigns are on track, under-pacing, or over-pacing.
- Final Calculations and Output
- Additional metrics such as impressions to serve today, 3-day CPM, and recommended daily budget are calculated.
- The script aggregates data to provide a summary of campaign performance and writes the output to a CSV file if running locally.
Vitals
- Script ID : 1439
- Client ID / Customer ID: 1306927171 / 60270139
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, 3 Day CPM, Auto. Pacing Cycle Clicks, Auto. Pacing Cycle Days, Auto. Pacing Cycle Days Elapsed, Auto. Pacing Cycle Days Remaining, Auto. Pacing Cycle End Date, Auto. Pacing Cycle Expected to Date, Auto. Pacing Cycle Impr., Auto. Pacing Cycle Pacing, Auto. Pacing Cycle Start Date, Auto. Pacing Cycle Pub. Cost, Auto. Pacing Cycle Threshold, Auto. Pacing Cycle Views, Delivery Status, Impressions Last 3 Days, Impressions to Serve Today, Installments, Last 3 Days End, Last 3 Days Start, Pacing Calculation Date, Pub. Cost Last 3 Days, Recommended Daily Budget, Todays Date, Total Clicks, Total Daily Target, Total Days, Total Days Elapsed, Total Expected to Date, Total Impressions, Total Pacing, Total Pub Cost, Total Target (Spend/Impr./Views), Total Views, Underserved Impressions, Pacing Cycle Daily Allocation (Spend/Impr./Views)
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jesus Garza (jgarza@marinsoftware.com)
- Created by Jesus Garza on 2024-10-11 14:27
- Last Updated by ascott@marinsoftware.com on 2024-11-22 20:56
> See it in Action
Python Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
## Infinite Digital Pacing Script
## name: New Campaign Calculations
## description:
##
##
## author: Jesus A. Garza, Michael S. Huang
## created: 2024-02-06
##
########### START - Local Mode Config ###########
# Step 1: Uncomment download_preview_input flag and run Preview successfully with the Datasources you want
download_preview_input=False
# Step 2: In MarinOne, go to Scripts -> Preview -> Logs, download 'dataSourceDict' pickle file, and update pickle_path below
# pickle_path = ''
pickle_path = '/Users/mhuang/Downloads/pickle/mary_beth_pacing_new_calc_20240905.pkl'
# Step 3: Copy this script into local IDE with Python virtual env loaded with pandas and numpy.
# Step 4: Run locally with below code to init dataSourceDict
# determine if code is running on server or locally
def is_executing_on_server():
try:
# Attempt to access a known restricted builtin
dict_items = dataSourceDict.items()
return True
except NameError:
# NameError: dataSourceDict object is missing (indicating not on server)
return False
local_dev = False
if is_executing_on_server():
print("Code is executing on server. Skip init.")
elif len(pickle_path) > 3:
print("Code is NOT executing on server. Doing init.")
local_dev = True
# load dataSourceDict via pickled file
import pickle
dataSourceDict = pickle.load(open(pickle_path, 'rb'))
# print shape and first 5 rows for each entry in dataSourceDict
for key, value in dataSourceDict.items():
print(f"Shape of dataSourceDict[{key}]: {value.shape}")
# print(f"First 5 rows of dataSourceDict[{key}]:\n{value.head(5)}")
# set outputDf same as inputDf
inputDf = dataSourceDict["1"]
outputDf = inputDf.copy()
# setup timezone
from datetime import timedelta
import datetime, timedelta
# Chicago Timezone is GMT-5. Adjust as needed.
CLIENT_TIMEZONE = datetime.timezone(datetime.timedelta(hours=-5))
# import pandas
import pandas as pd
import numpy as np
# other imports
import re
import urllib
# import Marin util functions
# from marin_scripts_utils import tableize, select_changed
# pandas settings
pd.set_option('display.max_columns', None) # Display all columns
pd.set_option('display.max_colwidth', None) # Display full content of each column
else:
print("Running locally but no pickle path defined. dataSourceDict not loaded.")
exit(1)
########### END - Local Mode Setup ###########
today = datetime.datetime.now(CLIENT_TIMEZONE).date()
# primary data source and columns
inputDf = dataSourceDict["1"]
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_DATE = 'Date'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_BRAND = 'Brand'
RPT_COL_GOAL = 'Goal'
RPT_COL_PACING__START_DATE = 'Pacing - Start Date'
RPT_COL_PACING__END_DATE = 'Pacing - End Date'
RPT_COL_TARGET_IMPR_PER_SPENDVIEWS = 'Target (Impr/Spend/Views)'
RPT_COL_CPM_RESTRAINT = 'CPM Restraint'
RPT_COL_CPC_RESTRAINT = 'CPC Restraint'
RPT_COL_CAMPAIGN_TYPE = 'Campaign Type'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_SBA_TRAFFIC = 'SBA Traffic'
RPT_COL_TOTAL_PUB_COST = 'Total Pub Cost'
RPT_COL_TOTAL_IMPRESSIONS = 'Total Impressions'
RPT_COL_TOTAL_CLICKS = 'Total Clicks'
RPT_COL_TOTAL_VIEWS = 'Total Views'
RPT_COL_PACING_CYCLE_PUB_COST = 'Pacing Cycle Pub Cost'
RPT_COL_PACING_CYCLE_IMPRESSIONS = 'Pacing Cycle Impressions'
RPT_COL_PACING_CYCLE_CLICKS = 'Pacing Cycle Month Clicks'
RPT_COL_PACING_CYCLE_VIEWS = 'Pacing Cycle Views'
RPT_COL_AUTO_PACING_CYCLE_START_DATE = 'Auto. Pacing Cycle Start Date'
RPT_COL_AUTO_PACING_CYCLE_END_DATE = 'Auto. Pacing Cycle End Date'
RPT_COL_TODAYS_DATE = 'Todays Date'
RPT_COL_AUTO_PACING_CYCLE_DAYS = 'Auto. Pacing Cycle Days'
RPT_COL_AUTO_PACING_CYCLE_DAYS_ELAPSED = 'Auto. Pacing Cycle Days Elapsed'
RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING = 'Auto. Pacing Cycle Days Remaining'
RPT_COL_AUTO_PACING_CYCLE_TARGET_REMAINING_SPEND_PER_IMPRVIEWS = 'Auto. Pacing Cycle Target Remaining (Spend/Impr./Views)'
RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE = 'Auto. Pacing Cycle Expected to Date'
RPT_COL_AUTO_PACING_CYCLE_PACING = 'Auto. Pacing Cycle Pacing'
RPT_COL_AUTO_PACING_CYCLE_THRESHOLD = 'Auto. Pacing Cycle Threshold'
RPT_COL_AUTO_PACING_CYCLE_CLICKS = 'Auto. Pacing Cycle Clicks'
RPT_COL_AUTO_PACING_CYCLE_IMPR = 'Auto. Pacing Cycle Impr.'
RPT_COL_AUTO_PACING_CYCLE_PUB_COST = 'Auto. Pacing Cycle Pub. Cost'
RPT_COL_AUTO_PACING_CYCLE_VIEWS = 'Auto. Pacing Cycle Views'
RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS = 'Total Target (Spend/Impr./Views)'
RPT_COL_TOTAL_DAYS = 'Total Days'
RPT_COL_TOTAL_DAYS_ELAPSED = 'Total Days Elapsed'
RPT_COL_TOTAL_DAILY_TARGET = 'Total Daily Target'
RPT_COL_TOTAL_EXPECTED_TO_DATE = 'Total Expected to Date'
RPT_COL_TOTAL_PACING = 'Total Pacing'
RPT_COL_DELIVERY_STATUS = 'Delivery Status'
RPT_COL_AUTO_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPRVIEWS = 'Auto. Pacing Cycle Daily Allocation (Spend/Impr./Views)'
RPT_COL_ADJ_REC_CAMPAIGN_BUDGET = 'Adj. Rec. Campaign Budget'
RPT_COL_IMPR = 'Impr.'
RPT_COL_CLICKS = 'Clicks'
RPT_COL_PUB_COST = 'Pub. Cost $'
RPT_COL_VIDEO_VIEWS = 'Video Views'
RPT_COL_INSTALLMENTS = 'Installments'
RPT_COL_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPR_VIEWS = 'Pacing Cycle Daily Allocation (Spend/Impr./Views)'
RPT_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS = 'Auto. Pacing Daily Target (Spend/Impressions/Views)'
RPT_COL_AUTO_PACING_CYCLE_IMPR = 'Auto. Pacing Cycle Impr.'
RPT_COL_LAST_3_DAYS_START = 'Last 3 Days Start'
RPT_COL_LAST_3_DAYS_END = 'Last 3 Days End'
RPT_COL_CAMPAIGN_ID = "Campaign ID"
RPT_COL_PUB_COST_LAST_3_DAYS = 'Pub. Cost Last 3 Days'
RPT_COL_IMPRESSIONS_LAST_3_DAYS = 'Impressions Last 3 Days'
RPT_COL_3_DAY_CPM = '3 Day CPM'
RPT_COL_UNDERSERVED_IMPRESSIONS = 'Underserved Impressions'
RPT_COL_IMPRESSIONS_TO_SERVE_TODAY = 'Impressions to Serve Today'
RPT_COL_RECOMMENDED_DAILY_BUDGET = 'Recommended Daily Budget'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_3_DAY_CPM = '3 Day CPM'
BULK_COL_AUTO_PACING_CYCLE_CLICKS = 'Auto. Pacing Cycle Clicks'
BULK_COL_AUTO_PACING_CYCLE_DAYS = 'Auto. Pacing Cycle Days'
BULK_COL_AUTO_PACING_CYCLE_DAYS_ELAPSED = 'Auto. Pacing Cycle Days Elapsed'
BULK_COL_AUTO_PACING_CYCLE_DAYS_REMAINING = 'Auto. Pacing Cycle Days Remaining'
BULK_COL_AUTO_PACING_CYCLE_END_DATE = 'Auto. Pacing Cycle End Date'
BULK_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE = 'Auto. Pacing Cycle Expected to Date'
BULK_COL_AUTO_PACING_CYCLE_IMPR = 'Auto. Pacing Cycle Impr.'
BULK_COL_AUTO_PACING_CYCLE_PACING = 'Auto. Pacing Cycle Pacing'
BULK_COL_AUTO_PACING_CYCLE_PUB_COST = 'Auto. Pacing Cycle Pub. Cost'
BULK_COL_AUTO_PACING_CYCLE_START_DATE = 'Auto. Pacing Cycle Start Date'
BULK_COL_AUTO_PACING_CYCLE_THRESHOLD = 'Auto. Pacing Cycle Threshold'
BULK_COL_AUTO_PACING_CYCLE_VIEWS = 'Auto. Pacing Cycle Views'
BULK_COL_DELIVERY_STATUS = 'Delivery Status'
BULK_COL_IMPRESSIONS_LAST_3_DAYS = 'Impressions Last 3 Days'
BULK_COL_IMPRESSIONS_TO_SERVE_TODAY = 'Impressions to Serve Today'
BULK_COL_INSTALLMENTS = 'Installments'
BULK_COL_LAST_3_DAYS_END = 'Last 3 Days End'
BULK_COL_LAST_3_DAYS_START = 'Last 3 Days Start'
BULK_COL_PACING_CALCULATION_DATE = 'Pacing Calculation Date'
BULK_COL_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPRVIEWS = 'Pacing Cycle Daily Allocation (Spend/Impr./Views)'
BULK_COL_PUB_COST_LAST_3_DAYS = 'Pub. Cost Last 3 Days'
BULK_COL_RECOMMENDED_DAILY_BUDGET = 'Recommended Daily Budget'
BULK_COL_TODAYS_DATE = 'Todays Date'
BULK_COL_TOTAL_CLICKS = 'Total Clicks'
BULK_COL_TOTAL_DAILY_TARGET = 'Total Daily Target'
BULK_COL_TOTAL_DAYS = 'Total Days'
BULK_COL_TOTAL_DAYS_ELAPSED = 'Total Days Elapsed'
BULK_COL_TOTAL_EXPECTED_TO_DATE = 'Total Expected to Date'
BULK_COL_TOTAL_IMPRESSIONS = 'Total Impressions'
BULK_COL_TOTAL_PACING = 'Total Pacing'
BULK_COL_TOTAL_PUB_COST = 'Total Pub Cost'
BULK_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS = 'Total Target (Spend/Impr./Views)'
BULK_COL_TOTAL_VIEWS = 'Total Views'
BULK_COL_UNDERSERVED_IMPRESSIONS = 'Underserved Impressions'
print("inputDf.shape", inputDf.shape)
print("inputDf.dtypes\n", inputDf.dtypes)
### Data Cleanup and Preliminaries
# Strip dollar signs and commas, then convert to numeric
spendviews = inputDf[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS].astype(str).str.replace('$', '').str.replace(',', '')
inputDf[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] = pd.to_numeric(spendviews, errors='coerce')
# Convert 'Pacing - Start Date' and 'Pacing - End Date' columns to datetime
inputDf[RPT_COL_PACING__START_DATE] = pd.to_datetime(inputDf[RPT_COL_PACING__START_DATE])
inputDf[RPT_COL_PACING__END_DATE] = pd.to_datetime(inputDf[RPT_COL_PACING__END_DATE])
inputDf[RPT_COL_DATE] = pd.to_datetime(inputDf[RPT_COL_DATE])
# Today
inputDf[RPT_COL_TODAYS_DATE] = pd.to_datetime(today)
# Pacing Calculation Date
inputDf[BULK_COL_PACING_CALCULATION_DATE] = pd.to_datetime(today)
##Auto. Pacing Cycle Start Date
##Formula: =IF(A2<>"",IF(AND(YEAR(E2)=YEAR(Y2),MONTH(Y2)=MONTH(E2)),E2,IF(Y2>F2,"",IF(AH2<=31,E2,EOMONTH(Y2,-1)+1))),"")
##Python
def calculate_auto_pacing_cycle_start_date(row):
if pd.notna(row['Campaign']):
if row['Installments'] == 'Manual':
return row['Auto. Pacing Cycle Start Date']
elif row['Installments'] == 'Auto':
if row['Todays Date'] > row['Pacing - End Date']:
return ""
elif (row['Pacing - Start Date'].year == row['Todays Date'].year and
row['Pacing - Start Date'].month == row['Todays Date'].month):
return row['Pacing - Start Date']
elif row['Total Days'] <= 31:
return row['Pacing - Start Date']
else:
return (row['Todays Date'] - pd.offsets.MonthBegin(1)).strftime('%Y-%m-%d')
elif row['Installments'] == 'Off' or row['Installments'] == "":
return row['Pacing - Start Date']
else:
return row['Pacing - Start Date']
else:
return ""
# Apply the function to each row of the DataFrame
inputDf[RPT_COL_AUTO_PACING_CYCLE_START_DATE] = inputDf.apply(calculate_auto_pacing_cycle_start_date, axis=1)
inputDf[RPT_COL_AUTO_PACING_CYCLE_START_DATE] = pd.to_datetime(inputDf[RPT_COL_AUTO_PACING_CYCLE_START_DATE], errors='coerce')
##Auto. Pacing Cycle End Date
##Formula: =IF(A2<>"",IF(Y2>F2,"",IF(AND(YEAR(F2)=YEAR(Y2),MONTH(Y2)=MONTH(F2)),F2,IF(AH2<=31,E2,EOMONTH(Y2,0)))),"")
##Python
def calculate_auto_pacing_cycle_end_date(row):
if pd.notna(row['Campaign']):
if row['Installments'] == 'Manual':
return row['Auto. Pacing Cycle End Date']
elif row['Installments'] == 'Auto':
if row['Todays Date'] > row['Pacing - End Date']:
return ""
elif (row['Pacing - End Date'].year == row['Todays Date'].year and
row['Pacing - End Date'].month == row['Todays Date'].month):
return row['Pacing - End Date']
elif row['Total Days'] <= 31:
return row['Pacing - End Date']
else:
return (row['Todays Date'] + pd.offsets.MonthEnd(0)).strftime('%Y-%m-%d')
elif row['Installments'] == 'Off' or row['Installments'] == "":
return row['Pacing - End Date']
else:
return row['Pacing - End Date']
else:
return ""
# Apply the function to each row of the DataFrame
inputDf[RPT_COL_AUTO_PACING_CYCLE_END_DATE] = inputDf.apply(calculate_auto_pacing_cycle_end_date, axis=1)
inputDf[RPT_COL_AUTO_PACING_CYCLE_END_DATE] = pd.to_datetime(inputDf[RPT_COL_AUTO_PACING_CYCLE_END_DATE], errors='coerce')
##Total Days
##Formula: =if(A2<>"",IF(F2-E2+1<1,"",F2-E2+1),"")
##Python
def calculate_total_days(row):
if pd.notnull(row[RPT_COL_CAMPAIGN]):
# Calculate the difference in days, then add 1
days_diff = (row[RPT_COL_PACING__END_DATE] - row[RPT_COL_PACING__START_DATE]).days + 1
# Check if the calculated difference is less than 1
if days_diff < 1:
return 0
else:
return days_diff
else:
return 0
# Apply the function to each row in the DataFrame
inputDf[RPT_COL_TOTAL_DAYS] = inputDf.apply(calculate_total_days, axis=1)
#### calculate metric totals for Pacing and Auto Pacing date ranges
## Make copies of metric values before zeroing out according to date range
# List of columns to be assigned the same value
columns_to_assign = [
[RPT_COL_TOTAL_IMPRESSIONS, RPT_COL_PACING_CYCLE_IMPRESSIONS, RPT_COL_AUTO_PACING_CYCLE_IMPR],
[RPT_COL_TOTAL_CLICKS, RPT_COL_PACING_CYCLE_CLICKS, RPT_COL_AUTO_PACING_CYCLE_CLICKS],
[RPT_COL_TOTAL_PUB_COST, RPT_COL_PACING_CYCLE_PUB_COST, RPT_COL_AUTO_PACING_CYCLE_PUB_COST],
[RPT_COL_TOTAL_VIEWS, RPT_COL_PACING_CYCLE_VIEWS, RPT_COL_AUTO_PACING_CYCLE_VIEWS]
]
# Corresponding values to assign
values_to_assign = [RPT_COL_IMPR, RPT_COL_CLICKS, RPT_COL_PUB_COST, RPT_COL_VIDEO_VIEWS]
# Assign values to columns
for cols, value in zip(columns_to_assign, values_to_assign):
for col in cols:
inputDf[col] = inputDf[value]
## Zero out values if Date not within PACING_START/END or AUTO_PACING_START/END
def zero_out_values(row):
if not (row[RPT_COL_PACING__START_DATE] <= row[RPT_COL_DATE] <= row[RPT_COL_PACING__END_DATE]):
row[RPT_COL_PACING_CYCLE_IMPRESSIONS] = 0
row[RPT_COL_PACING_CYCLE_CLICKS] = 0
row[RPT_COL_PACING_CYCLE_PUB_COST] = 0
row[RPT_COL_PACING_CYCLE_VIEWS] = 0
if not (row[RPT_COL_AUTO_PACING_CYCLE_START_DATE] <= row[RPT_COL_DATE] <= row[RPT_COL_AUTO_PACING_CYCLE_END_DATE]):
row[RPT_COL_AUTO_PACING_CYCLE_IMPR] = 0
row[RPT_COL_AUTO_PACING_CYCLE_CLICKS] = 0
row[RPT_COL_AUTO_PACING_CYCLE_PUB_COST] = 0
row[RPT_COL_AUTO_PACING_CYCLE_VIEWS] = 0
return row
# Apply the zero_out_values function to each row
inputDf = inputDf.apply(zero_out_values, axis=1)
# Now agg to get subtotal for each date range
agg_dict = {
RPT_COL_TOTAL_IMPRESSIONS: 'sum',
RPT_COL_TOTAL_CLICKS: 'sum',
RPT_COL_TOTAL_PUB_COST: 'sum',
RPT_COL_TOTAL_VIEWS: 'sum',
RPT_COL_PACING_CYCLE_IMPRESSIONS: 'sum',
RPT_COL_PACING_CYCLE_CLICKS: 'sum',
RPT_COL_PACING_CYCLE_PUB_COST: 'sum',
RPT_COL_PACING_CYCLE_VIEWS: 'sum',
RPT_COL_AUTO_PACING_CYCLE_PUB_COST: 'sum',
RPT_COL_AUTO_PACING_CYCLE_IMPR: 'sum',
RPT_COL_AUTO_PACING_CYCLE_CLICKS: 'sum',
RPT_COL_AUTO_PACING_CYCLE_VIEWS: 'sum',
}
# Keep the last value for all columns in inputDf that do not appear in agg_dict, excluding RPT_COL_DATE, RPT_COL_ACCOUNT, and RPT_COL_CAMPAIGN
for col in inputDf.columns:
if col not in agg_dict and col not in [RPT_COL_DATE, RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN]:
agg_dict[col] = 'last'
debugDf = inputDf
df_campaign_with_totals = inputDf.groupby([RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN]).agg(agg_dict).reset_index()
# Adding RPT_COL_GOAL as 'Goal'
#RPT_COL_GOAL = 'Goal'
#df_campaign_with_totals[RPT_COL_GOAL] = 'Goal'
# Round the necessary columns to 2 decimal places
columns_to_round = [
RPT_COL_TOTAL_PUB_COST,
RPT_COL_PACING_CYCLE_PUB_COST,
RPT_COL_AUTO_PACING_CYCLE_PUB_COST # <- Auto Pacing Cycle Pub. Cost included
]
df_campaign_with_totals[columns_to_round] = df_campaign_with_totals[columns_to_round].round(2)
# Debugging output
print("df_campaign_with_totals.shape", df_campaign_with_totals.shape)
print("df_campaign_with_totals.dtypes\n", df_campaign_with_totals.dtypes)
print("df_campaign_with_totals.shape", df_campaign_with_totals.shape)
print("df_campaign_with_totals.dtypes\n", df_campaign_with_totals.dtypes)
##Todays Date
##Formula: =if(A2<>"",today(),"")
##Python:
#today = datetime.datetime.now(CLIENT_TIMEZONE).date()
##Auto. Pacing Cycle Days
##Formula: =if(A2<>"",if(X2="","",if(AH2<=31,AH2,(X2-W2)+1)),"")
##Python:
def calculate_apc_days(row):
if pd.notnull(row[RPT_COL_CAMPAIGN]) and pd.notnull(row[RPT_COL_AUTO_PACING_CYCLE_START_DATE]) and pd.notnull(row[RPT_COL_AUTO_PACING_CYCLE_END_DATE]):
if row[RPT_COL_TOTAL_DAYS] <= 31:
return row[RPT_COL_TOTAL_DAYS]
else:
# Ensure both dates are timezone-naive before subtracting
start_date = row[RPT_COL_AUTO_PACING_CYCLE_START_DATE]
end_date = row[RPT_COL_AUTO_PACING_CYCLE_END_DATE]
return (end_date - start_date).days + 1
else:
return ""
# Apply the function row-wise
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_DAYS] = df_campaign_with_totals.apply(calculate_apc_days, axis=1)
##Auto. Pacing Cycle Days Elapsed
##Formula: =IF(A2<>"",IF(X2="","",IF(Y2-W2<=0,0,Y2-W2)),"")
##Definition: If campaign column is not blank and Auto. Pacing Cycle Start Date is not blank
##Python:
def calculate_date_apc_days_elapsed(row):
if pd.notnull(row[RPT_COL_CAMPAIGN]): # Checks if the campaign column is not empty
start_date = row[RPT_COL_AUTO_PACING_CYCLE_START_DATE]
today_date = pd.to_datetime(today).normalize() # Make today's date timezone naive
if pd.isnull(start_date): # Checks if the calculated start date is empty
return ""
# Calculate the difference between the dates in 'today_date' and 'start_date'
diff = (today_date - start_date).days
if diff <= 0: # If the difference is less than or equal to 0
return 0
else:
return diff
else:
return ""
# Apply the function to each row in the DataFrame
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_DAYS_ELAPSED] = df_campaign_with_totals.apply(calculate_date_apc_days_elapsed, axis=1)
##Auto. Pacing Cycle Days Remaining
##Formula: =if(A2<>"",IF(Z2-AA2<0,0,Z2-AA2),"")
##Python:
def calculate_apc_days_remaining(row):
if pd.notnull(row[RPT_COL_CAMPAIGN]):
# Convert the values to numeric types before subtraction
days = pd.to_numeric(row[RPT_COL_AUTO_PACING_CYCLE_DAYS], errors='coerce')
days_elapsed = pd.to_numeric(row[RPT_COL_AUTO_PACING_CYCLE_DAYS_ELAPSED], errors='coerce')
# Check if the conversion resulted in NaN (not a number)
if pd.isnull(days) or pd.isnull(days_elapsed):
return "" # Return an empty string or some other appropriate value for your context
# Calculate the difference
diff = days - days_elapsed
return max(0, diff)
else:
return ""
# Apply the function to each row in the DataFrame
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING] = df_campaign_with_totals.apply(calculate_apc_days_remaining, axis=1)
##Auto. Pacing Daily Target (Spend/Impressions/Views)
##Formula: =iferror(IF(D2="ms",G2/Z2,IF(D2="CPM",G2/Z2,IF(D2="CPV",G2/Z2,""))),"")
##Python
def calculate_auto_pacing_daily_target(row):
try:
# Since the calculation is the same for all specified campaign types, we directly perform the division
if row[RPT_COL_GOAL] in ["MS", "CPM", "CPV"]:
return row[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] / row[RPT_COL_AUTO_PACING_CYCLE_DAYS] if row[RPT_COL_AUTO_PACING_CYCLE_DAYS] != 0 else 0
else:
return 0 # Default to 0 for undefined campaign types or other errors
except:
return 0 # Safeguard against any other kind of error
# Apply the function to each row of the DataFrame
df_campaign_with_totals[RPT_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS] = df_campaign_with_totals.apply(calculate_auto_pacing_daily_target, axis=1)
df_campaign_with_totals[RPT_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS] = df_campaign_with_totals[RPT_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS].round(2)
df_campaign_with_totals[RPT_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS] = df_campaign_with_totals[RPT_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS].astype(str)
##Auto. Pacing Cycle Expected to Date
##Formula: =if(A2<>"",AC2*AA2,"")
##Python:
def calculate_apc_etd(row):
if pd.notnull(row[RPT_COL_CAMPAIGN]):
# Convert both columns to numeric types, coercing errors to NaN
daily_target = pd.to_numeric(row[RPT_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS], errors='coerce')
days_elapsed = pd.to_numeric(row[RPT_COL_AUTO_PACING_CYCLE_DAYS_ELAPSED], errors='coerce')
# Check if either value is NaN after conversion
if pd.isnull(daily_target) or pd.isnull(days_elapsed):
return 0 # Return 0 or some other appropriate default value
# Perform the multiplication
return daily_target * days_elapsed
else:
return 0 # Return 0 or some other appropriate default value if the campaign is null
# Apply the function to each row in the DataFrame
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] = df_campaign_with_totals.apply(calculate_apc_etd, axis=1)
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] = df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE].round(2)
#df1_deduplicated[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] = df1_deduplicated[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE].astype(str)
##Auto. Pacing Cycle Pacing
##Formula: =IFerror(IF(D2="ms",S2/AD2,IF(D2="CPM",T2/AD2,IF(D2="CPV",V2/AD2,0))),0)
##Python
def calculate_auto_pacing_cycle_pacing(row):
try:
# Check campaign type and perform corresponding calculation
if row[RPT_COL_GOAL] == "MS":
return row[RPT_COL_AUTO_PACING_CYCLE_PUB_COST] / row[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] if row[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] != 0 else 0
elif row[RPT_COL_GOAL] == "CPM":
return row[RPT_COL_AUTO_PACING_CYCLE_IMPR] / row[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] if row[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] != 0 else 0
elif row[RPT_COL_GOAL] == "CPV":
return row[RPT_COL_AUTO_PACING_CYCLE_VIEWS] / row[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] if row[RPT_COL_AUTO_PACING_CYCLE_EXPECTED_TO_DATE] != 0 else 0
else:
return 0
except:
return 0
# Calculate pacing as a percentage and convert to numeric type
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_PACING] = df_campaign_with_totals.apply(calculate_auto_pacing_cycle_pacing, axis=1)
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_PACING] = pd.to_numeric(df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_PACING], errors='coerce') * 100.0
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_PACING] = df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_PACING].round(2)
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_PACING] = df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_PACING].astype(str) + '%'
##Auto. Pacing Cycle Threshold
##Formula: =if(A2<>"",IF(Y2>F2,"Campaign Ended",if(AE2<97%,"Under Pacing",if(AND(AE2<104%,AE2>103%),"Over Pacing",if(AE2>105%,"Severely Overpacing","On Pace")))),"")
##Python
def calculate_auto_pacing_cycle_threshold(row):
today_date = row[RPT_COL_TODAYS_DATE]
if pd.notnull(row[RPT_COL_CAMPAIGN]):
end_date = row[RPT_COL_PACING__END_DATE]
if end_date is not pd.NaT and today_date > end_date:
return "Campaign Ended"
else:
# Convert pacing to a float by removing the '%' sign and dividing by 100
pacing_str = row[RPT_COL_AUTO_PACING_CYCLE_PACING].replace('%', '') # Remove the '%' sign
pacing = float(pacing_str) / 100.0 # Convert to float and divide by 100
if pacing < 0.97:
return "Under Pacing"
elif 0.97 <= pacing < 1.04:
return "On Pace"
elif 1.04 <= pacing <= 1.05:
return "Over Pacing"
elif pacing > 1.05:
return "Severely Overpacing"
else:
return "On Pace"
else:
return ""
# Apply the function to each row of the DataFrame
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_THRESHOLD] = df_campaign_with_totals.apply(calculate_auto_pacing_cycle_threshold, axis=1)
##Total Target (Spend/Impr./Views) v2
##Formula: =if(A22<>"",if(Or(N22="Auto",N22="No",N22=""),IF(F22-E22+1<=31,G22,(G22/30)*(F22-E22)+1),G22),"")
##Python
# Extract and parse target values
df_campaign_with_totals['Target (Impr/Spend/Views)'] = df_campaign_with_totals['Target (Impr/Spend/Views)'].astype(str).replace({',': '', '\s+': ''}, regex=True)
df_campaign_with_totals['Target (Impr/Spend/Views)'] = pd.to_numeric(df_campaign_with_totals['Target (Impr/Spend/Views)'], errors='coerce')
df_campaign_with_totals['Total Days'] = pd.to_numeric(df_campaign_with_totals['Total Days'], errors='coerce')
# Calculate days_diff
df_campaign_with_totals['days_diff'] = (pd.to_datetime(df_campaign_with_totals['Pacing - End Date']) - pd.to_datetime(df_campaign_with_totals['Pacing - Start Date'])).dt.days + 1
# Correct calculation for Total Target (Spend/Impr/Views) dynamically
def calculate_total_target(row):
if pd.notna(row['Campaign']):
if row['Installments'] in ["Auto", "No", ""]:
days_diff = row['days_diff']
initial_target = row['Target (Impr/Spend/Views)']
if days_diff <= 31:
return initial_target
else:
return (initial_target / 30) * (days_diff - 1)
else:
return row['Target (Impr/Spend/Views)']
return ""
# Apply the function to each row of the DataFrame
df_campaign_with_totals[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS] = df_campaign_with_totals.apply(calculate_total_target, axis=1)
df_campaign_with_totals[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS] = df_campaign_with_totals[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS].round(2)
df_campaign_with_totals[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS] = df_campaign_with_totals[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS].replace(np.nan, '', regex=True)
df_campaign_with_totals[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS] = df_campaign_with_totals[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS].astype(str)
##Total Days
##Formula: =if(A2<>"",IF(F2-E2+1<1,"",F2-E2+1),"")
##Python
def calculate_total_days(row):
if pd.notnull(row[RPT_COL_CAMPAIGN]):
# Calculate the difference in days, then add 1
days_diff = (row[RPT_COL_PACING__END_DATE] - row[RPT_COL_PACING__START_DATE]).days + 1
# Check if the calculated difference is less than 1
if days_diff < 1:
return ""
else:
return days_diff
else:
return ""
# Apply the function to each row in the DataFrame
df_campaign_with_totals[RPT_COL_TOTAL_DAYS] = df_campaign_with_totals.apply(calculate_total_days, axis=1)
##Total Days Elapsed
##Formula: =IF(A2<>"",IF(AH2="","",IF(Y2-E2<=0,"",if(Y2-E2>=AH2,AH2,Y2-E2))),"")
##Python
# Define the calculation as a function
def calculate_days_elapsed(row):
if pd.notnull(row[RPT_COL_CAMPAIGN]): # Checks if 'A' is not empty
if pd.isnull(row[RPT_COL_TOTAL_DAYS]): # Checks if 'AH' is empty
return ""
today_date = row[RPT_COL_TODAYS_DATE]
start_date = row[RPT_COL_PACING__START_DATE]
if start_date is not pd.NaT:
start_date = pd.to_datetime(start_date.date()) # Convert to Timestamp
today_date = row[RPT_COL_TODAYS_DATE]
# Calculate the difference in days between 'Y' and 'E'
days_diff = (today_date - start_date).days
if days_diff <= 0:
return ""
# Convert RPT_COL_TOTAL_DAYS to an integer before comparison
total_days = int(row[RPT_COL_TOTAL_DAYS]) if row[RPT_COL_TOTAL_DAYS] != "" else 0
if days_diff >= total_days:
return total_days
else:
return days_diff
else:
return ""
# Apply the function across each row
df_campaign_with_totals[RPT_COL_TOTAL_DAYS_ELAPSED] = df_campaign_with_totals.apply(calculate_days_elapsed, axis=1)
##Total Daily Target v2
##Formula: =iferror(AL2/AM2,"")
##Python
# Verify calculation for Total Daily Target
def safe_division(numerator, denominator):
try:
numerator = pd.to_numeric(numerator, errors='coerce')
if isinstance(denominator, (int, float)) and denominator != 0:
return numerator / denominator
else:
return 0
except ZeroDivisionError:
return 0
df_campaign_with_totals[RPT_COL_TOTAL_DAILY_TARGET] = df_campaign_with_totals.apply(lambda row: safe_division(row[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS], row[RPT_COL_TOTAL_DAYS]), axis=1)
df_campaign_with_totals[RPT_COL_TOTAL_DAILY_TARGET] = df_campaign_with_totals[RPT_COL_TOTAL_DAILY_TARGET].fillna(0).round(2) # Adjust rounding
##Total Expected to Date v2
##Formula: =if(AI2<>"",AI2*AJ2,"")
##Python
# Verify calculation for Total Expected to Date
#current_date = pd.to_datetime("2024-07-25")
#df1_deduplicated[RPT_COL_TOTAL_DAYS_ELAPSED] = (current_date - pd.to_datetime(df1_deduplicated['Pacing - Start Date'])).dt.days
def calculate_expected_to_date(row):
total_days_elapsed = pd.to_numeric(row['Total Days Elapsed'], errors='coerce')
total_daily_target = pd.to_numeric(row['Total Daily Target'], errors='coerce')
if pd.isnull(total_days_elapsed) or pd.isnull(total_daily_target):
return 0
return total_days_elapsed * total_daily_target
# Apply the function to each row in the DataFrame
df_campaign_with_totals[RPT_COL_TOTAL_EXPECTED_TO_DATE] = df_campaign_with_totals.apply(calculate_expected_to_date, axis=1)
df_campaign_with_totals[RPT_COL_TOTAL_EXPECTED_TO_DATE] = df_campaign_with_totals[RPT_COL_TOTAL_EXPECTED_TO_DATE].fillna(0).round(2) # Adjust rounding
##Total Pacing
##Formula: =IFerror(IF(D2="ms",O2/AK2,IF(D2="CPM",P2/AK2,IF(D2="CPV",R2/AK2,""))),"")
##Python
# Function to calculate Total Pacing as a percentage
def calculate_total_pacing(row):
try:
# Goal type is 'ms'
if row[RPT_COL_GOAL] == 'MS':
return row[RPT_COL_TOTAL_PUB_COST] / row[RPT_COL_TOTAL_EXPECTED_TO_DATE] if row[RPT_COL_TOTAL_EXPECTED_TO_DATE] != 0 else ""
# Goal type is 'CPM'
elif row[RPT_COL_GOAL] == 'CPM':
return row[RPT_COL_TOTAL_IMPRESSIONS] / row[RPT_COL_TOTAL_EXPECTED_TO_DATE] if row[RPT_COL_TOTAL_EXPECTED_TO_DATE] != 0 else ""
# Goal type is 'CPV'
elif row[RPT_COL_GOAL] == 'CPV':
return row[RPT_COL_TOTAL_VIEWS] / row[RPT_COL_TOTAL_EXPECTED_TO_DATE] if row[RPT_COL_TOTAL_EXPECTED_TO_DATE] != 0 else ""
else:
return ""
except:
# Handles division by zero or any other calculation error
return ""
# Calculate pacing as a percentage and convert to numeric type
df_campaign_with_totals[RPT_COL_TOTAL_PACING] = df_campaign_with_totals.apply(calculate_total_pacing, axis=1)
df_campaign_with_totals[RPT_COL_TOTAL_PACING] = pd.to_numeric(df_campaign_with_totals[RPT_COL_TOTAL_PACING], errors='coerce') * 100.0
df_campaign_with_totals[RPT_COL_TOTAL_PACING] = df_campaign_with_totals[RPT_COL_TOTAL_PACING].round(2)
df_campaign_with_totals[RPT_COL_TOTAL_PACING] = df_campaign_with_totals[RPT_COL_TOTAL_PACING].fillna('')
df_campaign_with_totals[RPT_COL_TOTAL_PACING] = df_campaign_with_totals[RPT_COL_TOTAL_PACING].apply(lambda x: f"{int(x)}%" if x != '' else x)
##Delivery Status
##Formula: =iferror(IF(AND(Y3>F3, D3="CPM", P3/AG3<97%), "Underdelivery",
# IF(AND(Y3>F3, D3="CPM", OR(P3/AG3>115%, P3-AG3>50000)), "Overdelivery",
# IF(AND(Y3>F3, D3="CPM", P3/AG3>=97%, P3/AG3<=115%), "On Target",
# IF(AND(Y3>F3, D3="MS", O3/AG3<100%), "Underdelivery",
# IF(AND(Y3>F3, D3="MS", OR(O3/AG3>103%, O3-AG3>500)), "Overdelivery",
# IF(AND(Y3>F3, D3="MS", O3/AG3>=100%, O3/AG3<=103%), "On Target",
# IF(AND(Y3>F3, D3="CPV", R3/AG3<100%), "Underdelivery",
# IF(AND(Y3>F3, D3="CPV", OR(R3/AG3>103%, R3-AG3>50000)), "Overdelivery",
# IF(AND(Y3>F3, D3="CPV", R3/AG3>=100%, R3/AG3<=103%), "On Target", "")
##Python
def calculate_delivery_status(row):
try:
if row[RPT_COL_AUTO_PACING_CYCLE_THRESHOLD] == "Campaign Ended":
goal = row[RPT_COL_GOAL]
total_target_spend_per_imprviews = float(str(row[RPT_COL_TOTAL_TARGET_SPEND_PER_IMPRVIEWS]).replace(',', ''))
if goal == "CPM":
impressions = float(str(row[RPT_COL_TOTAL_IMPRESSIONS]).replace(',', ''))
ratio = impressions / total_target_spend_per_imprviews
if ratio < 0.97:
return "Underdelivery"
elif ratio > 1.15 or (impressions - total_target_spend_per_imprviews > 50000):
return "Overdelivery"
elif 0.97 <= ratio <= 1.15:
return "On Target"
elif goal == "CPV":
views = float(str(row[RPT_COL_TOTAL_VIEWS]).replace(',', ''))
ratio = views / total_target_spend_per_imprviews
if ratio < 1.00:
return "Underdelivery"
elif ratio > 1.03 or (views - total_target_spend_per_imprviews > 50000):
return "Overdelivery"
elif 1.00 <= ratio <= 1.03:
return "On Target"
elif goal == "MS":
cost = float(str(row[RPT_COL_TOTAL_PUB_COST]).replace(',', ''))
ratio = cost / total_target_spend_per_imprviews
if ratio < 1.00:
return "Underdelivery"
elif ratio > 1.03 or (cost - total_target_spend_per_imprviews > 500): # Corrected to use `cost` instead of `views`
return "Overdelivery"
elif 1.00 <= ratio <= 1.03:
return "On Target"
else:
return ""
except Exception as e:
# Return the exception for troubleshooting
return str(e)
return ""
# Apply the function to each row
df_campaign_with_totals[RPT_COL_DELIVERY_STATUS] = df_campaign_with_totals.apply(calculate_delivery_status, axis=1)
##Auto. Pacing Cycle Target Remaining (Spend, Impr., Views)
##Formula: =IF(A2<>"",IF(D2="CPM",G2-T2,if(D2="ms",G2-S2,IF(D2="CPV",G2-V2))),"")
##Python
def calculate_auto_pacing_cycle_target_remaining(row):
if row[RPT_COL_CAMPAIGN] != "": # Checks if the campaign column is not empty
if row[RPT_COL_GOAL] == "CPM":
return row[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] - row[RPT_COL_AUTO_PACING_CYCLE_IMPR]
elif row[RPT_COL_GOAL] == "MS":
return row[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] - row[RPT_COL_AUTO_PACING_CYCLE_PUB_COST]
elif row[RPT_COL_GOAL] == "CPV":
return row[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] - row[RPT_COL_AUTO_PACING_CYCLE_VIEWS]
else:
return ""
else:
return ""
# Apply the function to each row of the DataFrame
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_TARGET_REMAINING_SPEND_PER_IMPRVIEWS] = df_campaign_with_totals.apply(calculate_auto_pacing_cycle_target_remaining, axis=1)
df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_TARGET_REMAINING_SPEND_PER_IMPRVIEWS] = df_campaign_with_totals[RPT_COL_AUTO_PACING_CYCLE_TARGET_REMAINING_SPEND_PER_IMPRVIEWS].apply(
lambda x: f'{x:.2f}' if not pd.isna(pd.to_numeric(x, errors='coerce')) else x
)
##Pacing Cycle Daily Allocation (Spend, Impr., Views)
##Formula: =iferror(IF(D2="MS",(G2-S2)/AB2,IF(D2="CPM",(G2-T2)/AB2,if(D2="CPV",(G2-V2)/AB2,""))))
##Python
def calculate_pacing_cycle_daily_allocation(row):
try:
if row[RPT_COL_GOAL] == "MS":
remaining_target = row[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] - row[RPT_COL_AUTO_PACING_CYCLE_PUB_COST]
return remaining_target / row[RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING] if row[RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING] != 0 else 0
elif row[RPT_COL_GOAL] == "CPM":
remaining_target = row[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] - row[RPT_COL_AUTO_PACING_CYCLE_IMPR]
return remaining_target / row[RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING] if row[RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING] != 0 else 0
elif row[RPT_COL_GOAL] == "CPV":
remaining_target = row[RPT_COL_TARGET_IMPR_PER_SPENDVIEWS] - row[RPT_COL_AUTO_PACING_CYCLE_VIEWS]
return remaining_target / row[RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING] if row[RPT_COL_AUTO_PACING_CYCLE_DAYS_REMAINING] != 0 else 0
else:
return 0 # Return 0 or an appropriate value for rows that do not match any campaign type conditions
except:
return 0 # Handles division by zero or any other calculation error gracefully
# Apply the function to each row of the DataFrame
df_campaign_with_totals[RPT_COL_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPR_VIEWS] = df_campaign_with_totals.apply(calculate_pacing_cycle_daily_allocation, axis=1)
df_campaign_with_totals[RPT_COL_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPR_VIEWS] = df_campaign_with_totals[RPT_COL_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPR_VIEWS].apply(
lambda x: f'{x:.2f}' if not pd.isna(pd.to_numeric(x, errors='coerce')) else x
)
# Re-add RPT_COL_GOAL after merge
#df_campaign_with_totals['Goal'] = 'Goal'
# Function to calculate 'Last 3 Days Start'
def calculate_last_3_days(row):
today = datetime.datetime.now().date()
if pd.notnull(row['Campaign']) and row['Campaign'] != "":
return today - datetime.timedelta(days=1)
return pd.NaT
# Function to calculate 'Last 3 Days End'
def calculate_last_3_days_end(row):
today = datetime.datetime.now().date()
if pd.notnull(row['Campaign']) and row['Campaign'] != "":
return today - datetime.timedelta(days=4)
return pd.NaT
# Function to calculate Pub. Cost Last 3 Days
def calculate_last_3_days_cost(df):
today = datetime.datetime.now().date()
last_3_days_start = pd.Timestamp(today - datetime.timedelta(days=4))
last_3_days_end = pd.Timestamp(today - datetime.timedelta(days=1)) # Exclude today
filtered_data = inputDf[(inputDf['Date'] >= last_3_days_start) & (inputDf['Date'] <= last_3_days_end)]
result = filtered_data.groupby('Campaign ID')['Pub. Cost $'].sum().reset_index()
result['Pub. Cost $'] = result['Pub. Cost $'].round(2)
result.rename(columns={'Pub. Cost $': 'Pub. Cost Last 3 Days'}, inplace=True)
return result
# Function to calculate Last 3 Days Impressions
def calculate_last_3_days_impr(df):
today = datetime.datetime.now().date()
last_3_days_start = pd.Timestamp(today - datetime.timedelta(days=4))
last_3_days_end = pd.Timestamp(today - datetime.timedelta(days=1)) # Exclude today
filtered_data = inputDf[(inputDf['Date'] >= last_3_days_start) & (inputDf['Date'] <= last_3_days_end)]
result = filtered_data.groupby('Campaign ID')['Impr.'].sum().reset_index()
result.rename(columns={'Impr.': 'Impressions Last 3 Days'}, inplace=True)
return result
# Function to calculate 'Underserved Impressions'
def calculate_underserved_impressions(row):
if pd.notnull(row['Campaign']) and row['Goal'] == "CPM":
underserved_impressions = row['Auto. Pacing Cycle Expected to Date'] - row['Auto. Pacing Cycle Impr.']
# Round the result to 2 decimal places
return round(underserved_impressions, 2)
return 0
# Function to calculate 'Impressions to Serve Today'
def calculate_impressions_to_serve_today(row):
if pd.notnull(row['Campaign']) and row['Goal'] == "CPM":
auto_pacing_daily_target = pd.to_numeric(row['Auto. Pacing Daily Target (Spend/Impressions/Views)'], errors='coerce')
underserved_impressions = pd.to_numeric(row['Underserved Impressions'], errors='coerce')
if pd.isnull(auto_pacing_daily_target) or pd.isnull(underserved_impressions):
return 0
if underserved_impressions <= 0:
# Round the daily target to 2 decimal places
return round(auto_pacing_daily_target, 2)
# Round the sum of the daily target and underserved impressions to 2 decimal places
return round(auto_pacing_daily_target + underserved_impressions, 2)
return 0
# Function to calculate '3 Day CPM'
def calculate_3_day_cpm(row):
try:
if pd.notnull(row['Impressions Last 3 Days']) and row['Impressions Last 3 Days'] > 0:
cpm = (row['Pub. Cost Last 3 Days'] / row['Impressions Last 3 Days']) * 1000
# Round the CPM to 2 decimal places
return round(max(cpm, 0.5), 2)
else:
return 0.5
except (ZeroDivisionError, TypeError):
return 0.5
# Apply the functions to the df_campaign_with_totals dataset
df_campaign_with_totals['Last 3 Days Start'] = df_campaign_with_totals.apply(calculate_last_3_days, axis=1)
df_campaign_with_totals['Last 3 Days End'] = df_campaign_with_totals.apply(calculate_last_3_days_end, axis=1)
# Recalculate the last 3 days cost and merge it back
last_3_days_cost = calculate_last_3_days_cost(df_campaign_with_totals)
df_campaign_with_totals = pd.merge(df_campaign_with_totals, last_3_days_cost, on='Campaign ID', how='left')
# Recalculate the last 3 days impressions and merge it back
last_3_days_impr = calculate_last_3_days_impr(df_campaign_with_totals)
df_campaign_with_totals = pd.merge(df_campaign_with_totals, last_3_days_impr, on='Campaign ID', how='left')
# Apply underserved impressions
df_campaign_with_totals['Underserved Impressions'] = df_campaign_with_totals.apply(calculate_underserved_impressions, axis=1)
# Apply impressions to serve today
df_campaign_with_totals['Impressions to Serve Today'] = df_campaign_with_totals.apply(calculate_impressions_to_serve_today, axis=1)
# Apply 3 Day CPM calculation
df_campaign_with_totals['3 Day CPM'] = df_campaign_with_totals.apply(calculate_3_day_cpm, axis=1)
# Round the final columns in the DataFrame to 2 decimal places (if necessary)
df_campaign_with_totals['Pub. Cost Last 3 Days'] = df_campaign_with_totals['Pub. Cost Last 3 Days'].round(2)
df_campaign_with_totals['3 Day CPM'] = df_campaign_with_totals['3 Day CPM'].round(2)
df_campaign_with_totals['Underserved Impressions'] = df_campaign_with_totals['Underserved Impressions'].round(2)
df_campaign_with_totals['Impressions to Serve Today'] = df_campaign_with_totals['Impressions to Serve Today'].round(2)
##Recommended Daily Budget v2
##Formula: =IFERROR(IF(AND(AA2=0,D2="ms"),AC2,IF(AND(AA2=0,D2="CPM"),(O2/P2)*AC2,IF(AND(AA2=0,D2="cpv"),O2/V2*AC2,IF(D2="MS",(G2-S2)/AB2,IF(D2="CPM",(O2/P2)*AO2,IF(D2="CPV",O2/V2*AO2,"")))))),"")
##Python
def convert_to_float(value):
"""
Converts a value to float. If the value is a string that includes commas,
it removes the commas before conversion. If the value is empty or not convertible,
it returns 0.0.
Parameters:
value (str or float): The value to convert.
Returns:
float: The converted value or 0.0 if conversion is not possible.
"""
try:
# Check if the value is already a float
if isinstance(value, float):
return value
# If the value is a string, remove commas and convert to float
if isinstance(value, str) and value:
return float(value.replace(',', ''))
except ValueError:
pass
# Return 0.0 for empty strings or values that cannot be converted
return 0.0
def convert_to_float(value):
"""
Converts a value to float. If the value is a string that includes commas,
it removes the commas before conversion. If the value is empty or not convertible,
it returns 0.0.
Parameters:
value (str or float): The value to convert.
Returns:
float: The converted value or 0.0 if conversion is not possible.
"""
try:
# Check if the value is already a float
if isinstance(value, float):
return value
# If the value is a string, remove commas and convert to float
if isinstance(value, str) and value:
return float(value.replace(',', ''))
except ValueError:
pass
# Return 0.0 for empty strings or values that cannot be converted
return 0.0
def calculate_recommended_daily_budget(row):
"""
Calculates the recommended daily budget based on campaign goal, remaining targets, and pacing.
Parameters:
row (pd.Series): A row from the DataFrame.
Returns:
str: The recommended daily budget for the campaign as a formatted string.
"""
try:
# Calculate the pacing cycle daily allocation and print it
pacing_cycle_daily_allocation = calculate_pacing_cycle_daily_allocation(row)
print(f"Pacing Cycle Daily Allocation (Spend/Impr./Views): {pacing_cycle_daily_allocation}")
# Check if no pacing cycle days have elapsed
if row['Auto. Pacing Cycle Days Elapsed'] == 0:
if row['Goal'] == "ms":
return row['Auto. Pacing Daily Target (Spend/Impressions/Views)']
elif row['Goal'] == "CPM":
return (row['Total Pub Cost'] / row['Total Impressions']) * row['Auto. Pacing Daily Target (Spend/Impressions/Views)']
elif row['Goal'] == "cpv":
return (row['Total Pub Cost'] / row['Auto. Pacing Cycle Views']) * row['Auto. Pacing Daily Target (Spend/Impressions/Views)']
# If pacing cycle days have elapsed, apply further logic based on the goal
if row['Goal'] == "MS":
return (row['Target (Impr/Spend/Views)'] - row['Auto. Pacing Cycle Pub. Cost']) / row['Auto. Pacing Cycle Days Remaining']
elif row['Goal'] == "CPM":
# New logic for CPM calculation when days have elapsed
return (row['3 Day CPM'] / 1000) * (1 + 0.025) * row['Impressions to Serve Today']
elif row['Goal'] == "CPV":
return (row['Total Pub Cost'] / row['Auto. Pacing Cycle Views']) * pacing_cycle_daily_allocation
# Return an empty result if no condition is met
return ""
except Exception as e:
print(f"Error calculating recommended daily budget for campaign {row['Campaign']}: {e}")
result = "" # Set a default value in case of an error
# Ensure result is a float before formatting
try:
result = float(result)
return f"{result:.2f}"
except ValueError:
# If result cannot be converted to float, return it as is
return result
# Apply the adjusted function
df_campaign_with_totals[RPT_COL_RECOMMENDED_DAILY_BUDGET] = df_campaign_with_totals.apply(calculate_recommended_daily_budget, axis=1)
df_campaign_with_totals[RPT_COL_RECOMMENDED_DAILY_BUDGET] = df_campaign_with_totals[RPT_COL_RECOMMENDED_DAILY_BUDGET].apply(
lambda x: f'{x:.2f}' if isinstance(x, (int, float)) and not pd.isna(x) else ""
)
# Assuming df1_deduplicated is your final DataFrame after all transformations
print(df_campaign_with_totals[[RPT_COL_CAMPAIGN, RPT_COL_RECOMMENDED_DAILY_BUDGET]].head()) # Print a sample for verification
outputDf = df_campaign_with_totals
print("outputDf shape", outputDf.shape)
## local debug
if local_dev:
output_filename = 'outputDf.csv'
outputDf.to_csv(output_filename, index=False)
print(f"Local Dev: Output written to: {output_filename}")
debug_filename = 'debugDf.csv'
debugDf.to_csv(debug_filename, index=False)
print(f"Local Dev: Debug written to: {debug_filename}")
Post generated on 2024-11-27 06:58:46 GMT