• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

pandas timestamp isocalendar

date: 2024-05-25 excerpt: pandas timestamp isocalendar

tag: pythonpandasチートシート


pandas timestamp isocalendar

概要

  • pandasのtimestampオブジェクトには、isocalendarメソッドがあり、その年、週、曜日を取得できる

サンプルコード

ser = pd.to_datetime(pd.Series(["2024-05-05", "2024-05-06", "2024-05-07"]))
ser.dt.isocalendar()
"""
|   year |   week |   day |
|-------:|-------:|------:|
|   2024 |     18 |     7 |
|   2024 |     19 |     1 |
|   2024 |     19 |     2 |
"""
# 週の情報を取り出す
weekday_names = {1: '月', 2: '火', 3: '水', 4: '木', 5: '金', 6: '土', 7: '日'}
ser.dt.isocalendar().day.apply(lambda x: weekday_names.get(x))
"""
0    日
1    月
2    火
Name: day, dtype: object
"""

参考

  • pandas.Series.dt.isocalendar


pythonpandasチートシート Share Tweet