Summing up datetimes without using pandas

Viewed 35

I have a data set of rain fall in half hour intervals. I want to sum up the rainfall for each day and keep track of how many data points are summed per day to account for data gaps. Then I want to create a new file with a column for the date, a column for the rainfall, and a column for how many data points were available to sum for each day. daily sum is my function that is trying to do this, get data is my function for extracting the data.

def get_data(avrains):
    print('opening{}'.format(avrains))
    with open(avrains, 'r') as rfile:
        header = rfile.readline()
        dates = []
        rainfalls = []
        for line in rfile:
            line = (line.strip())
            row = line.split(',')
            d = datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
            r = row[-1]
            dates.append(d)
            rainfalls.append(float(r))
        data = zip(dates, rainfalls)
        data = sorted(data)
        return (data)

def dailysum(rains):
    day_date = []
    rain_sum = []
    for i in rains:
        dayi = i[0]
        rainsi = i[1]
    for i in dayi:
        try:
            if dayi[i]== dayi[i+1]:
                s= rains[i]+rains[i+1]
                rain_sum.append(float(s))
        except:
            pass
            day_date.append(dayi[i]) 
1 Answers

There's a lot of ways to solve this, but I'll try to stay as close to your existing code as I can:

def get_data(avrains):
    """
    opens the file specified in avrains and returns a dictionary
    keyed by date, containing a 2-tuple of the total rainfall and
    the count of data points, like so:
    {
      date(2018, 11, 1) : (0.25, 6),
      date(2018, 11, 2) : (0.00, 5),
    }
    """
    print('opening{}'.format(avrains))
    rainfall_totals = dict()

    with open(avrains, 'r') as rfile:
        header = rfile.readline()
        for line in rfile:
            line = (line.strip())
            row = line.split(',')
            d = datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
            r = row[-1]

            try:
                daily_rainfall, daily_count = rainfalls[d]
                daily_rainfall += r
                daily_count += 1
                rainfalls[d] = (daily_rainfall, daily_count)
            except KeyError:
                # if we don't find that date in rainfalls, add it
                rainfalls[d] = (r, 1)

    return rainfalls

Now when you call get_data("/path/to/file"), you'll get back a dictionary. You can spit out the values with some thing like this:

foo = get_data("/path/to/file")
for (measure_date, (rainfall, observations)) in foo.items():
    print measure_date, rainfall, observations

(I will leave the formatting of the date, and any sorting or file-writing as an exercise :) )

Related