datetime、dateutil、pandas 解析日期

import datetime
from dateutil.parser import parse
import pandas as pd

stamp = datetime.datetime(2021,9,10)
print(stamp) # 2021-09-10 00:00:00
print(stamp.date()) # 2021-09-10
print(stamp.strftime("%Y-%m-%d")) # 2021-09-10
# datetime.strftime通过已知格式解析日期
print(datetime.datetime.strftime(stamp,"%Y-%m-%d %H:%M:%S")) # 2021-09-10 00:00:00

# dateutil 可以解析大部分日期格式
print(parse('2021/09/01 11:23:46')) # 2021-09-01 11:23:46
print(parse('Jan 28,2020 1:45 PM')) # 2021-01-28 13:45:00
print(parse('1/2/2019 11:23:46',dayfirst=True)) # 2019-02-01 11:23:46

# pandas解析标准日期格式
datestr=['1/2/2019','2020/6/9','2021-2-1']
print(pd.to_datetime(datestr))
# DatetimeIndex(['2019-01-02', '2020-06-09', '2021-02-01'], dtype='datetime64[ns]', freq=None)
posted @ 2021-09-09 18:13  OTAKU_nicole  阅读(120)  评论(0编辑  收藏  举报