从日期到可用值Python [英] From date-time to usable value Python

查看:173
本文介绍了从日期到可用值Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一段时间内制作事件的直方图。我的数据集以格式ex给我每个事件的时间。 2013-09-03 17:34:04,我该如何将其转换为可以在直方图中绘制的东西?
我知道如何用datetime和time命令来做另外一种方法。



我的数据集包含1.500.000以上的数据点,所以请只有解决方案可以通过循环或类似的方式进行自动化;)

解决方案

使用 time.strptime )将本地时间字符串转换为 time.struct_time 然后 time.mktime(),它会将 time.struct_time 转换为自1970-01-01 00:00:00 UTC开始的秒数。

 #! / usr / bin / env python 

import time

def timestr_to_secs(timestr):
fmt ='%Y-%m-%d%H:% M:%S'
time_struct = time.strptime(timestr,fmt)
secs = time.mktime(time_struct)
return int(secs)

timestrs = [
'2013-09-03 17:34:04',
'2013-09-03 17:34:05',
'2013-09-03 17:35:04 ',
'1970-01-01 00:00:00'
]

在时间戳中的ts:
打印ts,timestr_to_secs(ts)

我在时区+10,上面的代码给我的输出是:

  2013-09-03 17:34:04 1378193644 
2013-09-03 17:34:05 1378193645
2013-09-03 17:35:04 1378193704
1970-01-01 00:00:00 -36000

当然,对于直方图制作,您可能希望从这些数字中减去方便的基准时间。






这是一个更好的版本,灵感来自于a评论者:J. F. Sebastian。

 #! / usr / bin / env python 

import time
import calendar

def timestr_to_secs(timestr):
fmt ='%Y-%m- %d%H:%M:%S'
time_struct = time.strptime(timestr,fmt)
secs = calendar.timegm(time_struct)
return secs

timrs = [
'2013-09-03 17:34:04',
'2013-09-03 17:34:05',
'2013-09-03 17: 35:04',
'1970-01-01 00:00:00'
]

在时间戳中的ts:
print ts,timestr_to_secs(ts)

输出



< pre class =lang-none prettyprint-override> 2013-09-03 17:34:04 1378229644
2013-09-03 17:34:05 1378229645
2013- 09-03 17:35:04 1378229704
1970-01-01 00:00:00 0






每当我想到使用localtime()时可能出现的问题,我都想起了这个经典的例子,这是我多年前的一个朋友发生的。 >

程序员谁是FidoNet的常规贡献者C_ECHO已经为啤酒厂编写过程控制代码。不幸的是,他的代码使用localtime()而不是gmtime(),当啤酒厂的计算机在夏令时结束时自动调整其时钟时,会产生意想不到的后果。那天早上,当地时间凌晨2点发生两次。所以他的计划重复了这个过程,它已经在第一次凌晨2点进行了,这是为了开始填充一个相当大的桶用啤酒成分。你可以想像,啤酒厂的地板是一团糟。 :)


I need to make a histogram of events over a period of time. My dataset gives me the time of each event in the format ex. 2013-09-03 17:34:04, how do I convert this into something I'm able to plot in a histogram i Python? I know how to do it the other way around with the datetime and time commands.

By the way my dataset contains above 1.500.000 datapoint, so please only solutions that can be automated by loops or something like that ;)

解决方案

Use time.strptime() to convert the local time string to a time.struct_time and then time.mktime(), which will convert the time.struct_time to the number of seconds since 1970-01-01 00:00:00, UTC.

#! /usr/bin/env python

import time

def timestr_to_secs(timestr):
    fmt = '%Y-%m-%d %H:%M:%S'
    time_struct = time.strptime(timestr, fmt)
    secs = time.mktime(time_struct)
    return int(secs)

timestrs = [
    '2013-09-03 17:34:04',
    '2013-09-03 17:34:05',
    '2013-09-03 17:35:04',
    '1970-01-01 00:00:00'
]

for ts in timestrs:
    print ts,timestr_to_secs(ts)

I'm in timezone +10, and the output the above code gives me is:

2013-09-03 17:34:04 1378193644
2013-09-03 17:34:05 1378193645
2013-09-03 17:35:04 1378193704
1970-01-01 00:00:00 -36000

Of course, for histogram-making purpose you may wish to subtract a convenient base time from these numbers.


Here's a better version, inspired by a comment by J. F. Sebastian.

#! /usr/bin/env python

import time
import calendar

def timestr_to_secs(timestr):
    fmt = '%Y-%m-%d %H:%M:%S'
    time_struct = time.strptime(timestr, fmt)
    secs = calendar.timegm(time_struct)
    return secs

timestrs = [
    '2013-09-03 17:34:04',
    '2013-09-03 17:34:05',
    '2013-09-03 17:35:04',
    '1970-01-01 00:00:00'
]

for ts in timestrs:
    print ts,timestr_to_secs(ts)

output

2013-09-03 17:34:04 1378229644
2013-09-03 17:34:05 1378229645
2013-09-03 17:35:04 1378229704
1970-01-01 00:00:00 0


Whenever I think about the problems that can arise from using localtime() I'm reminded of this classic example that happened to a friend of mine many years ago.

A programmer who was a regular contributor to the FidoNet C_ECHO had written process control code for a brewery. Unfortunately, his code used localtime() instead of gmtime(), which had unintended consequences when the brewery computer automatically adjusted its clock at the end of daylight saving. On that morning, localtime 2:00 AM happened twice. So his program repeated the process that it had already performed the first time 2:00 AM rolled around, which was to initiate the filling of a rather large vat with beer ingredients. As you can imagine, the brewery floor was a mess. :)

这篇关于从日期到可用值Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆