zoukankan      html  css  js  c++  java
  • 时间字符串 操作

    str -> date

    import datetime
    
    detester = ‘2017-01-01'
    date = datetime.datetime.strptime(detester,’%Y-%m-%d')
    

    date -> str

    1 import datetime
    2 
    3 date = datetime.now()
    4 
    5 detester = date.strftime(‘%Y-%m-%d')
    # td['purchase-date']   是数据框 dataframe中的一列
    
    for x in td['purchase-date']:
        nPos = x.index('T')
        print(x[:nPos])
    
    
    # 上面方法 等同于 
    
    
    [x[:x.index('T')]) for x in td['purchase-date']]
    

      

     示例:

    import numpy as np
    import pandas as pd
    import datetime
    
    #把字符串转成datetime
    def string_toDatetime(string):
        return datetime.datetime.strptime(string, "%Y-%m-%d")
    
    
    data = pd.DataFrame([string_toDatetime(x[:x.index('T')]) for x in td['purchase-date']],columns=['purchase-date'])   #截取字符串
    # x.index("T") 获取到T 在字符串中的位置, x 在 nArray  td['purchase-date']中   x[:x.index('T')] 获取 从0 到 字符串位置 的 子字符串

    td.loc[:,'purchase-date'] = data['purchase-date'] # 把处理好的日期列 更新到 原来的 td 数据框内

     

    按月统计 生成柱状图

    df1['month'] = df1['日期'].map(lambda x: x[ :x.rindex('-')])
    

      

  • 相关阅读:
    ②.kubernetes service
    c2p
    ⑤.docker dockerfile
    ④.docker volume
    ②.docker image
    ③.docker container
    ①.docker介绍
    三剑客之grep
    ⑦.shell 数组
    shell 正则
  • 原文地址:https://www.cnblogs.com/UpThinking/p/9496961.html
Copyright © 2011-2022 走看看