In [1]:
import pandas as pd

In [2]:
Table = pd.read_csv('C:\Users\Ty Dickinson\Downloads\COOP Stations\Wimberley 1 NW.csv')
Table = Table.replace({'Precipitation': {' T':0, ' M':0}}) #Sets trace and missing rainfall to 0
#Sets values that have an 'A' attached to be zero since we do not know when that rain fell
for i in range(len(Table.Precipitation)):
    try:
        float(Table.Precipitation[i])
    except ValueError:
        Table.Precipitation[i] = 0
Table.Date = pd.to_datetime(Table.Date)
pd.options.mode.chained_assignment = None

#Create lists for precip values
Winter1DayPrecip = []
Spring1DayPrecip = []
Summer1DayPrecip = []
Fall1DayPrecip = []

Winter2DayPrecip = []
Spring2DayPrecip = []
Summer2DayPrecip = []
Fall2DayPrecip = []

Winter3DayPrecip = []
Spring3DayPrecip = []
Summer3DayPrecip = []
Fall3DayPrecip = []

#define dates for seasons boundaries and convert them to datetime for 1 day
Date1 = '12-01-1980'
Date2 = '02-28-1981'
Date3 = '03-01-1981'
Date4 = '05-31-1981'
Date5 = '06-01-1981'
Date6 = '08-31-1981'
Date7 = '09-01-1981'
Date8 = '11-30-1981'
Date1 = pd.to_datetime(Date1)
Date2 = pd.to_datetime(Date2)
Date3 = pd.to_datetime(Date3)
Date4 = pd.to_datetime(Date4)
Date5 = pd.to_datetime(Date5)
Date6 = pd.to_datetime(Date6)
Date7 = pd.to_datetime(Date7)
Date8 = pd.to_datetime(Date8)

#Date boundaries for seasons for 2 day 
Date9 = '12-01-1980'
Date10 = '02-28-1981'
Date11 = '03-01-1981'
Date12 = '05-31-1981'
Date13 = '06-01-1981'
Date14 = '08-31-1981'
Date15 = '09-01-1981'
Date16 = '11-30-1981'
Date9 = pd.to_datetime(Date9)
Date10 = pd.to_datetime(Date10)
Date11 = pd.to_datetime(Date11)
Date12 = pd.to_datetime(Date12)
Date13 = pd.to_datetime(Date13)
Date14 = pd.to_datetime(Date14)
Date15 = pd.to_datetime(Date15)
Date16 = pd.to_datetime(Date16)

Winter1Count = 0
Spring1Count = 0
Summer1Count = 0
Fall1Count = 0

Winter2Count = 0
Spring2Count = 0
Summer2Count = 0
Fall2Count = 0


#set the index of the table to the date column for df.loc
df = Table.set_index(['Date'])

Table.Precipitation = Table.Precipitation.astype(float)

#while statements to calculate one day max precipitation
while Winter1Count <=36:
    x = df.loc[Date1:Date2]
    y = x.Precipitation.max()
    float(y)
    Winter1DayPrecip.insert(Winter1Count, y)
    Date1 = Date1 + pd.DateOffset(years=1)
    Date2 = Date2 + pd.DateOffset(years=1)
    Winter1Count = Winter1Count + 1

while Spring1Count <=36:
    x = df.loc[Date3:Date4]
    y = x.Precipitation.max()
    float(y)
    Spring1DayPrecip.insert(Spring1Count, y)
    Date3 = Date3 + pd.DateOffset(years=1)
    Date4 = Date4 + pd.DateOffset(years=1)
    Spring1Count = Spring1Count + 1

while Summer1Count <=36:
    x = df.loc[Date5:Date6]
    y = x.Precipitation.max()
    float(y)
    Summer1DayPrecip.insert(Summer1Count, y)
    Date5 = Date5 + pd.DateOffset(years=1)
    Date6 = Date6 + pd.DateOffset(years=1)
    Summer1Count = Summer1Count + 1

while Fall1Count <=36:
    x = df.loc[Date7:Date8]
    y = x.Precipitation.max()
    float(y)
    Fall1DayPrecip.insert(Fall1Count, y)
    Date7 = Date7 + pd.DateOffset(years=1)
    Date8 = Date8 + pd.DateOffset(years=1)
    Fall1Count = Fall1Count + 1


    
#Maximum rainfall 2 day totals
while Winter2Count <=36:
    x = df.loc[Date9:Date10]
    x.Precipitation = x.Precipitation.astype(float)
    MaxPrecip = 0
    for i in range((len(x.Precipitation.index)-1)):
        a = x.Precipitation[i] + x.Precipitation[i+1]
        if a > MaxPrecip:
            MaxPrecip = a
        i = i+1
    Winter2DayPrecip.insert(Winter2Count, MaxPrecip)
    if Date10.is_leap_year:
        Date10 = Date10 - pd.DateOffset(days=1)
    Date9 = Date9 + pd.DateOffset(years=1)
    Date10 = Date10 + pd.DateOffset(years=1)
    if Date10.is_leap_year:
        Date10 = Date10 + pd.DateOffset(days=1)
    Winter2Count = Winter2Count + 1

while Spring2Count <=36:
    x = df.loc[Date11:Date12]
    x.Precipitation = x.Precipitation.astype(float)
    MaxPrecip = 0
    for i in range((len(x.Precipitation.index)-1)):
        a = x.Precipitation[i] + x.Precipitation[i+1]
        if a > MaxPrecip:
            MaxPrecip = a
        i = i+1
    Spring2DayPrecip.insert(Spring2Count, MaxPrecip)
    Date11 = Date11 + pd.DateOffset(years=1)
    Date12 = Date12 + pd.DateOffset(years=1)
    Spring2Count = Spring2Count + 1 

while Summer2Count <=36:
    x = df.loc[Date13:Date14]
    x.Precipitation = x.Precipitation.astype(float)
    MaxPrecip = 0
    for i in range((len(x.Precipitation.index)-1)):
        a = x.Precipitation[i] + x.Precipitation[i+1]
        if a > MaxPrecip:
            MaxPrecip = a
        i = i+1
    Summer2DayPrecip.insert(Summer2Count, MaxPrecip)
    Date13 = Date13 + pd.DateOffset(years=1)
    Date14 = Date14 + pd.DateOffset(years=1)
    Summer2Count = Summer2Count + 1

while Fall2Count <=36:
    x = df.loc[Date15:Date16]
    x.Precipitation = x.Precipitation.astype(float)
    MaxPrecip = 0
    for i in range((len(x.Precipitation.index)-1)):
        a = x.Precipitation[i] + x.Precipitation[i+1]
        if a > MaxPrecip:
            MaxPrecip = a
        i = i+1
    Fall2DayPrecip.insert(Fall2Count, MaxPrecip)
    Date15 = Date15 + pd.DateOffset(years=1)
    Date16 = Date16 + pd.DateOffset(years=1)
    Fall2Count = Fall2Count + 1

#Creating dataframes for each seasons 1 and 2 day precip
df1 = pd.DataFrame({'Winter 1 Day Precip': Winter1DayPrecip})
df2 = pd.DataFrame({'Spring 1 Day Precip': Spring1DayPrecip})
df3 = pd.DataFrame({'Summer 1 Day Precip': Summer1DayPrecip})
df4 = pd.DataFrame({'Fall 1 Day Precip': Fall1DayPrecip})
df5 = pd.DataFrame({'Winter 2 Day Precip': Winter2DayPrecip})
df6 = pd.DataFrame({'Spring 2 Day Precip': Spring2DayPrecip})
df7 = pd.DataFrame({'Summer 2 Day Precip': Summer2DayPrecip})
df8 = pd.DataFrame({'Fall 2 Day Precip': Fall2DayPrecip})
#concatenate the dataframes together, axis=1 to do it by column and then export to csv
dftot = pd.concat([df1, df2, df3, df4, df5, df6, df7, df8], axis=1)
dftot.to_csv('Wimberley 1 NW.csv')

In [ ]: