Search code examples
pythondatetimestrptimestrftime

Appending new items to a Python list with logic operators (Time values)


I'm starting out on Python on a job (I'm used to R) where I have to get daily data from an API that returns the datetime and value (which is a certain number of listeners on a podcast) and then send that data to a bigquery database.

After I split up the date and time, I need to add a new column that indicates which program was playing in that moment. In other words:

if time is >= than 11:00 and <= 11:59 then add a 'program name' value to the row into the column 'program'.

I've ran into several problems, namely the fact that time has been split as strings (could be due to the fact that we use google data studio, which has extremely rigid datetime implementation).

How would you go about it?

 if response.status_code == 200:
     data = response.text
     result = json.loads(data)
     test = result

     #Append Items
     for k in test:
         l = []
         l.append(datetime.datetime.strptime(k["time"], "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y-%m-%d"))
         l.append(datetime.datetime.strptime(k["time"], "%Y-%m-%dT%H:%M:%S.%fZ").astimezone(pytz.timezone("America/Toronto")).strftime("%H:%M"))

         l.append(k["value"])

Solution

  • You need to have a 'DB' of the programs timetable. See below. Your loop will call the function below with the time value and you will have the program name.

    import datetime
    from collections import namedtuple
    
    Program = namedtuple('Program', 'name start end')
    
    PROGRAMS_DB = [Program('prog1', datetime.time(3, 0, 0), datetime.time(3, 59, 0)),
               Program('prog2', datetime.time(18, 0, 0), datetime.time(18, 59, 0)),
               Program('prog3', datetime.time(4, 0, 0), datetime.time(4, 59, 0))]
    
    
    def get_program_name(time_val):
        for program in PROGRAMS_DB:
            if program.start <= time_val <= program.end:
                return program.name
    
    
    data_from_the_web = [{"time": "2019-02-19T18:10:00.000Z", "value": 413, "details": None},
                     {"time": "2019-02-19T15:12:00.000Z", "value": 213, "details": None}]
    
    for entry in data_from_the_web:
        t = datetime.datetime.strptime(entry["time"], "%Y-%m-%dT%H:%M:%S.%fZ").time()
        entry['prog'] = get_program_name(t)
    
    for entry in data_from_the_web:
        print(entry)
    

    Output

    {'prog': 'prog2', 'details': None, 'value': 413, 'time': '2019-02-19T18:10:00.000Z'}
    {'prog': None, 'details': None, 'value': 213, 'time': '2019-02-19T15:12:00.000Z'}