Advanced Features of HydroPy


In [1]:
# Import the libraries that we'll be using
import numpy as np
import pandas as pd
import hydropy as hp

# Set the notebook to plot graphs in the output cells.
%matplotlib inline

In [2]:
# Create a Pandas dataframe using the USGS daily discharge for Herring Run.
start = '2011-01-01'
end = '2016-01-01'
herring = hp.get_usgs('01585200', 'dv', start, end)
stony = hp.get_usgs('01589464', 'dv', start, end)

Create a timeseries and calculate baseflow


In [3]:
bfc = hp.get_baseflow_chapman(herring, 0.00001)
bfc.head()


Out[3]:
0
datetime
2011-01-01 0.000000
2011-01-02 0.444998
2011-01-03 0.365000
2011-01-04 0.360000
2011-01-05 0.355000

In [4]:
type(bfc)


Out[4]:
pandas.core.frame.DataFrame

In [5]:
bfb = hp.get_baseflow_boughton(herring, .5, 15.0)
bfb.head()


Out[5]:
0
datetime
2011-01-01 0.000000
2011-01-02 0.834375
2011-01-03 0.710449
2011-01-04 0.697202
2011-01-05 0.687413

In [6]:
bfi = hp.get_baseflow_ihacres(herring, 0.1, 15, .5)
bfi.head()


Out[6]:
0
datetime
2011-01-01 0.000000
2011-01-02 1.176562
2011-01-03 1.108916
2011-01-04 1.024118
2011-01-05 1.009526

In [7]:
stony.head()


Out[7]:
value
datetime
2011-01-01 0.53
2011-01-02 0.65
2011-01-03 0.53
2011-01-04 0.54
2011-01-05 0.53

Merge two dataframes


In [8]:
multi = pd.merge(herring, stony, left_index=True, right_index=True, suffixes=('_herring', '_stony'))
multi.head()


Out[8]:
value_herring value_stony
datetime
2011-01-01 0.73 0.53
2011-01-02 0.89 0.65
2011-01-03 0.73 0.53
2011-01-04 0.72 0.54
2011-01-05 0.71 0.53

In [9]:
# Analyze the data using the HydroAnalysis class.
my_analysis = hp.HydroAnalysis(multi)

# Plot discharge on a logarithmic scale for the Y axis.
my_analysis.plot(figsize=(16,6), logy=True)


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a753155dd8>

Mark peak discharges


In [10]:
# We'll need to import pyplot to access the sub-plotfeature
import matplotlib.pyplot as plt

# Create a single figure with a single x-axis.
fig, ax = plt.subplots(figsize=(13, 6))

# Plot our data like normal, but use the special x-axis 'ax'.
my_analysis.plot(ax=ax)

# Plot a new column of data that only includes peaks that are in the 90th percentile;
# Plot the new column using filled circles, and use the axis 'ax' that we created.
my_analysis.get_highpeaks(150, above_percentile=0.9).plot(style='o', ax=ax)


Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x1a75415feb8>

Seasonal Analysis


In [11]:
my_analysis.data.groupby('season').mean()


Out[11]:
value_herring value_stony
season
Autumn 3.749585 3.326787
Spring 3.819626 3.120901
Summer 4.339484 3.673484
Winter 2.909482 2.566353

In [12]:
my_analysis.summary()


Out[12]:
value_herring value_stony
count 1755.000000 1734.000000
mean 3.720598 3.179354
std 10.903775 8.778873
min 0.070000 0.380000
25% 0.740000 0.910000
50% 1.200000 1.200000
75% 2.200000 2.000000
max 179.000000 161.000000

In [ ]: