【问题标题】:How to parse a Timestamp() with Python?如何用 Python 解析 Timestamp()?
【发布时间】:2018-02-25 04:33:52
【问题描述】:

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

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.'}

这是我用来计算user 值出现在initial_dateending_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 值的次数的结果?

【问题讨论】:

  • 看看这个:medium.com/@eleroy/…
  • 在您的情况下,您需要从 Timestamp 对象中获取字符串,或者更好地比较实际的 UTC 时间戳,这更容易
  • 顺便说一句,psycopg 默认将日期转换为 Datetime 对象,所以您是否只需尝试 Counter([li['user'] for li in sample if initial_date
  • @MrE 如何比较实际的 UTC 时间戳?
  • 什么是时间戳?这是从哪里来的?它来自你的 ORM 吗?您使用哪种 ORM?

标签: python python-3.x collections


【解决方案1】:

问题:如何解析我在这些字典中遇到的 Timestamp 值?

使用来自pandasclass Timestampe

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})
    

问题:如果想避免使用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}
    

用 Python 测试:3.4.2

【讨论】:

  • 出现错误。我有an integer is required (got type Timestamp) :(
【解决方案2】:

使用Timestamp.to_datetime() 转换为datetime 对象

猜你喜欢
  • 2019-05-21
  • 2022-01-24
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 2018-10-06
  • 1970-01-01
相关资源
最近更新 更多