常用时间处理05

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42658739/article/details/89344836

pandas中,to_datetime()函数是用来进行获取时间和进行时间数据操作的函数。

dates = pd.to_datetime(pd.Series(['1989-8-18 13:14:55','1995-2-16']),format='%Y-%m-%d %H:%M:%S')

根据面临的数据格式不同,还有以下几种常用的方法:

print('返回日期值:\n',dates.dt.date) #由于Series并没有date这个属性,所以需要使用dt方法
print('返回第几季度:\n',dates.dt.quarter)
print('返回几点钟:\n',dates.dt.hour)
print('返回年中的天(就是当前日期是当年的第几天的意思):\n',dates.dt.dayofyear)
print('返回年中的周(就是当前日期是当年的第几周的意思):\n',dates.dt.weekofyear)
print('返回星期几的名称:\n',dates.dt.weekday_name)
print("返回日期在月份的天数:\n",dates.dt.days_in_month)

猜你喜欢

转载自blog.csdn.net/qq_42658739/article/details/89344836