<jupyter_start><jupyter_text>HAWAIIAN VACATION WEATHER ANALYSIS<jupyter_code>%matplotlib inline
from matplotlib import style
style.use('fivethirtyeight')
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from scipy.stats import stats
from statistics import mean
import datetime as dt<jupyter_output><empty_output><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
engine = create_engine("sqlite:///Resources/hawaii.sqlite")
# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
# We can view all of the classes that automap found
Base.classes.keys()
# Save references to each table
Measurement = Base.classes.measurement
Station = Base.classes.station
# Create our session (link) from Python to the DB
session = Session(engine)
conn = engine.connect()<jupyter_output><empty_output><jupyter_text>Exploratory Climate Analysis<jupyter_code># Review the information in the Measurement table
first_row = session.query(Measurement).first()
# View the query object as a dictionary and view column names and values for a single row
first_row.__dict__
# Review the information in the Station table
first_row = session.query(Station).first()
# View the query object as a dictionary and view column names and values for a single row
first_row.__dict__
# 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
last_date = session.query(func.max(Measurement.date)).scalar()
last_date

query_date = dt.datetime.strptime(last_date, '%Y-%m-%d') - dt.timedelta(days=365)

# Perform a query to retrieve the data and precipitation scores
last_year = session.query(Measurement.date, Measurement.prcp).\
    filter(Measurement.date >= query_date).all()

# Save the query results as a Pandas DataFrame and set the index to the date column
df = pd.DataFrame(last_year)

# Set the index to the date column
df.set_index('date', inplace=True)

# Rename the column
df.rename(columns = {'prcp':'precipitation'}, inplace = True)

# Sort the dataframe by date
df.sort_index(inplace=True)


# Use Pandas Plotting with Matplotlib to plot the data
ax3 = df.plot(figsize=(10,8))
             
# Suppress the xtick labels by passing an empty list (otherwise it is too crowded with so many dates)
plt.xticks(ticks=[],labels=None)
plt.savefig('Images/PandasPrecipitation.png')
plt.show()
# Using the last date from above perform a query to retrieve the last 12 months of precipitation data
# and the dates and save query results as a Pandas Dataframe - using sqlalchemy method to get data
query_date = dt.datetime.strptime(last_date, '%Y-%m-%d') - dt.timedelta(days=365)

# Generate a query object
last_year = session.query(Measurement.date, Measurement.prcp).\
    filter(Measurement.date >= query_date).all()

# Put query object results into a Pandas dataframe
df = pd.DataFrame(last_year)

# Set the index to the date column
df.set_index('date', inplace=True)

# Rename the column
df.rename(columns = {'prcp':'precipitation'}, inplace = True) 
             
# Sort the dataframe by date
df.sort_index(inplace=True)

# Plot the data using pandas.plot method
ax3 = df.plot(figsize=(10,8))
             
# Suppress the xtick labels by passing an empty list (otherwise it is too crowded with so many dates)
plt.xticks(ticks=[],labels=None)
plt.savefig('Images/PandasPrecipitation.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?
num_stations = session.query(Station.station).\
                             group_by(Station.station).count()
print(f'The number of unique stations in this database is: {num_stations}')
# What are the most active stations? (i.e. what stations have the most rows)?
# List the stations and the counts in descending order.
most_active = session.query(Measurement.station,func.count(Measurement.station)).\
    filter(Station.station == Measurement.station).group_by(Measurement.station).\
    order_by(func.count(Station.station).desc()).all()
print('The following stations have the most rows in desc order:')
most_active
# Using the station id from the previous query, calculate the lowest temperature recorded, 
# highest temperature recorded, and average temperature of the most active station?
low_hi_avg = session.query(func.min(Measurement.tobs),func.max(Measurement.tobs),\
    func.avg(Measurement.tobs)).filter(Measurement.station == 'USC00519281').first()
low_hi_avg
# Get the last year of temperature observations (tobs) from the most active station and put into
# a Pandas dataframe
last_year_tobs = pd.read_sql("SELECT tobs FROM measurement where \
    station = 'USC00519281' AND date >= DATE('2017-08-23','-12 month')", conn)
#last_year_tobs
# 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
# Choose the station with the highest number of temperature observations.
# Query the last 12 months of temperature observation data for this station

last_year_tobs = pd.read_sql("SELECT tobs FROM measurement where \
    station = 'USC00519281' AND date >= DATE('2017-08-23','-12 month')", conn)

# Plot the results as a histogram

num_bins = 12
fig, ax = plt.subplots(figsize=(10,7))

n, bins, patches = ax.hist(last_year_tobs['tobs'], num_bins)

ax.set_ylabel('Frequency')
ax.legend(['tobs'],loc='best')

plt.savefig('Images/Histogram.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Bonus Challenge Assignment  Temperature Analysis I Hawaii is reputed to enjoy mild weather all year. Is there a meaningful difference between the temperature in, for example, June and December?<jupyter_code># Determine the average temperature for June
june_temps = session.query(Measurement.tobs).\
        filter(Measurement.date.like("%-06-%")).all()
june_avg_temp = session.query(func.avg(Measurement.tobs)).\
        filter(Measurement.date.like("%-06-%")).scalar()
print(f"Avg June temp for all stations and all years is: {round(june_avg_temp,2)} F")
# Determine the average temperature for December
dec_temps = session.query(Measurement.tobs).\
        filter(Measurement.date.like("%-12-%")).all()
dec_avg_temp = session.query(func.avg(Measurement.tobs)).\
        filter(Measurement.date.like("%-12-%")).scalar()
print(f"Avg December temp for all stations and all years is: {round(dec_avg_temp,2)} F")<jupyter_output>Avg December temp for all stations and all years is: 71.04 F<jupyter_text>Use the t-test to determine whether the difference in the means, if any, is statistically significant. Will you use a paired t-test, or an unpaired t-test? Why?A paired t-test will be used because the same thing is being measured multiple times.<jupyter_code># Generate function to do a t-test plot pair
def ttestplot(pop1, pop2):
    # Scatter Plot of Data
    plt.subplot(2, 1, 1)
    plt.scatter(range(len(pop1)), pop1, label="pop1")
    plt.scatter(range(len(pop2)), pop2, label="pop2")
    plt.legend()

    # Histogram Plot of Data
    plt.subplot(2, 1, 2)
    plt.hist(pop1, 10, density=True, alpha=0.7, label="pop1")
    plt.hist(pop2, 10, density=True, alpha=0.7, label="pop2")
    plt.axvline(mean(pop1), color='k', linestyle='dashed', linewidth=1)
    plt.axvline(mean(pop2), color='k', linestyle='dashed', linewidth=1)
    plt.savefig('Images/ttest_plot.png')
    plt.legend()
# Convert the list of tuples into a list for each observed month
june_temp_list = [item for t in june_temps for item in t] 
dec_temp_list = [item for t in dec_temps for item in t]
# Call the ttestplot function to visualize the data
ttestplot(june_temp_list,dec_temp_list)
# Run a t-test between the June temps and Dec temps
stats.ttest_ind(june_temp_list,dec_temp_list, equal_var=False)<jupyter_output><empty_output><jupyter_text>The t-test has a large F statistic AND a VERY SMALL p value so the June and December average temperatures are statistically different from one another.¶  Temperature Analysis II Calculate the min, avg, and max temperatures for your trip using the matching dates from the previous yearPlot the min, avg, and max temperature from your previous query as a bar chart.Use the average temperature as the bar height.Use the peak-to-peak (tmax-tmin) value as the y error bar (yerr).<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('2010-02-28', '2010-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.
min_avg_max = calc_temps('2017-02-22', '2017-03-01')
min_avg_max
#Store the information in a data frame
trip_temp_df = pd.DataFrame(min_avg_max, columns=["Min Temp.", "Avg Temp.", "Max Temp."])
trip_temp_df
# 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)

#Calculate the error
tmin = 61
tmax = 69.7
tavg = 77
error = tmax-tmin

# Plot the bar using the error
fig, ax = plt.subplots(figsize=(5,8))
ax.bar(1,tavg, yerr=error, align='center', alpha=0.5, ecolor='black', capsize=10)
ax.set_ylabel('Temp (F)')
ax.set_xticklabels([])
ax.set_title('Trip Avg Temp')
ax.yaxis.grid(True)

# Save the figure and show
plt.savefig('Images/bar_plot_with_error_bars.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Daily Rainfall Average<jupyter_code># 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

rain_station = pd.read_sql("SELECT s.station,s.name,s.latitude,s.longitude,s.elevation,sum(m.prcp) as 'Total Rainfall'\
                            FROM station s\
                            JOIN measurement m ON(s.station = m.station)\
                            WHERE m.date BETWEEN '2017-02-22' AND '2017-03-01'\
                            GROUP BY s.station\
                            ORDER BY SUM(m.prcp) DESC" , conn)
rain_station.head(10)
# 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
# trip dates 
trip_dates = ['02-22','02-23','02-24','02-25','02-26','02-27','02-28','03-01']


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

# Set the start and end date of the trip
start_vacation = dt.date(2018,2,22)

numdays = 8

# Strip off the year and save a list of %m-%d strings
date_list = [str(start_vacation + dt.timedelta(days=x))[5:] for x in range(numdays)]
print(date_list)
# Loop through the list of %m-%d strings and calculate the normals for each date
normals = [daily_normals(month_day) for month_day in date_list]
normals
# using list comprehension 
# convert list of tuples to list of list 
normals_list = [list(elements) for elements in normals]
normals_list
#Load the list of daily normals into a Pandas DataFrame and set the index equal to the date.
normal_temp_df = pd.DataFrame(normals_list,columns=['tmin','tavg','tmax']).rename_axis('date')
normal_temp_df.head()

# Plot the daily normals as an area plot with `stacked=False`
normal_temp_df.plot.area(stacked=False)
plt.xlabel('Date')
plt.ylabel('Temperature')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1020675821632464, 'fraction_numerical': 0.017358432340688165, 'mean_word_length': 4.506796941376381, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '344936', 'license': 'ADSL', 'n_tokens_mistral': 4225, 'n_tokens_neox': 3983, 'n_words': 1389, 'path': 'climate_starter.ipynb', 'repo_name': 'Lareena/sqlalchemy-challenge', 'size': '12962'}