python中datatime模版,来写一个程序获取当前日期并打印出今天是周几

Python022

python中datatime模版,来写一个程序获取当前日期并打印出今天是周几,第1张

import datetime

today = datetime.date.today()

weekday = datetime.date.weekday(today)+1

print today

print weekday

datetime.timedelta对象代表两个时间之间的时间差,两个date或datetime对象相减就可以返回一个timedelta对象。

利用以下数据进行说明:

如果我们发现时间相关内容的变量为int,float,str等类型,不方便后面的分析,就需要使用该函数转化为常用的时间变量格式:pandas.to_datetime

转换得到的时间单位如下:

如果时间序列格式不统一,pd.to_datetime()的处理方式:

当然,正确的转换是这样的:

第一步:to_datetime()

第二步:astype(datetime64[D]),astype(datetime64[M])

本例中:

order_dt_diff必须是Timedelta('0 days 00:00:00')格式,可能是序列使用了diff()

或者pct_change()。

前者往往要通过'/np.timedelta'去掉单位days。后者其实没有单位。

假如我们要统计某共享单车一天内不同时间点的用户使用数据,例如

还有其他维度的提取,年、月、日、周,参见:

Datetime properties

注意 :.dt的对象必须为pandas.Series,而不可以是Series中的单个元素