<jupyter_start><jupyter_text>Reflect Tables into SQLAlchemy ORM<jupyter_code># Python SQL toolkit and Object Relational Mapper
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect
engine = create_engine("sqlite:///Resources/hawaii.sqlite")
# reflect an existing database into a new model

# Declare a Base using `automap_base()`
Base = automap_base()

# reflect the tables

# Use the Base class to reflect the database tables
Base.prepare(engine, reflect=True)
# We can view all of the classes that automap found

# Print all of the classes mapped to the Base
Base.classes.keys()
# Save references to each table

# Assign the measurement class to a variable called `Measurement`
Measurement = Base.classes.measurement

# Assign the station class to a variable called `Station`
Station = Base.classes.station
# Create our session (link) from Python to the DB

# Create a session
session = Session(engine)
# Display the Measurement class row's columns and data in dictionary format
first_row = session.query(Measurement).first()
first_row.__dict__
# Display the Station class row's columns and data in dictionary format
first_row = session.query(Station).first()
first_row.__dict__
inspector = inspect(engine)
inspector.get_table_names()
# Get a list of column names and types
columns_measurement = inspector.get_columns('measurement')
for c in columns_measurement:
    print(c['name'], c["type"])
# Get a list of column names and types
columns_station = inspector.get_columns('station')
for c in columns_station:
    print(c['name'], c["type"])<jupyter_output>id INTEGER
station TEXT
name TEXT
latitude FLOAT
longitude FLOAT
elevation FLOAT<jupyter_text>Exploratory Climate Analysis<jupyter_code># Design a query to retrieve the last 12 months of precipitation data and plot the results

# Calculate the date 1 year ago from the last data point in the database

# Perform a query to retrieve the data and precipitation scores

random_date = dt.datetime(2011, 5, 31)
print(random_date)
today = dt.date.today()
print(today)

# Save the query results as a Pandas DataFrame and set the index to the date column

# Sort the dataframe by date

# Use Pandas Plotting with Matplotlib to plot the data

results_last_date = session.query(Measurement.date, Measurement.prcp)\
                    .order_by(Measurement.date.desc()).first()
results_last_date
last_date = pd.to_datetime(results_last_date[0])
print(last_date)

# date 1 year ago from last_date
first_date = last_date - dt.timedelta(days=365)
print(first_date)

last_date = last_date.date()
first_date = first_date.date()
results = session.query(Measurement.date, Measurement.prcp)\
                        .filter(Measurement.date.between(first_date, last_date)).all()
results
df = pd.DataFrame(results, columns=['date', 'prcp'])
df['date'] = pd.to_datetime(df['date'])
df
df.set_index('date', inplace=True)
df.plot(kind = 'line', figsize=(12, 8), color='#807060')
plt.title("Daily prcp for last 12 months")
plt.xlabel("Year-Month-Day")
plt.ylabel("Daily PRCP (inches)")
plt.tight_layout()
plt.show()
df_copy = pd.DataFrame(results, columns=['date', 'prcp'])
df_copy['date'] = pd.to_datetime(df_copy['date'])
df_copy
df_copy['month_year'] = df_copy['date'].dt.to_period("M") 
df_copy
#df.set_index('date', inplace=True)
gdf = df_copy.groupby(df_copy.month_year).sum()
gdf
gdf.plot(kind = 'bar', figsize=(12, 8), color='#808080')
plt.title("Total monthly prcp for last 12 months")
plt.xlabel("Year-Month")
plt.ylabel("Total Monthly PRCP")
plt.tight_layout()
plt.tight_layout()
plt.savefig("plot1.png")
plt.show()
# Use Pandas to calcualte the summary statistics for the precipitation data
df.describe()
# Design a query to show how many stations are available in this dataset?
unique_stations_list = session.query(Measurement.station, func.avg(Measurement.tobs))\
                        .group_by(Measurement.station)\
                        .order_by(Measurement.station).all()
print("Stations in dataset with average TOBS per station")
print(f"Number of Stations in the dataset: {len(unique_stations_list)}")
unique_stations_list
# What are the most active stations? (i.e. what stations have the most rows)?
# List the stations and the counts in descending order.
stations_list = session.query(Measurement.station, func.count(Measurement.tobs))\
                        .group_by(Measurement.station)\
                        .order_by(func.count(Measurement.tobs).desc()).all()
print("Stations with most rows in the descending order")
stations_list
# Using the station id from the previous query, calculate the lowest temperature recorded, 
# highest temperature recorded, and average temperature of the most active station?
sel = [Measurement.station, 
       func.count(Measurement.tobs),
       func.min(Measurement.tobs), 
       func.max(Measurement.tobs), 
       func.avg(Measurement.tobs)]

stations_list_stats = session.query(*sel)\
                        .group_by(Measurement.station)\
                        .order_by(func.count(Measurement.tobs).desc()).all()
print("Statistics of Temperature for Stations with activity volume in descending order")
stations_list_stats
# Choose the station with the highest number of temperature observations.
# Query the last 12 months of temperature observation data for this station and plot the results as a histogram
most_active_station = stations_list_stats[0][0]
print(most_active_station)
results_top_station = session.query(Measurement.station, Measurement.date, Measurement.tobs)\
                        .filter(Measurement.station == most_active_station)\
                        .filter(Measurement.date.between(first_date, last_date)).all()
results_top_station
df_temp = pd.DataFrame(results_top_station, columns=['station','date', 'tobs'])
df_temp['date'] = pd.to_datetime(df_temp['date'])
df_temp.drop(['station'], axis = 1, inplace = True)
df_temp
df_temp.set_index('date', inplace=True)
ticks = np.arange(df_temp.tobs.min(), df_temp.tobs.max())
df_temp.hist(figsize=(12, 8), bins='auto', color='#0504aa', alpha=0.7, rwidth=0.85)
plt.title(f"Histogram of Temps for station {most_active_station}")
plt.xlabel("Temperatures")
plt.ylabel("Number of Days(Freq)")
plt.xticks(ticks)
plt.tight_layout()
plt.tight_layout()
plt.savefig("plot2.png")
plt.show()
df_temp_copy = pd.DataFrame(results_top_station, columns=['station','date', 'tobs'])
df_temp_copy['date'] = pd.to_datetime(df_temp_copy['date'])
df_temp_copy.drop(['station'], axis = 1, inplace = True)
df_temp_copy
df_temp_copy['month_year'] = df_temp_copy['date'].dt.to_period("M") 
df_temp_copy
gdf_temp = df_temp_copy.groupby(df_temp_copy.month_year).mean()
gdf_temp
gdf_temp.plot(kind = 'bar', figsize=(12, 8), color='#607080')
plt.title("Average monthly Temps for last 12 months")
plt.xlabel("Year-Month")
plt.ylabel("Average Monthly Temp")
plt.tight_layout()
plt.tight_layout()
plt.savefig("plot3.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Bonus Challenge Assignment<jupyter_code># This function called `calc_temps` will accept start date and end date in the format '%Y-%m-%d' 
# and return the minimum, average, and maximum temperatures for that range of dates
def calc_temps(start_date, end_date):
    """TMIN, TAVG, and TMAX for a list of dates.
    
    Args:
        start_date (string): A date string in the format %Y-%m-%d
        end_date (string): A date string in the format %Y-%m-%d
        
    Returns:
        TMIN, TAVE, and TMAX
    """
    
    return session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
        filter(Measurement.date >= start_date).filter(Measurement.date <= end_date).all()

# function usage example
print(calc_temps('2012-02-28', '2012-03-05'))
# Use your previous function `calc_temps` to calculate the tmin, tavg, and tmax 
# for your trip using the previous year's data for those same dates.
print("What will be weather expected for your trip? Want to find out...\n")
trip_start_date = input ("Start Date of your trip (YYYY-MM-DD):")
trip_end_date = input ("End Date of your trip (YYYY-MM-DD):")
# find dates 1 year ago from provided trip dates
print("Search will be initiated for stats from same dates previous year...\n")
trip_start_date = pd.to_datetime(trip_start_date)
start_date = trip_start_date - dt.timedelta(days=365)
print(f"Historical start date: {start_date}")
trip_end_date = pd.to_datetime(trip_end_date)
end_date = trip_end_date - dt.timedelta(days=365)
print(f"Historical end date: {end_date}")
historical_data = calc_temps(start_date.date(), end_date.date())
print(f"TMIN, TAVG, TMAX: {historical_data}")
# Plot the results from your previous query as a bar chart. 
# Use "Trip Avg Temp" as your Title
# Use the average temperature for the y value
# Use the peak-to-peak (tmax-tmin) value as the y error bar (yerr)

bars = [historical_data[0][1], historical_data[0][2]-historical_data[0][0]]
print(f"Bar heights: {bars}")
df_hist = pd.DataFrame({'Hist Avg Temp':bars[0],
                       'Hist Temp Error':bars[1]}, index = [0])
df_hist
df_hist.plot(kind = 'bar', figsize=(6, 6), color=['#103050','#503010'], width = 0.5 )
plt.title("Trip Avg Temp")
plt.xlabel("Historical Parameters")
plt.ylabel("Temperature (deg F)")
plt.xticks([-0.125,0.125], ('Hist Avg Temp','Hist Temp Error'))
plt.tight_layout()
plt.savefig("plot4.png")
plt.show()
df_hist['Hist Avg Temp'].plot(kind = 'bar', figsize=(6, 6), color='#908070', width = 0.25, yerr=df_hist['Hist Temp Error'])
plt.title("Trip Avg Temp")
plt.xlabel("Trip Historical Parameter")
plt.ylabel("Temperature (deg F)")
plt.xticks([0], ['Hist Avg Temp'])
plt.tight_layout()
plt.savefig("plot5.png")
plt.show()
# Calculate the total amount of rainfall per weather station for your trip dates using the previous year's matching dates.
# Sort this in descending order by precipitation amount and list the station, name, 
# latitude, longitude, and elevation
combo_list = session.query(Measurement.station, func.sum(Measurement.prcp), \
                            Station.name, Station.latitude, Station.longitude,\
                            Station.elevation)\
                            .filter(Measurement.station == Station.station)\
                            .group_by(Measurement.station)\
                            .order_by(Measurement.prcp.desc()).all()
combo_list
# Create a query that will calculate the daily normals 
# (i.e. the averages for tmin, tmax, and tavg for all historic data matching a specific month and day)

def daily_normals(date):
    """Daily Normals.
    
    Args:
        date (str): A date string in the format '%m-%d'
        
    Returns:
        A list of tuples containing the daily normals, tmin, tavg, and tmax
    
    """
    
    sel = [func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)]
    return session.query(*sel).filter(func.strftime("%m-%d", Measurement.date) == date).all()
    
daily_normals("01-01")
# calculate the daily normals for your trip

# push each tuple of calculations into a list called `normals`

# Set the start and end date of the trip

# Use the start and end date to create a range of dates

# Strip off the year and save a list of %m-%d strings

# Loop through the list of %m-%d strings and calculate the normals for each date

print("What will be daily temps expected for your trip? Want to find out...\n")
trip_start_date = input ("Start Date of your trip (YYYY-MM-DD):")
trip_end_date = input ("End Date of your trip (YYYY-MM-DD):")
drange = pd.to_datetime(trip_end_date) - pd.to_datetime(trip_start_date)
print(drange)
drange = int(drange.days)
drange

range_of_dates = []
for day in range(drange):
    days_to_add = dt.timedelta(days=day)
    range_of_dates.append(pd.to_datetime(trip_start_date) + days_to_add)
    range_of_dates[day] = range_of_dates[day].to_pydatetime()
range_of_dates
range_of_dates_trimmed = []
for row in range_of_dates:
    range_of_dates_trimmed.append(row.strftime("%m-%d"))
range_of_dates_trimmed
list_tuple_daily_normals = []
for row in range_of_dates_trimmed:
    #print(daily_normals(row)[0])
    list_tuple_daily_normals.append(daily_normals(row)[0])
list_tuple_daily_normals 
# Load the previous query results into a Pandas DataFrame and add the `trip_dates` range 
# as the `date` index
df_normals = pd.DataFrame(list_tuple_daily_normals, columns=['tmin','tavg', 'tmax'])
df_normals['date'] = range_of_dates_trimmed
#df_temp_copy['date'] = pd.to_datetime(df_temp_copy['date'])
#df_temp_copy.drop(['station'], axis = 1, inplace = True)
df_normals
df_normals.set_index('date', inplace=True)
df_normals
# Plot the daily normals as an area plot with `stacked=False`
ax = df_normals.plot(figsize=(12,6))
#ax = df_normals.plot(x="date", y=["tmin","tavg","tmax"], kind="bar", figsize=(12, 8)).area(stacked=False)
#df_normals.plot(x="date", y="tmin", kind="bar", ax=ax, color="C2")
#df_normals.plot(x="date", y="tmax", kind="bar", ax=ax, color="C3")
plt.title("Trip Normals (Min, Avg, Max) Temps")
plt.xlabel("Dates (MM-DD)")
plt.ylabel("Temperature (deg F)")
plt.tight_layout()
plt.tight_layout()
plt.savefig("plot6.png")
plt.show()
# Plot the daily normals as an area plot with `stacked=False`
ax = df_normals.plot.area(stacked=False, figsize=(12,6))
#ax = df_normals.plot(x="date", y=["tmin","tavg","tmax"], kind="bar", figsize=(12, 8)).area(stacked=False)
#df_normals.plot(x="date", y="tmin", kind="bar", ax=ax, color="C2")
#df_normals.plot(x="date", y="tmax", kind="bar", ax=ax, color="C3")
plt.title("Trip Normals (Min, Avg, Max) Temps")
plt.xlabel("Dates (MM-DD)")
plt.ylabel("Temperature (deg F)")
plt.tight_layout()
plt.savefig("plot7.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Bonus- Temperature Analysis 1<jupyter_code># Create a query that will return the daily temperatures
# for all historic data matching a specific month

def monthly_normals(date):
    """daily temperature for month specified.
    
    Args:
        date (str): A date string in the format '%m'
        
    Returns:
        A list of tuples containing the daily temp.
    
    """
    
    sel = [Measurement.tobs]
    return session.query(*sel).filter(func.strftime("%m", Measurement.date) == date)\
    .limit(1500).all()

# test function for February
monthly_normals("02")
# Using function monthly_normals(), get tenmperatures for June and December
june_temps = monthly_normals('06')
dec_temps = monthly_normals('12')

print(len(june_temps))
print(len(dec_temps))
june_temps = np.ravel(june_temps)
dec_temps = np.ravel(dec_temps)
#print(june_temps)
temp_df = pd.DataFrame({"June": june_temps,
                       "Dec": dec_temps})
temp_df.head()
temp_df.describe()
temp_df =  temp_df.sample(n=150)
temp_df.describe()
stats.shapiro(june_temps)
stats.shapiro(dec_temps)<jupyter_output><empty_output><jupyter_text>Which t-test to choose? The data is coming from same 9 Hawaii stations(same stations & same locations of the stations) for two different periods of time). These are same subjects (dependent) which is why I choose "paired" t-test. The distraibution of each sample is also "normally" distributed as shown below.<jupyter_code>#stats.ttest_ind(june_temps,dec_temps)
# Perform paired t-test on two samples
stats.ttest_rel(june_temps,dec_temps)
_,p_value = stats.ttest_rel(june_temps,dec_temps)
p_value
if p_value < 0.05:
    print("We reject null hypothesis!")
    print ("Null Hypothesis was - there is NO statistical difference in means of two populations")
    print("Conclusion - the means of the two samples are significantly different")
else:
    print("We accept null hypothesis!")
    print ("Null Hypothesis was - there is NO statistical difference in means of two populations")
    print("Conclusion - the means of the two samples are NOT significantly different")
temp_df.corr()
sns.pairplot(temp_df)
def plot_distribution(input_data):
    plt.figure()
    ax = sns.distplot(input_data)
    plt.axvline(np.mean(input_data), color='k', linestyle='dashed', linewidth=5)
    _, max_ = plt.ylim()
    plt.text(
            input_data.mean() + input_data.mean() /10,
            max_ -  max_ / 10,
            "Mean: {:.2f}".format(input_data.mean()),
    )
    return plt.figure

plot_distribution(temp_df['June'])
plot_distribution(temp_df['Dec'])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup