Script 549: Budget Pacing

Purpose

The Python script calculates the remaining days for each campaign based on its specified end date.

To Elaborate

The script is designed to manage and pace the daily budget for various marketing campaigns by calculating the number of days remaining until the end of each campaign. It processes campaign data, which includes campaign names formatted with specific start and end dates. The script extracts these dates, calculates how many days are left until the campaign’s end date, and appends this information to the campaign data. This allows for better budget allocation and pacing by providing insights into how much time is left to spend the allocated budget effectively. The script is particularly useful for marketing teams who need to ensure that their campaigns are on track to utilize their budgets efficiently over the campaign period.

Walking Through the Code

  1. Data Source Initialization
    • The script begins by defining the primary data source, inputDf, which is expected to contain campaign data. This data is structured in a way that includes campaign names with embedded date information.
  2. Data Preparation
    • A sample DataFrame df is created with campaign data. This is a placeholder for actual data loading logic, which should be replaced with real data input methods.
  3. Function Definition
    • The calculate_remaining_days function is defined to process each campaign name. It splits the campaign name to extract the end date, converts it to a timezone-aware datetime object, and calculates the remaining days until this date.
  4. Application of Function
    • The function is applied to the ‘Campaign’ column of the DataFrame df, creating a new column ‘DAYS_REMAINING’ that stores the calculated remaining days for each campaign.
  5. Output
    • Finally, the updated DataFrame, now including the ‘DAYS_REMAINING’ column, is saved to a CSV file. This output can be used for further analysis or reporting.

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-11-27 06:58:46 GMT

comments powered by Disqus