Using datetime to get CSV dates in Python
Question: I am trying to get dates from a csv file to chart a graph, and am having
difficulty getting a method with which to compare the data. The dates are in
the format MM/DD/YYYY HH:MM:SS . I have struggled with finding a method to
perform this task for several days, but my instructor insists using online
resources instead of giving direct assistance for our final project.

I think that I will need to use a for loop to create the 'now' variable, but I
don't know how I could then compare all of the dates under the 'now' variable.
I also don't know how else to insert arguments into the 'then' variable,
because now[n] gives the nth entry for date in the csv file.

Thank you ahead of time for your help, and I will try to organize my post as
much as needed to clarify!

    
    
    """
    Demo of scatter plot with varying marker colors and sizes.
    """
    import numpy as np
    import matplotlib.pyplot as plt
    import os.path
    import random
    import datetime
    from datetime import timedelta
    import time
    
    
    # Load a numpy record array from yahoo csv data with fields date,
    # open, close, volume, adj_close from the mpl-data/example directory.
    # The record array stores python datetime.date as an object array in
    # the date column
    directory = os.path.dirname(os.path.abspath(__file__))
    datafile = os.path.join(directory, 'v2vcomm.csv')
    data = np.recfromcsv(datafile, delimiter=',', filling_values=np.nan, case_sensitive=True, deletechars='', replace_space=' ')
    
    #start = datetime.datetime(np.data(data.date)
    
    
    #datetime.datetime.time.strftime("%y-%m-%d-%H-%M")
    time = data.date
    now = time.replace('/', ' ').replace(':', ' ').split() #having problems turning this into a useable list. It outputs a list containing each individual date as a element, so it isn't helpful to me
    then = datetime.datetime(int(now[2]), int(now[0]), int(now[1]), int(now[3]), int(now[4]), int(now[5])) #trying to input int values for each item in one date, but the formatting is tricky to me
    
    length = len(np.diff(data.msg_id))
    
    #these random values are from an earlier assignment, going to  eliminate them when I get datetime working
    x= []
    z= []
    for v in range(1,length+1):
        x.append(random.randint(0,300))
        z.append(random.randint(1,100))
    y = np.diff(data.msg_id)
    
    count = 1
    
    
    
    
    fig, ax = plt.subplots()
    ax.scatter(x, y, c=z, s=30, alpha=0.5)
    
    ax.set_xlabel(r'Total Time Elasped since  - Seconds (300=5 min)', fontsize=12)
    ax.set_ylabel(r'Impediments', fontsize=12)
    ax.set_title('Traffic Flow Impediment Frequency')
    
    ax.grid(True)
    fig.tight_layout()
    
    plt.show()
    


Answer: You **can** compare Python's `date` and `datetime` object(s).

**Example:**

    
    
    >>> from datetime import date
    >>> d1 = date(2014, 05, 21)
    >>> d2 = date(2014, 05, 20)
    >>> d1 < d2
    False
    >>> d1
    datetime.date(2014, 5, 21)
    >>> d1 > d2
    True
    

See: [datetime](https://docs.python.org/2/library/datetime.html)
Documentation.

Regarding your 2nd problem of insertion into list(s):

    
    
    >>> xs = [0, 1, 2, 4]
    >>> xs.insert(3, 3)
    >>> xs
    [0, 1, 2, 3, 4]
    

See: `help(list)` or `pydoc list` (_on the command line_).

