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

# # Bonus: Temperature Analysis I

import pandas as pd
from datetime import datetime as dt

# "tobs" is "temperature observations"
df = pd.read_csv('resources/hawaii_measurements.csv')
df.head()

# Convert the date column format from string to datetime
datetime_format = "%Y-%m-%d"
df["date"] = pd.to_datetime(df["date"], format=datetime_format)

# Set the date column as the DataFrame index
df.set_index("date", inplace = True)

# Drop the date column
if "prcp" in df.columns:
    df.drop("prcp", axis=1, inplace=True)
df

# ### Compare June and December data across all years 

from scipy import stats

# Filter data for desired months June
june_df = df.filter(regex ="\d\d\d\d-06-\d\d", axis=0).sort_index()
june_df

# Cont. Filter data for desired months December
dec_df = df.filter(regex ="\d\d\d\d-12-\d\d", axis=0).sort_index()
dec_df


# +
# Identify the average temperature for June
def get_year_ranges(data):
    min_date = min(data.index)
    max_date = max(data.index)
    return list(range(min_date.year, max_date.year))

print("Identify the average temperature for June")
all_june_data = {}
for year in get_year_ranges(june_df):
    key = str(year)
    value = june_df.filter(regex = f"{year}-\d\d-", axis=0)["tobs"]
    all_june_data[key] = value
    print(f"year: {year}, mean: {round(value.mean(), 2)}")
    
    
# -

# Identify the average temperature for December
print("Identify the average temperature for December")
all_dec_data = {}
for year in get_year_ranges(dec_df):
    key = str(year)
    value = dec_df.filter(regex = f"{year}-\d\d-", axis=0)["tobs"]
    all_dec_data[key] = value
    print(f"year: {year}, mean: {round(value.mean(), 2)}")

# +
# Create collections of temperature data
# -

# Run paired t-test
t_value, p_value = stats.ttest_ind(june_df["tobs"], dec_df["tobs"])
print(f"t_value {t_value}")
print(f"p_value {p_value}")

# ### Analysis

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

years = get_year_ranges(june_df)
fig, ax = plt.subplots(1, 1, figsize = (7,6))
plt.boxplot(
    [june_df.filter(regex = f"{year}-\d\d-", axis=0)["tobs"]
    for year in years], 
    vert=True, 
    labels=years)
ax.yaxis.set_major_formatter(mtick.StrMethodFormatter('{x:,.0f}°')) 
plt.title(f'\nObservations for State of Hawaii Temperature in June', fontsize = (18))
plt.xlabel('Years', fontsize = (22))
plt.ylabel('Temperature (°F)', fontsize = (22))
plt.grid(alpha = 0.7)
plt.savefig('Images/bonus_1_hawaii_june_temps.png')
plt.show()

years = get_year_ranges(dec_df)
fig, ax = plt.subplots(1, 1, figsize = (7,6))
plt.boxplot(
    [dec_df.filter(regex = f"{year}-\d\d-", axis=0)["tobs"]
    for year in years], 
    vert=True, 
    labels=years)
ax.yaxis.set_major_formatter(mtick.StrMethodFormatter('{x:,.0f}°')) 
plt.title(f'\nObservations for State of Hawaii Temperature in December', fontsize = (18))
plt.xlabel('Years', fontsize = (22))
plt.ylabel('Temperature (°F)', fontsize = (22))
plt.grid(alpha = 0.7)
plt.savefig('Images/bonus_1_hawaii_dec_temps.png')
plt.show()

# 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? 
#
# Answer:  Due to the data being different the t-test will not be utilized in this case.




StarcoderdataJupyterScriptsDedupFiltered