Script 801: Pacing New Campaign Calculations
Purpose
The Python script calculates pacing metrics for digital advertising campaigns to ensure they meet their budget and performance goals.
To Elaborate
The script is designed to manage and calculate pacing metrics for digital advertising campaigns, ensuring they align with their budget and performance goals. It processes campaign data to determine the start and end dates of pacing cycles, calculates the total days and days elapsed, and evaluates the expected performance to date. The script also computes various metrics such as daily targets, pacing percentages, and delivery statuses. These calculations help in assessing whether a campaign is on track, under-delivering, or over-delivering based on predefined goals like impressions, spend, or views. The script is particularly useful for advertisers and marketers who need to monitor and adjust their campaign strategies to optimize performance and budget utilization.
Walking Through the Code
- Initialization and Setup
- The script begins by determining if it is running on a server or locally. If local, it loads 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 columns to appropriate data types, such as numeric and datetime.
- It initializes columns for calculations, ensuring data is ready for processing.
- Pacing Cycle Calculations
- Functions are defined to calculate the start and end dates of the auto pacing cycle based on campaign data.
- The script calculates total days, days elapsed, and days remaining for each campaign.
- Metric Calculations
- The script computes various metrics, including daily targets, expected performance to date, and pacing percentages.
- It evaluates whether campaigns are on track, under-pacing, or over-pacing based on these metrics.
- Final Calculations and Output
- The script calculates the recommended daily budget and delivery status for each campaign.
- It aggregates results and prepares the final output, which can be saved locally for further analysis.
Vitals
- Script ID : 801
- Client ID / Customer ID: 1306927731 / 60270139
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, 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 Pub. Cost, Auto. Pacing Cycle Start Date, Auto. Pacing Cycle Threshold, Auto. Pacing Cycle Views, Delivery Status, 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, Pacing Calculation Date
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: ascott@marinsoftware.com (ascott@marinsoftware.com)
- Created by ascott@marinsoftware.com on 2024-03-13 14:59
- Last Updated by ascott@marinsoftware.com on 2024-09-13 16:18
> 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
## 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
import datetime
# 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().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_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS = 'Auto. Pacing Daily Target (Spend/Impressions/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_TARGET_REMAINING_SPEND_PER_IMPRVIEWS = 'Auto. Pacing Cycle Target Remaining (Spend/Impr./Views)'
RPT_COL_AUTO_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPRVIEWS = 'Auto. Pacing Cycle Daily Allocation (Spend/Impr./Views)'
RPT_COL_RECOMMENDED_DAILY_BUDGET = 'Recommended Daily 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_IMPR_VIEWS = 'Pacing Cycle Daily Allocation (Spend/Impr./Views)'
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
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_TARGET_REMAINING_SPEND_PER_IMPRVIEWS = 'Auto. Pacing Cycle Target Remaining (Spend/Impr./Views)'
BULK_COL_AUTO_PACING_CYCLE_THRESHOLD = 'Auto. Pacing Cycle Threshold'
BULK_COL_AUTO_PACING_CYCLE_VIEWS = 'Auto. Pacing Cycle Views'
BULK_COL_INSTALLMENTS = 'Installments'
BULK_COL_PACING_CALCULATION_DATE = 'Pacing Calculation Date'
BULK_COL_AUTO_PACING_DAILY_TARGET_SPEND_PER_IMPRESSIONSVIEWS = 'Auto. Pacing Daily Target (Spend/Impressions/Views)'
BULK_COL_DELIVERY_STATUS = 'Delivery Status'
BULK_COL_AUTO_PACING_CYCLE_DAILY_ALLOCATION_SPEND_PER_IMPRVIEWS = 'Auto. Pacing Cycle Daily Allocation (Spend/Impr./Views)'
BULK_COL_PACING_CYCLE_DAILY_ALLOCATION_SPEND_IMPR_VIEWS = 'Pacing Cycle Daily Allocation (Spend/Impr./Views)'
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'
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()
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_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_IMPR_VIEWS] = df_campaign_with_totals[RPT_COL_PACING_CYCLE_DAILY_ALLOCATION_SPEND_IMPR_VIEWS].apply(
lambda x: f'{x:.2f}' if not pd.isna(pd.to_numeric(x, errors='coerce')) else x
)
##Recommended Daily Budget
##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}")
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 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":
return (row['Total Pub Cost'] / row['Total Impressions']) * pacing_cycle_daily_allocation
elif row['Goal'] == "CPV":
return (row['Total Pub Cost'] / row['Auto. Pacing Cycle Views']) * pacing_cycle_daily_allocation
return ""
if result <= 0:
result = 0.10 # Set a default value if the result is less than or equal to 0
except Exception as e:
print(f"Error calculating recommended daily budget for campaign {row[RPT_COL_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