1 import pandas as pd 2 """ 3 pandas默认支持的时间点类型——Timestamp 4 pandas默认支持的时间序列类型——DatetimeIndex 5 numpy默认支持的时间点数据类型——datetime64 6 """ 7 8 # 可以使用pd.to_datetime 将时间点转化为pandas默认支持的时间点类型 9 res = pd.to_datetime("2019-11-11") 10 print("res: \n", res) 11 print("res的类型: \n", type(res)) 12 13 # 可以使用pd.to_datetime 将时间序列转化为pandas支持的时间序列类型 14 res = pd.to_datetime(["2019-11-11", "2019-12-12", "2020-02-14", "2020-03-07"]) 15 print("res: \n", res) 16 print("res的类型: \n", type(res)) 17 18 # 可以使用pd.DatetimeIndex 将时间序列转化为pandas支持的时间序列类型, 不能转化时间点 19 res = pd.DatetimeIndex(["2019-11-11", "2019-12-12", "2020-02-14", "2020-03-07"]) 20 print("res: \n", res) 21 print("res的类型: \n", type(res)) 22 23 # 加载detail 24 detail = pd.read_excel("../day05/meal_order_detail.xlsx") 25 print("detail: \n", detail) 26 print("detail的列名称: \n", detail.columns) 27 print(detail.dtypes) 28 29 # 将 place_order_time 转化为pandas默认支持的时间序列类型 30 detail.loc[:, "place_order_time"] = pd.to_datetime(detail.loc[:, "place_order_time"]) 31 print(detail.dtypes) 32 # 可以提取出时间序列中的属性 33 34 # 年属性 35 year = [i.year for i in detail.loc[:, "place_order_time"]] 36 print("year: \n", year) 37 38 # 月属性 39 month = [i.month for i in detail.loc[:, "place_order_time"]] 40 print("month: \n", month) 41 42 # 日属性 43 day = [i.day for i in detail.loc[:, "place_order_time"]] 44 print("day: \n", day) 45 46 # 周属性——一年的第N周 47 week = [i.week for i in detail.loc[:, "place_order_time"]] 48 print("week: \n", week) 49 50 week_of_year = [i.weekofyear for i in detail.loc[:, "place_order_time"]] 51 print("week_of_year: \n", week_of_year) 52 53 day_of_year = [i.dayofyear for i in detail.loc[:, "place_order_time"]] 54 print("day_of_year: \n", day_of_year) 55 56 # 获取一周中的第N天 57 day_of_week = [i.dayofweek for i in detail.loc[:, "place_order_time"]] 58 print("day_of_week: \n", day_of_week) 59 60 # 获取周几 61 weekday = [i.weekday for i in detail.loc[:, "place_order_time"]] 62 print("weekday: \n", weekday) 63 64 weekday_name = [i.weekday_name for i in detail.loc[:, "place_order_time"]] 65 print("weekday_name: \n", weekday_name) 66 67 # 获取第几季度 68 quarter = [i.quarter for i in detail.loc[:, "place_order_time"]] 69 print("quarter: \n", quarter) 70 71 # 时间数据的运算 72 res = pd.to_datetime("2019-11-11") + pd.Timedelta(days=2) 73 res = pd.to_datetime("2019-11-11") + pd.Timedelta(weeks=1) 74 res = pd.to_datetime("2019-11-11") + pd.Timedelta(weeks=-1) 75 76 # 时间差——返回days 77 res = pd.to_datetime("2019-11-11") - pd.to_datetime("2002-1-8") 78 print("res: \n", res) 79 res = res.days 80 print("res: \n", res) 81 res = res/365 82 print("年龄: \n", res) 83 84 # 还可以获取本机的最初始时间、最大时间 85 print("本机的最小时间: \n", pd.Timestamp.min) 86 print("本机的最大时间: \n", pd.Timestamp.max) 87 88 # 生成时间数据的API 89 # start——开始日期 90 # end——结束日期 91 # periods——如果end不传, 生成时间数据的数量 92 # freq——默认按天 93 res = pd.date_range(start="2019-11-11", periods=5) 94 res = pd.date_range(start="2019-11-11", end="2019-11-16") # end和period不能同时传 95 # 生成频次为36天 96 res = pd.date_range(start="2019-11-11", end="2020-11-16", freq="36D") 97 print(res)
原文地址:https://www.cnblogs.com/Tree0108/p/12116054.html
时间: 2024-11-12 20:45:52