【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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Koma_zhe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值