【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

你可能感兴趣的:(#,Python语法及相关知识,Python相关,python,深度学习,机器学习)