Want to get involved? We're always looking for ideas and content for Weekly Challenges.
SUBMIT YOUR IDEAIf I was to calculate the DateTimeDiff between the last time @SeanAdams and I raced to the finish on something, it would have been a while... until now 😉
#SnakingMyWayThruChallenges (and starting with the easier ones, because I don't really know what I'm doing yet...)
# Read in various packages + Alteryx start data from input 1
from ayx import Package
import datetime
import re
import pandas
from ayx import Alteryx
dist_data = Alteryx.read("#1").values
# Define the datediff_list variable, which will become
# the list of our date differences.
# Also define the pattern that will be used in the RegEx
# expression to isolate datetime diff values.
datediff_list = list()
pattern = '(\d+)\sdays\s(\d+):(\d+):(\d+)'
# For loop to iterate through records & create a list
# for each of the desired date component values.
for i in range (len(dist_data)):
# Calculate datediff (from datetime module) using
# data in positions 1 & 2, which are datetime values.
testrecord1 = dist_data[i][1]
testrecord2 = dist_data[i][2]
diff = str(testrecord2 - testrecord1)
# Split the results of datetime diff calc from
# "n days nn:nn:nn" format into 4 columns.
diff = re.split(pattern,diff)
# Regex leaves some extra '' values in list,
# so this will remove them.
while '' in diff: diff.remove('')
# Convert the 4 list values into integers.
for n in range (0,4):
diff[n] = int(diff[n])
# Prepend original list values to the new diff list.
diff.insert(0,str(dist_data[i][2]))
diff.insert(0,str(dist_data[i][1]))
diff.insert(0,str(dist_data[i][0]))
#For each iteration, append new list to the existing list.
datediff_list.append(diff)
output = pandas.DataFrame(datediff_list)
## OUTPUT DATA
# Convert to dataframe using pandas module,
# and then rename columns.
output = pandas.DataFrame(datediff_list)
output = output.rename(columns={0: "Registrant ID", 1: "TIMESTAMP", 2: "Time_Now", 3: "Days", 4: "Hours", 5: "Minutes", 6: "Seconds"})
Alteryx.write(output,1)
Cheers!
NJ
My solution