skip to content
barorin&?

Pythonで文字列を日付型に変える方法(ついでにPandasの場合とも比較)

/ 1 min read

はじめに

Pythonで文字列を日付型に変える方法です。ついでにPandasの場合とも比較しています。

方法

import datetime

t = 'May 26, 2022'
t2 = datetime.datetime.strptime(t, '%b %d, %Y')
t3 = datetime.date(t2.year, t2.month, t2.day)
# Output -> 2022-05-26

# dfの場合
import pandas as pd
pd.to_datetime(df['date'], format='%b %d, %Y')
# Output -> 2022-05-26