Script 549: Budget Pacing
Purpose
Pace Daily Budget for each Campaign
To Elaborate
The Python script solves the problem of pacing the daily budget for each campaign. It calculates the remaining days for each campaign based on the flight date in the campaign name and the current date. The remaining days are then used to determine the daily budget allocation for each campaign.
Walking Through the Code
- The primary data source and columns are defined.
- Output columns and initial values are set.
- The client timezone is set.
- The current date is obtained.
- A sample DataFrame is created with campaign names.
- The
calculate_remaining_days
function is defined to calculate the remaining days for a given campaign name.- The campaign name is split using ‘_’ as a separator.
- If the campaign name has 3 parts:
- The flight date is extracted from the second part of the name.
- The flight date string is converted to a datetime object.
- The naive datetime is converted to an aware datetime with the client timezone.
- The remaining days are calculated by subtracting the current date from the flight date.
- The remaining days are returned.
- If the campaign name does not have 3 parts, None is returned.
- The
calculate_remaining_days
function is applied to the ‘Campaign’ column of the DataFrame, creating a new column ‘DAYS_REMAINING’. - The DataFrame is saved to a CSV file with the new column.
Vitals
- Script ID : 549
- Client ID / Customer ID: 1306926843 / 60270139
- Action Type: Bulk Upload (Preview)
- Item Changed: Campaign
- Output Columns: Account, Campaign, Days Elapsed, Days Remaining
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: Jesus Garza (jgarza@marinsoftware.com)
- Created by Jesus Garza on 2023-11-29 15:34
- Last Updated by Jesus Garza on 2023-12-06 04:01
> 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
##
## name: Budget Pacing
## description:
## Pace Daily Budget for each Campaign
##
## author: Jesus Garza
## created: 2023-11-29
##
# primary data source and columns
inputDf = dataSourceDict["1"]
# output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_DAYS_ELAPSED = 'Days Elapsed'
BULK_COL_DAYS_REMAINING = 'Days Remaining'
outputDf[BULK_COL_DAYS_ELAPSED] = "<<YOUR VALUE>>"
outputDf[BULK_COL_DAYS_REMAINING] = "<<YOUR VALUE>>"
# Set timezone
CLIENT_TIMEZONE = 'America/Denver'
today = datetime.datetime.now().astimezone(pytz.timezone(CLIENT_TIMEZONE)).date()
# Sample DataFrame creation (replace this with your data loading logic)
data = {'Campaign': ['Client1_10/2/2023-12/31/2023_CPM_1300', 'Client2_11/15/2023-12/31/2023_CPC_1500']}
df = pd.DataFrame(data)
def calculate_remaining_days(campaign_name):
# Split the campaign name using '_' as a separator
parts = campaign_name.split('_')
if len(parts) == 3:
# Extract the flight date from the second part of the name
flight_date_str = parts[1]
# Convert the flight date string to a datetime object
flight_date_naive = datetime.datetime.strptime(flight_date_str, "%m/%d/%Y")
# Convert the naive datetime to aware datetime
flight_date = flight_date_naive.astimezone(pytz.timezone(CLIENT_TIMEZONE))
# Calculate the remaining days
remaining_days = (flight_date - datetime.datetime.now().astimezone(pytz.timezone(CLIENT_TIMEZONE))).days
return remaining_days
else:
return None
# Apply the function to the 'Campaign' column and create a new column 'DAYS_REMAINING'
df['DAYS_REMAINING'] = df['Campaign'].apply(calculate_remaining_days)
# Save the DataFrame back to the CSV file with the new column
df.to_csv('/Users/jgarza/Desktop/days_remaining.csv', index=False) # Replace 'output_file.csv' with the desired output file path
Post generated on 2024-05-15 07:44:05 GMT