forked from udacity/pdsnd_github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbikeshare.py
More file actions
295 lines (226 loc) · 10.3 KB
/
bikeshare.py
File metadata and controls
295 lines (226 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import time
from pprint import pprint
import pandas as pd
import numpy as np
CITY_DATA = { 'chicago': 'chicago.csv',
'new york city': 'new_york_city.csv',
'washington': 'washington.csv' }
def get_filters():
"""
Asks user to specify a city, month, and day to analyze.
Returns:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
"""
city, month, day = '' , '', ''
print('Hello! Let\'s explore some US bikeshare data!')
# get user input for city (chicago, new york city, washington). HINT: Use a while loop to handle invalid inputs
while True:
try:
city = input('\nWhat city would you like to view data for: Chicago, New York City, Washington?\n')
# removing whitespaces and converting to lowercase
city = city.strip().lower()
except EOFError:
city = ''
if city in ['chicago', 'new york city', 'washington']:
break
else:
print('Ooops! Please check your input and try again. Eg: New York City\n')
# get user input for month (all, january, february, ... , june)
while True:
try:
month = input('\nWhich month(s) would you like to view bikeshare data for: all, January, ..., June?\n')
month = month.strip().lower()
except EOFError:
month = ''
if month in ['all', 'january', 'february', 'march', 'april', 'may', 'june']:
break
else:
print('Ooops! Please try again. Enter a month from January to June or all. Eg: all or March\n')
# get user input for day of week (all, monday, tuesday, ... sunday)
while True:
try:
day = input('\nWhich day(s) would you like to view data for: all, Monday, ..., Sunday?\n')
day = day.strip().lower()
except EOFError:
day = ''
if day in ['all', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday','sunday']:
break
else:
print('Ooops! Please try again. Enter a day from Monday to Sunday or all. Eg: all or Wednesday\n')
print('-'*40)
return city, month, day
def load_data(city, month, day):
"""
Loads data for the specified city and filters by month and day if applicable.
Args:
(str) city - name of the city to analyze
(str) month - name of the month to filter by, or "all" to apply no month filter
(str) day - name of the day of week to filter by, or "all" to apply no day filter
Returns:
df - Pandas DataFrame containing city data filtered by month and day
loaded_df - Pandas DataFrame containing data loaded from data file filtered by city, month, and day
"""
# load data file into a dataframe
df = pd.read_csv(CITY_DATA[city])
# make copy of DataFrame for raw trip data output
loaded_df = df.copy()
loaded_df.rename(columns = {'Unnamed: 0' : 'index'}, inplace = True)
# convert the Start Time column to datetime
df['Start Time'] = pd.to_datetime(df['Start Time'])
# extract month, day of week, and hour from Start Time to create new columns
df['month'] = df['Start Time'].dt.month
df['day_of_week'] = df['Start Time'].dt.weekday_name
df['hour'] = df['Start Time'].dt.hour
# filter by month if applicable
if month != 'all':
# use the index of the months list to get the corresponding int
months = ['january', 'february', 'march', 'april', 'may', 'june']
month = months.index(month) + 1
# filter by month to create the new dataframe
df = df[df['month'] == month]
# filter by day of week if applicable
if day != 'all':
# filter by day of week to create the new dataframe
df = df[df['day_of_week'] == day.title()]
# applying city, month, day filters to raw data dataframe
loaded_df = loaded_df[loaded_df.index.isin(df.index)]
return df, loaded_df
def data_viewer(df, n):
"""Displays raw data of descriptive statitics n lines at a time."""
i = 0
while True:
try:
# error handling EOFError when taking input
first_prompt = '\nWould you like to view trip data?\nEnter yes or y to view or any other key to continue program.\n'
subsequent_prompt = '\nView some more trip data?\nEnter yes or y to view or any other key to continue program.\n'
if i == 0:
raw_data = input(first_prompt)
else:
raw_data = input(subsequent_prompt)
print('\n')
except EOFError:
raw_data = ''
if raw_data.strip().lower() not in ['yes', 'y']:
break
else:
# splitting rows of data into list of dictionaries
trip_records = df.to_dict('records')
# display raw data 5 lines of raw data
if i < len(trip_records):
subset = trip_records[i : i + n]
for trip_data in subset:
pprint(trip_data)
i += n
else:
# end of records
print('\nNo more data to display\n')
break
def time_stats(df):
"""Displays statistics on the most frequent times of travel."""
print('\nCalculating The Most Frequent Times of Travel...\n')
start_time = time.time()
# display the most common month
popular_month = df['month'].mode()
print('Most Common Month of Travel: ', popular_month.to_string(index = False))
# display the most common day of week
popular_day = df['day_of_week'].mode()
print('Most Frequent Day of Travel: ', popular_day.to_string(index = False))
# display the most common start hour
popular_hour = df['hour'].mode()
print('Most Frequent Start Hour: ', popular_hour.to_string(index = False))
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def station_stats(df):
"""Displays statistics on the most popular stations and trip."""
print('\nCalculating The Most Popular Stations and Trip...\n')
start_time = time.time()
# display most commonly used start station
popular_start_station = df['Start Station'].mode()
print('Most Popular Start Station: ', popular_start_station.to_string(index = False))
# display most commonly used end station
popular_end_station = df['End Station'].mode()
print('Most Commonly Used End Station: ', popular_end_station.to_string(index = False))
# display most frequent combination of start station and end station trip
df['station_combo'] = df['Start Station'] + ' to ' + df['End Station']
popular_station_combo = df['station_combo'].mode()
print('Most Frequent Combination of Start Station and End Station:\n', popular_station_combo.to_string(index = False))
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def time_converter(seconds):
"""
Converts time in seconds to days, hours, minutes and seconds.
Args:
(int) seconds - time in seconds to for conversion
Returns:
(int) d - time in days only
(int) h - time in hours only
(int) m - time in minutes only
(int/float) s - time in seconds only
"""
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
return int(d), int(h), int(m), s
def trip_duration_stats(df):
"""Displays statistics on the total and average trip duration."""
print('\nCalculating Trip Duration...\n')
start_time = time.time()
# display total travel time
total_time = df['Trip Duration'].sum()
total_d, total_h, total_m, total_s = time_converter(total_time)
print('Total Travel Time: {} seconds or {} days {} hours {} minutes {} seconds'.format(total_time, total_d, total_h, total_m, total_s))
# display mean travel time
avg_time = df['Trip Duration'].mean()
avg_d, avg_h, avg_m, avg_s = time_converter(avg_time)
print('Average Travel Time: {} seconds or {} days {} hours {} minutes {:.1f} seconds'.format(avg_time, avg_d, avg_h, avg_m, avg_s))
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def user_stats(df):
"""Displays statistics on bikeshare users."""
print('\nCalculating User Stats...\n')
start_time = time.time()
# display counts of user types
user_types = df['User Type'].value_counts()
print('Counts of User Types:\n', user_types.to_string())
try:
# display counts of gender
gender_types = df['Gender'].value_counts()
print('\nCounts of Gender Types:\n', gender_types.to_string())
except KeyError:
print('\nBreakdown of Gender Types: Gender data unavailable for the selected city')
try:
# display earliest, most recent, and most common year of birth
print('\nEarliest Year of Birth: ', int(df['Birth Year'].min()))
print('Most Recent Year of Birth: ', int(df['Birth Year'].max()))
print('Most Common Year of Birth: ', int(df['Birth Year'].mode()))
except KeyError:
print('\nEarliest, Most Recent and Common Years of Birth: Birth Year data unavailable for the selected city')
print("\nThis took %s seconds." % (time.time() - start_time))
print('-'*40)
def main():
"""Calls other funtions to run the interactive program in a loop."""
while True:
try:
city, month, day = get_filters()
df, loaded_df = load_data(city, month, day)
time_stats(df)
station_stats(df)
trip_duration_stats(df)
user_stats(df)
data_viewer(loaded_df, 5)
try:
# error handling EOFError when taking input
restart = input('Would you like to restart?\nEnter yes or y to restart or any other key to exit.\n')
print('\n\n')
except EOFError:
restart = ''
if restart.strip().lower() not in ['yes', 'y']:
break
except KeyboardInterrupt:
# keyboard intterupts while the program is running will cause a force quit
print('\n\nForce quit\nExiting program now...')
break
if __name__ == "__main__":
main()