Script 127: Auto Pause After Event Date

Purpose

Auto Pause After Event Date

To Elaborate

The Python script changes the status of a group to “Paused” for events that have already occurred.

Walking Through the Code

  1. The script defines column constants for various data columns.
  2. The current date and time in the CST timezone is obtained.
  3. The script extracts the date from the “Group” column and converts it to a datetime object.
  4. A new “Status” column is initialized with the current group status.
  5. The script updates the “Status” column to “Paused” for groups with a date earlier than the current date.
  6. The script checks if any changes were made to the group status.
  7. If changes were made, the modified rows are printed and filtered to specific columns.
  8. The resulting output DataFrame is printed.
  9. If no changes were made, a message stating “No changes” is printed and an empty DataFrame is created.

Vitals

  • Script ID : 127
  • Client ID / Customer ID: 1306925293 / 60269377
  • Action Type: Bulk Upload
  • Item Changed: AdGroup
  • Output Columns: Account, Campaign, Group, Status
  • Linked Datasource: M1 Report
  • Reference Datasource: None
  • Owner: Jeff Sands (jsands@marinsoftware.com)
  • Created by Jeff Sands on 2023-05-23 19:14
  • Last Updated by lneels@marinsoftware.com 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
RPT_COL_GROUP = 'Group'
RPT_COL_PUBLISHER = 'Publisher'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_CAMPAIGN_STATUS = 'Campaign Status'
RPT_COL_GROUP_STATUS = 'Group Status'
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_GROUP = 'Group'
BULK_COL_STATUS = 'Status' 


# Assuming inputDf is the DataFrame where the data is stored

today = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=-6))).date()  # CST timezone

# Extract the date from RPT_COL_GROUP and convert it to datetime object
inputDf['ParsedDate'] = inputDf[RPT_COL_GROUP].str.extract(r'(\d{1,2}/\d{1,2}/\d{4})')
inputDf['ParsedDate'] = pd.to_datetime(inputDf['ParsedDate'], format="%m/%d/%Y")

# initialize new Status column with current Group Status
inputDf[BULK_COL_STATUS] = inputDf[RPT_COL_GROUP_STATUS]

# Update the Status column using vectorized operations
inputDf.loc[inputDf['ParsedDate'].dt.date < today, BULK_COL_STATUS] = "Paused"

changed = (inputDf[BULK_COL_STATUS] == "Paused") & (inputDf[BULK_COL_STATUS] != inputDf[RPT_COL_GROUP_STATUS])

if changed.sum() > 0:

    print("status changed", tableize(inputDf[changed]))

    # Filter and output the modified rows with specific columns
    outputDf = inputDf[changed][[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_GROUP, BULK_COL_STATUS]]

    # Print the resulting output DataFrame
    print("outputDf", tableize(outputDf))

else:
    print("No changes")
    outputDf = pd.DataFrame(columns=[RPT_COL_ACCOUNT, RPT_COL_CAMPAIGN, RPT_COL_GROUP, BULK_COL_STATUS])

Post generated on 2024-05-15 07:44:05 GMT

comments powered by Disqus