pd.to_datetime(arg , errors=“raise” , dayfirst=False , yearfirst=False , utc=None , box=True , format=None , exact=True , unit=None , infer_datetime_format=False , origin=“unix” , cache=False)
arg :指定转换的数据对象,可以是整型、浮点型、字符串、列表、元组、一维数组、 Serise 、 DataFrame 和字典
errors :设置出错提示方式,可选 {"ignore","raise","coercec"} ,默认值为 "raise" ,如果转换失败,则给出出错提示信息; "ignore" 则不出发出错提示信息; "coercec" 在转换过程存在无效时间值时,自动转为 NaT 值
dayfirst :指定 arg 参数转换时的顺序,设置为 True 时,则先转换日期,再转换时间,默认值为 False
yearfirst :值为 True 时则先转换日期,默认值为 False
utc :值为 True 返回 UTC DatetimeIndex ,默认值为 None
box :默认值为 True 返回 DatetimeIndex 或相关索引对象;值为 False 则返回多维数组
format :字符串,跟单网www.gendan5.com默认值为 None ,指定字符串时间转化为时间时的 strftime 的格式,类似 strftime 方法转化为时间的使用方法
exact :默认值为 True 表示精确匹配格式,值为 False 则允许匹配目标字符串中的任何位置
unit :字符串,默认值为 "ns" ,对转换对象指定时间单位( D 天、 s 秒、 ma 毫秒、 ns 纳秒)
infer_datetime_format :默认值为 False ,如果为 True ,且没有给出转换固定格式( format 参数),且字符串日期时间格式确定,则可以提高转换速度
origin :确定日期的开始点,默认值为 "unix" ,则日期的开始点为 1970-01-01 ,若提供值为 Timestamp 日期,则以 Timestamp 的起点日期作为开始点日期
cache :默认值为 False ,如果为 True ,则是用唯一的转换日期缓存来应用日期时间转换,解析重复的日期字符串时可以提高转换速度
import pandas as pdfrom datetime import datetime
filename = r"D:\data_test.xlsx"
df = pd.read_excel(filename)
print(df.head())print("="*30)print(df.info())
name gender birthday start_work income tel email \
0 赵一 男 1989/8/10 2012-09-08 15000 13611011234 zhaoyi@qq.com
1 王二 男 1990/10/2 2014-03-06 12500 13500012234 wanger@163.com
2 张三 女 1987/3/12 2009-01-08 18500 13515273330 zhangsan@qq.com
3 李四 女 1991/8/16 2014-06-04 13000 13923673388 lisi@gmail.com
4 刘五 女 1992/5/24 2014-08-10 8500 17823117890 liuwu@qq.com
other
0 { 教育:本科,专业:电子商务,爱好:运动 }
1 { 教育:大专,专业:汽修,爱好: }
2 { 教育:本科,专业:数学,爱好:打篮球 }
3 { 教育:硕士,专业:统计学,爱好:唱歌 }
4 { 教育:本科,专业:美术,爱好: }
==============================
RangeIndex: 8 entries, 0 to 7
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 8 non-null object
1 gender 8 non-null object
2 birthday 8 non-null object
3 start_work 8 non-null datetime64[ns]
4 income 8 non-null int64
5 tel 8 non-null int64
6 email 8 non-null object
7 other 8 non-null object
dtypes: datetime64[ns](1), int64(2), object(5)
memory usage: 640.0+ bytes
None
df.birthday=pd.to_datetime(df.birthday,format="%Y-%m-%d")
df.info()
1
2
RangeIndex: 8 entries, 0 to 7
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 8 non-null object
1 gender 8 non-null object
2 birthday 8 non-null datetime64[ns]
3 start_work 8 non-null datetime64[ns]
4 income 8 non-null int64
5 tel 8 non-null int64
6 email 8 non-null object
7 other 8 non-null object
dtypes: datetime64[ns](2), int64(2), object(4)
memory usage: 640.0+ bytes
data = pd.DataFrame({" 客户 ":[" 李 "," 张 "," 刘 "," 宋 "]," 工资 ":[3500,2500,1500,500]," 日期 ":["2020-11-19","2020-11-20","2020-12-19","2020-12-20"]},index = ["A","B","C","D"])print(data.info)
A 李 3500 2020-11-19
B 张 2500 2020-11-20
C 刘 1500 2020-12-19
D 宋 500 2020-12-20>