ライドの間隔はどのくらい?
最後の演習では、Pandas のインデックス機能を活用して少し面白いことをしてみます。ライドとライドの間にはどれくらいの時間が空いているのでしょうか?
この演習はコースの一部です
Pythonで扱う日付と時刻
演習の手順
- 現在の行の
Start dateと、直前の行のEnd dateの差を計算し、rides['Time since']に代入します。 - 取り扱いやすくするために、
rides['Time since']を秒に変換します。 Start dateに基づいて、ridesを月ごとのバケットにリサンプリングします。- 平均を (60*60) で割って、W20529 が次にピックアップされるまでドックで待機していた平均時間(時間単位)を求めます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Shift the index of the end date up one; now subract it from the start date
rides['Time since'] = rides['Start date'] - (rides[____].shift(1))
# Move from a timedelta to a number of seconds, which is easier to work with
rides['Time since'] = rides['Time since'].____
# Resample to the month
monthly = rides.____('____', on = 'Start date')
# Print the average hours between rides each month
print(monthly['Time since'].____/(60*60))