如何用Python解析Timestamp()?

时间:2017-09-15 23:56:59

标签: python python-3.x collections

我正在迭代一个包含SQL数据库数据的字典,我想计算userinitial_date之间出现ending_date值的次数,但是,我是当我尝试解析Timestamp值时遇到一些问题。这是我的代码

initial_date = datetime(2017,09,01,00,00,00)
ending_date  = datetime(2017,09,30,00,00,00)

dictionary sample that I got

sample = {'id': 100008222, 'sector name': 'BONGOX', 'site name': 'BONGO', 'region': 'EMEA', 
'open date': Timestamp('2017-09-11 00:00:00'), 'mtti': '16', 'mttr': '1', 'mttc': '2','user':'John D.'},
{'id': 100008234, 'sector name': 'BONGOY', 'site name': 'BONGO', 'region': 'EMEA', 
'open date': Timestamp('2017-09-09 12:05:00'), 'mtti': '1', 'mttr': '14', 'mttc': '7','user':'John D.'}
{'id': 101108234, 'sector name': 'BONGOA', 'site name': 'BONGO', 'region': 'EMEA', 
'open date': Timestamp('2017-09-01 10:00:00'), 'mtti': '1', 'mttr': '12', 'mttc': '1','user':'John C.'}
{'id': 101108254, 'sector name': 'BONGOB', 'site name': 'BONGO', 'region': 'EMEA', 
'open date': Timestamp('2017-09-02 20:00:00'), 'mtti': '2', 'mttr': '19', 'mttc': '73','user':'John C.'}

这是我用来计算userinitial_date

之间出现ending_date值的次数的代码
from datetime import time, datetime
from collections import Counter 

#This approach does not work  
Counter([li['user'] for li in sample if initial_date < dateutil.parser.parse(time.strptime(str(li.get(
'open date'),"%Y-%m-%d %H:%M:%S") < ending_date])

上面的代码不起作用,因为我遇到了错误decoding to str: need a bytes-like object, Timestamp found

我有两个问题:

  1. 如何解析我在这些词典中遇到的Timestamp值?
  2. 我在这篇文章Why Counter is slow中读到,与其他计算项目出现次数的方法相比,Collections.Counter是一种缓慢的方法。如果想避免使用Counter.Collections,我怎样才能达到计算user值在这些日期之间显示的次数所需的结果?

2 个答案:

答案 0 :(得分:1)

使用SELECT 'foo' as "bar" INTO ... FROM ... >>> outputs foo as a value of bar. SELECT "foo" as "bar" INTO ... FROM ... >>> outputs null as a value of bar. 转换为class OMER(Packet): name = "OMER" fields_desc = [StrLenField("Omer", "", None)] 对象

答案 1 :(得分:0)

  

问题:如何解析我在这些词典中遇到的时间戳值?

使用class Timestampe

中的pandas
from pandas import Timestamp
  1. 使用Counter()

    # Initialize a Counter() object
    c = Counter()
    
    # Iterate data
    for s in sample:
        # Get a datetime from Timestamp
        dt = s['open date'].to_pydatetime()
    
        # Compare with ending_date
        if dt < ending_date:
            print('{} < {}'.format(dt, ending_date))
    
            # Increment the Counter key=s['user']
            c[s['user']] += 1
    print(c)
    
      

    输出

    2017-09-11 00:00:00 < 2017-09-30 00:00:00
    2017-09-09 12:05:00 < 2017-09-30 00:00:00
    2017-09-01 10:00:00 < 2017-09-30 00:00:00
    2017-09-02 20:00:00 < 2017-09-30 00:00:00
    Counter({'John C.': 2, 'John D.': 2})
    
  2.   

    问题:如果想避免使用Counter.Collections,我怎样才能达到理想的计数结果

    1. 没有Counter()

      # Initialize a Dict object
      c = {}
      # Iterate data
      for s in sample:
          # Get a datetime from Timestamp
          dt = s['open date'].to_pydatetime()
      
          # Compare with ending_date
          if dt < ending_date:
              # Add key=s['user'] to Dict if not exists
              c.setdefault(s['user'], 0)
      
              # Increment the Dict key=s['user']
              c[s['user']] += 1
      print(c)
      
        

      输出

      {'John D.': 2, 'John C.': 2}
      
    2. 使用Python测试:3.4.2