zoukankan      html  css  js  c++  java
  • 时间格式

    pandas中时间字符串转换为年月日方法总结。

    • 创建一个dataframe
    df = pd.DataFrame(['2019-12-09', '2019-12-02'], columns=["date"])
    • 方法1:先转换为时间类型,在获取年月日
    # 转换为时间类型
    df["date"] = pd.to_datetime(df["date"], format='%Y-%m-%d')
    # 获取年
    df["year"] = pd.to_datetime(df["date"]).dt.year
    # 获取月
    df["month"] = pd.to_datetime(df["date"]).dt.month
    # 获取日
    df["day"] = pd.to_datetime(df["date"]).dt.day
    # 获取周
    df["week"] = pd.to_datetime(df["date"]).dt.week
    print(df)
    print(df.dtypes)

    结果如下:

            date  year  month  day  week
    0 2019-12-09  2019     12    9    50
    1 2019-12-02  2019     12    2    49
    date     datetime64[ns]
    year              int64
    month             int64
    day               int64
    week              int64
    dtype: object
    ts.resample('5Min').sum()
    '''
    2012-01-01 00:00:00    74477
    2012-01-01 00:05:00    74834
    2012-01-01 00:10:00    76489
    2012-01-01 00:15:00    25095
    Freq: 5T, dtype: int64

    # 每5分钟进行一次聚合
    ts.resample('5Min').sum()
    '''
    2012-01-01 00:00:00    74477
    2012-01-01 00:05:00    74834
    2012-01-01 00:10:00    76489
    2012-01-01 00:15:00    25095
    Freq: 5T, dtype: int64
    '''
     
  • 相关阅读:
    decltype类型指示符
    vector的使用
    参数使用
    CSPS模拟 43
    CSPS模拟 41
    CSPS模拟 42
    NOIP模拟 40
    NOIP模拟 39
    NOIP模拟 38
    NOIP模拟 37
  • 原文地址:https://www.cnblogs.com/wangzhenghua/p/13708258.html
Copyright © 2011-2022 走看看