始める無料で始める

DateTime 列を扱う

pandas は既定で日時の列を object 型として読み込みます。テキストのままの日時は時系列分析には使いづらいため、これらの列を datetime 型に変換できる必要があります。datetime 型にすると、pandas の日付をより柔軟に扱え、そこから有用な特徴量を取り出せます。

別の株価データセット(今回は Apple)が apple として読み込まれています。

この演習はコースの一部です

Anomaly Detection in Python

コースを見る

演習の手順

  • appleDate 列を datetime 列に変換してください。
  • 曜日を抽出して、新しい列 day_of_week に保存してください。
  • 月の番号を抽出して、新しい列 month に保存してください。
  • 月内の日にちを抽出して、新しい列 day_of_month に保存してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Convert the Date column to DateTime
apple['Date'] = ____

# Create a column for the day of the week
apple['day_of_week'] = ____

# Create a column for the month
apple['month'] = ____

# Create a column for the day of the month
apple['day_of_month'] = ____

print(apple[['day_of_week', 'month', 'day_of_month']])
コードを編集して実行