Skip to content

Instantly share code, notes, and snippets.

@drheinheimer
Created October 3, 2018 20:55
Show Gist options
  • Save drheinheimer/cbdff3c82e5ba0b861f7c9c706fdca26 to your computer and use it in GitHub Desktop.
Save drheinheimer/cbdff3c82e5ba0b861f7c9c706fdca26 to your computer and use it in GitHub Desktop.
import pandas as pd
from math import ceil
for region in ['CherryEleanor', 'DonPedro', 'Hetchy']:
# define input and output paths
inpath = 'baserun/{}/statvar.csv'.format(region)
outpath = 'baserun/{}/10day.csv'.format(region)
# read in the original csv
df = pd.read_csv(inpath, index_col=0, parse_dates=True)
# add indices
df['year'] = [dt.year for dt in df.index]
df['month'] = [dt.month for dt in df.index]
df['10day'] = [min(ceil(dt.day/10),3) for dt in df.index]
df.reset_index(drop=True, inplace=True)
df.set_index(['year', 'month', '10day'], inplace=True)
# aggregate by 10-day period to sum and/or mean and add to new dataframe
# dfsum = df.groupby(by=['year', 'month', '10day']).sum()
dfmean = df.groupby(by=['year', 'month', '10day']).mean()
out = pd.DataFrame(index=dfmean.index)
out['runoff'] = dfmean['basin_cfs']
out['swe'] = dfmean['basin_pweqv']
# write results
out.to_csv(outpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment