zoukankan      html  css  js  c++  java
  • 【数据分析&数据挖掘】pandas时间数据

     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: 
    ", res)
    11 print("res的类型: 
    ", 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: 
    ", res)
    16 print("res的类型: 
    ", 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: 
    ", res)
    21 print("res的类型: 
    ", type(res))
    22 
    23 # 加载detail
    24 detail = pd.read_excel("../day05/meal_order_detail.xlsx")
    25 print("detail: 
    ", detail)
    26 print("detail的列名称: 
    ", 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: 
    ", year)
    37 
    38 # 月属性
    39 month = [i.month for i in detail.loc[:, "place_order_time"]]
    40 print("month: 
    ", month)
    41 
    42 # 日属性
    43 day = [i.day for i in detail.loc[:, "place_order_time"]]
    44 print("day: 
    ", day)
    45 
    46 # 周属性——一年的第N周
    47 week = [i.week for i in detail.loc[:, "place_order_time"]]
    48 print("week: 
    ", week)
    49 
    50 week_of_year = [i.weekofyear for i in detail.loc[:, "place_order_time"]]
    51 print("week_of_year: 
    ", week_of_year)
    52 
    53 day_of_year = [i.dayofyear for i in detail.loc[:, "place_order_time"]]
    54 print("day_of_year: 
    ", 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: 
    ", day_of_week)
    59 
    60 # 获取周几
    61 weekday = [i.weekday for i in detail.loc[:, "place_order_time"]]
    62 print("weekday: 
    ", weekday)
    63 
    64 weekday_name = [i.weekday_name for i in detail.loc[:, "place_order_time"]]
    65 print("weekday_name: 
    ", weekday_name)
    66 
    67 # 获取第几季度
    68 quarter = [i.quarter for i in detail.loc[:, "place_order_time"]]
    69 print("quarter: 
    ", 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: 
    ", res)
    79 res = res.days
    80 print("res: 
    ", res)
    81 res = res/365
    82 print("年龄: 
    ", res)
    83 
    84 # 还可以获取本机的最初始时间、最大时间
    85 print("本机的最小时间: 
    ", pd.Timestamp.min)
    86 print("本机的最大时间: 
    ", 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)
  • 相关阅读:
    linux的一般命令------附加
    linux(4)----------ssh config详解
    linux(3)--------SSH工具的安装使用
    linux(2)-----新装linux配置
    linux(1)------vmvear虚拟机安装linux
    (3)hadoop单节点配置
    (2)hadoop之-----配置免密码登录
    (1)hadoop之----linux配置jdk环境
    BZOJ 1037 生日聚会(神DP)
    BZOJ 1046 上升序列(LIS变形)
  • 原文地址:https://www.cnblogs.com/Tree0108/p/12116054.html
Copyright © 2011-2022 走看看