栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Pandas】日期抽取.dt.weekday与.weekday()

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Pandas】日期抽取.dt.weekday与.weekday()

二者区别在与处理的数据类型不一样:如下二例:

.weekday():

python
import pandas as pd
from pandas import to_datetime
li="2020/04/26 22:11:20"
df_dt = to_datetime(li, format="%Y/%m/%d %H:%M:%S")
print(df_dt)
y=df_dt.year
s=df_dt.second
m=df_dt.minute
h=df_dt.hour
d=df_dt.day
M=df_dt.month
w=df_dt.weekday()  #范围:0-6
print("年:",y)
print("月:",M)
print("日:",d)
print("时:",h)
print("分:",m)
print("秒:",s)
print("周几:",w+1)  

# output:
# 2022-05-24 11:11:11
# 年: 2022
# 月: 5
# 日: 24
# 时: 11
# 分: 11
# 秒: 11
# 周几: 2

.dt.weekday:

import pandas as pd
from pandas import to_datetime
li=["2022/05/23 11:11:11","2022/05/24 11:11:11"]
s = pd.Series(li)
raw_data = pd.DataFrame(columns=["time"])
raw_data["time"]= li
#print(raw_data)
#df_dt = to_datetime(s, format="%Y/%m/%d %H:%M:%S")
df_dt = to_datetime(raw_data["time"], format="%Y/%m/%d %H:%M:%S")
print(df_dt)
weekday=df_dt.dt.weekday
y=df_dt.dt.year
s=df_dt.dt.second
m=df_dt.dt.minute
h=df_dt.dt.hour
d=df_dt.dt.day
M=df_dt.dt.month
w=df_dt.dt.weekday
print("年:n",y)
print("月:n",M)
print("日:n",d)
print("时:n",h)
print("分:n",m)
print("秒:n",s)
print("周几:n",w+1)

#output:
# 0   2020-04-22 22:11:20
# 1   2020-04-26 22:11:20
# dtype: datetime64[ns]
# 年: 
# 0    2020
# 1    2020
# dtype: int64
# 月:
# 0    4
# 1    4
# dtype: int64
# 日:
# 0    22
# 1    26
# dtype: int64
# 时:
# 0    22
# 1    22
# dtype: int64
# 分: 0    11
# 1    11
# dtype: int64
# 秒:
# 0    20
# 1    20
# dtype: int64
# 周几:
# 0    3
# 1    7
# dtype: int64
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929061.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号