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

# # Wind Statistics

# ### Introduction:
#
# The data have been modified to contain some missing values, identified by NaN.  
# Using pandas should make this exercise
# easier, in particular for the bonus question.
#
# You should be able to perform all of these operations without using
# a for loop or other looping construct.
#
#
# 1. The data in 'wind.data' has the following format:

# + jupyter={"outputs_hidden": false}
"""
Yr Mo Dy   RPT   VAL   ROS   KIL   SHA   BIR   DUB   CLA   MUL   CLO   BEL   MAL
61  1  1 15.04 14.96 13.17  9.29   NaN  9.87 13.67 10.25 10.83 12.58 18.50 15.04
61  1  2 14.71   NaN 10.83  6.50 12.62  7.67 11.50 10.04  9.79  9.67 17.54 13.83
61  1  3 18.50 16.88 12.33 10.13 11.17  6.17 11.25   NaN  8.50  7.67 12.75 12.71
"""
# -

#    The first three columns are year, month and day.  The
#    remaining 12 columns are average windspeeds in knots at 12
#    locations in Ireland on that day.   
#
#    More information about the dataset go [here](wind.desc).

# ### Step 1. Import the necessary libraries

# + jupyter={"outputs_hidden": false}
import numpy as np
import pandas as pd
# -

# ### Step 2. Import the dataset from this [address](https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/06_Stats/Wind_Stats/wind.data)

# ### Step 3. Assign it to a variable called data and replace the first 3 columns by a proper datetime index.

# + jupyter={"outputs_hidden": false}
data = pd.read_csv('wind.data', sep='\s+', parse_dates=[[0,1,2]])
# -

data.shape


# ### Step 4. Year 2061? Do we really have data from this year? Create a function to fix it and apply it.

# + jupyter={"outputs_hidden": false}
def modifyYr(date):
    return date - pd.DateOffset(years=100) if date.year>1989 else date


# -

data['Yr_Mo_Dy'] = data.Yr_Mo_Dy.apply(modifyYr)


def fix_century(x):
    year = x.year -100 if x.year > 1989 else x.year
    return datetime.date(year, x.month, x.day)


# +
# data['Yr_Mo_Dy'].apply(fix_century)
# -

data.head()

data.info()

# ### Step 5. Set the right dates as the index. Pay attention at the data type, it should be datetime64[ns].

# + jupyter={"outputs_hidden": false}
data.set_index('Yr_Mo_Dy', inplace=True)
# -

data.head()

# ### Step 6. Compute how many values are missing for each location over the entire record.  
# #### They should be ignored in all calculations below. 

# + jupyter={"outputs_hidden": false}
data.isnull().sum()
# -

# ### Step 7. Compute how many non-missing values there are in total.

# + jupyter={"outputs_hidden": false}
data.count()
# -

data.notnull().sum()
# data.shape[0] - data.isnull().sum()

# ### Step 8. Calculate the mean windspeeds of the windspeeds over all the locations and all the times.
# #### A single number for the entire dataset.

# + jupyter={"outputs_hidden": false}
# data.mean().mean()
# -

data.sum().sum()/data.notna().sum().sum()

# ### Step 9. Create a DataFrame called loc_stats and calculate the min, max and mean windspeeds and standard deviations of the windspeeds at each location over all the days 
#
# #### A different set of numbers for each location.

# + jupyter={"outputs_hidden": false}
loc_stats = data.agg(['min', 'max', 'mean', 'std'])
# -

loc_stats

data.describe(percentiles=[])

# ### Step 10. Create a DataFrame called day_stats and calculate the min, max and mean windspeed and standard deviations of the windspeeds across all the locations at each day.
#
# #### A different set of numbers for each day.

# + jupyter={"outputs_hidden": false}
day_stats = data.agg(['min', 'max', 'mean', 'std'], axis=1)
# -

day_stats

# ### Step 11. Find the average windspeed in January for each location.  
# #### Treat January 1961 and January 1962 both as January.

# + jupyter={"outputs_hidden": false}
data[(data.index.year.isin(['1961', '1962'])) & (data.index.month==1)].agg(['mean'])
# -

# ### Step 12. Downsample the record to a yearly frequency for each location.

# + jupyter={"outputs_hidden": false}
data.resample('1AS').mean()

# +
# data.groupby(data.index.to_period('A')).mean()
# -

# ### Step 13. Downsample the record to a monthly frequency for each location.

# + jupyter={"outputs_hidden": false}
data.resample('1MS').mean()

# +
# data.groupby(data.index.to_period('M')).mean()
# -

# ### Step 14. Downsample the record to a weekly frequency for each location.

# + jupyter={"outputs_hidden": false}
data.resample('1W').mean()

# +
# data.groupby(data.index.to_period('W')).mean()
# -

# ### Step 15. Calculate the min, max and mean windspeeds and standard deviations of the windspeeds across all locations for each week (assume that the first week starts on January 2 1961) for the first 52 weeks.

# + jupyter={"outputs_hidden": false}
data.resample('1W').agg(['min', 'max', 'mean', 'std']).iloc[1:53,:]
# -

# ### 频率选项

# 别名|偏移量类型|说明
# :--|:--|:--
# D|Day|每日历日
# B|BusinessDay|每工作日
# H|Hour|每小时
# T/min|Minute|每分
# S|Second|每秒
# L/ms|Million|每毫秒
# U|Micro|每微妙
# M|MonthEnd|每月最后一个日历日
# BM|BusinessMonthEnd|每月最后一个工作日
# MS|MonthBegin|每月第一个日历日
# BMS|BusinessMonthBegin|每月第一个工作日
# W-MON、W-TUE…|Week|从指定的星期几开始算起，每周
# WOM-1MON、WOM-2MON…|WeekOfMonth|产生每月第一、二、三、四周的星期几，例如WOM-1MON表示每月的第一个星期一
# Q-JAN、Q-FEB…|QuarterEnd|对于以指定月份（JAN、FEB、…、DEC）结束的年度，每季度的最后一月的最后一个日历日
# BQ-JAN、BQ-FEB…|BusinessQuarterEnd|对于以指定月份（JAN、FEB、…、DEC）结束的年度，每季度的最后一月的最后一个工作日
# QS-JAN、QS-FEB…|QuarterBegin|对于以指定月份（JAN、FEB、…、DEC）结束的年度，每季度的最后一月的第一个日历日
# BQS-JAN、BQS-FEB…|BusinessQuarterBegin|对于以指定月份（JAN、FEB、…、DEC）结束的年度，每季度的最后一月的第一个工作日
# A-JAN、A-FEB…|YearEnd|每年指定月份最后一个日历日
# BA-JAN、BA-FEB…|BusinessYearEnd|每年指定月份最后一个工作日
# AS-JAN、AS-FEB…|YearBegin|每月指定月份第一个日历日
# BAS-JAN、BAS-FEB…|BusinessYearBegin|每月指定月份第一个工作日



StarcoderdataJupyterScriptsDedupFiltered