# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernel_info:
#     name: python3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import warnings
warnings.filterwarnings('ignore')

# %matplotlib inline
from matplotlib import style
style.use('fivethirtyeight')
import matplotlib.pyplot as plt

import numpy as np
import pandas as pd

import datetime as dt

# # Reflect Tables into SQLAlchemy ORM

# +
# 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 modelBase = automap_base()
Base = automap_base()
Base.prepare(engine, reflect=True)
Base.classes.keys()

# reflect the tables
# -

# We can view all of the classes that automap found


# 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)

# # Exploratory Climate Analysis

# +
# Design a query to retrieve the last 12 months of precipitation data and plot the results

last_year=dt.date(2017,8,23)-dt.timedelta(days=365)
last_year

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

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

type(result)

# Save the query results as a Pandas DataFrame and set the index to the date column
prcp_df=pd.DataFrame(result,columns=['Date','prcp'])
prcp_df.head()

prcp_df=prcp_df.sort_values(by='Date')
prcp_df.head()

# +
# Use Pandas Plotting with Matplotlib to plot the data
# -

prcp_df.plot(x='Date', y='prcp', rot=90)
plt.show()

# Use Pandas to calcualte the summary statistics for the precipitation data
prcp_df['prcp'].describe()

# # Station

# Design a query to show how many stations are available in this dataset?
from sqlalchemy import distinct
session.query(func.count(distinct(Measurement.station)))


# +
# What are the most active stations? (i.e. what stations have the most rows)?
# List the stations and the counts in descending order.

session.query(Measurement.station,func.count(Measurement.station)).\
    group_by(Measurement.station).\
    order_by(func.count(Measurement.station).desc()).all()

# +
# Using the station id from the previous query, calculate the lowest temperature recorded, 
# highest temperature recorded, and average temperature of the most active station?

session.query(func.min(Measurement.tobs),func.max(Measurement.tobs),func.avg(Measurement.tobs)).\
                  filter(Measurement.station == 'USC00519281').all() 

# -

# 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


temp_result=session.query(Measurement.tobs).\
            filter(Measurement.station == 'USC00519281').\
            filter(Measurement.date>=last_year).all()
df=pd.DataFrame(temp_result, columns=['tobs'])
df.plot.hist(bins=12)


# +
# 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.

start_trip=dt.date(2016,12,1)-dt.timedelta(days=365)
end_trip=dt.date(2016,12,7)-dt.timedelta(days=365)


tmin, tavg, tmax =calc_temps(start_trip, end_trip)[0]

print(tmin, tavg, tmax)

# +
# 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)

fig, ax =plt.subplots(figsize= plt.figaspect(2.))
x = 1
y_err=tmax-tmin
bar_chart = ax.bar(x,tmax,yerr=y_err, alpha=0.5, color='coral', align='center')
ax.set(xticks=range(x),xticklabels="a", title="Trip Avg Temp", ylabel="Temperature F")
ax.margins(.2,.2)
fig.tight_layout()


# +
# 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

start_date='2016-12-01'
end_date='2016-12-07'
sel=[Station.station, Station.name,Station.latitude, Station.longitude, Station.elevation,func.sum(Measurement.prcp)]
result=session.query(*sel).filter(Measurement.station==Station.station).\
        filter(Measurement.date>=start_date).\
        filter(Measurement.date>=end_date).group_by(Station.name).order_by(func.sum(Measurement.prcp).desc()).all()
              
result


# +
# 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
start_date='2016-12-01'
end_date='2016-12-07'
# Use the start and end date to create a range of dates
trip_dates=pd.date_range(start_date, end_date, freq='D')

# Stip off the year and save a list of %m-%d strings
trip_month_date=trip_dates.strftime('%m-%d')

# Loop through the list of %m-%d strings and calculate the normals for each date
normals=[]
for date in trip_month_date:
    daily_data = daily_normals(date)
    normals.append(*daily_data)
    
normals

# +
# Load the previous query results into a Pandas DataFrame and add the `trip_dates` range as the `date` index

temp_df=pd.DataFrame(normals,columns=['Min_Temp','Avg_Temp','Max_Temp'])
temp_df['Date'] = trip_dates
temp_df.set_index('Date', inplace=True)
temp_df.head()
# -

# Plot the daily normals as an area plot with `stacked=False`
temp_df.plot(kind='area', stacked=False, x_compat=True, alpha=0.2)
plt.tight_layout()
plt.show()

# ## Bonus Challenge Assignment

# + active=""
# BONUS: Hawaii is reputed to enjoy mild weather all year.
# Is there a meaningful difference between the temperature in, for example, June and December?
# You may either use SQLAlchemy or pandas's `read_csv()` to perform this portion.
# Identify the average temperature in June at all stations across all available years in the dataset.
# Do the same for December temperature.
#
# -

hawai_df=pd.read_csv('./Resources/hawaii_measurements.csv')
hawai_df.shape

hawai_df.head()

hawai_df['month']=pd.DatetimeIndex(hawai_df['date']).month
hawai_df.head()

# # June Average Temperature

# June temperature
hawai_df_June=hawai_df.loc[(hawai_df['month']==6)]
hawai_df_June.head()

hawai_df_June.shape

# Average Temperature for June
June_avg_temp=hawai_df_June['tobs'].mean()
June_avg_temp

# # Dec Average Temperature

# Dec temperature
hawai_df_Dec=hawai_df.loc[(hawai_df['month']==12)]
hawai_df_Dec.head()

hawai_df_Dec.shape


# Average Temperature for December
Dec_avg_temp=hawai_df_Dec['tobs'].mean()
Dec_avg_temp

# # T-test

# + active=""
#  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?
#

# + active=""
# For comparing temperatures between June and December for Hawaii, we will use Paired sample t-test.
# A Paired sample t-test compares means from the same group (hawai_df) at different times (June and December)
#
# There are three main types of t-test:
# 1. An Independent Samples t-test compares the means for two groups.
# 2. A Paired sample t-test compares means from the same group at different times.
# 3. A One sample t-test tests the mean of a single group against a known mean.
#

# +
from scipy.stats import ttest_ind

June = hawai_df[hawai_df['month']==6]
Dec= hawai_df[hawai_df['month']==12]

ttest_ind(June['tobs'], Dec['tobs'])


# +
Null Hypothesis: There is NO meaningful difference between the temperature in June and December.
Alternate Hypothesis: There IS meaningful difference between the temperature in June and December.

Since the p-value is less than 0.5, we can reject the Null Hypothesis.T-test is statistically significant.
Conclusion: There IS meaningful difference between the temperature in June and December.


StarcoderdataJupyterScriptsDedupFiltered