Pythonの日時

Pythonで扱う日付と時刻

Max Shron

Data Scientist and Author

コース概要

  • 第1章: 日付とカレンダー
  • 第2章: 日付と時刻の組み合わせ
  • 第3章: タイムゾーンと夏時間
  • 第4章: Pandasでの日時
Pythonで扱う日付と時刻

Pythonの日時

Pythonで扱う日付と時刻

なぜPythonで日付クラスが必要か

two_hurricanes = ["10/7/2016", "6/21/2017"]

次をどのように行いますか:

  • 経過日数を求めるには?
  • 早い順に並んでいるか確認するには?
  • 各日付の曜日を知るには?
  • 特定の期間のハリケーンだけを抽出するには?
Pythonで扱う日付と時刻

dateオブジェクトの作成

# Import date
from datetime import date

# Create dates two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]
Pythonで扱う日付と時刻

dateの属性

# Import date
from datetime import date

# Create dates
two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]

print(two_hurricanes_dates[0].year) print(two_hurricanes_dates[0].month) print(two_hurricanes_dates[0].day)
2016
10
7
Pythonで扱う日付と時刻

日付の曜日を求める

print(two_hurricanes_dates[0].weekday())
4
  • Pythonの曜日
    • 0 = 月
    • 1 = 火
    • 2 = 水
    • ...
    • 6 = 日
Pythonで扱う日付と時刻

Pythonの日時

Pythonで扱う日付と時刻

Preparing Video For Download...