【问题标题】:Python 2.7 CSV graph time formatPython 2.7 CSV 图形时间格式
【发布时间】:2015-08-20 14:42:41
【问题描述】:

我有一个 CSV 文件,其中一个冒号值是时间戳,但是当我使用 numpy.getfromtxt 时,它会将其更改为字符串。我的目标是创建一个图表,但使用正常的时间格式,我只喜欢秒。

这是我从下面的代码中得到的数组:

array([('0:00:00',), ('0:00:00.001000',), ('0:00:00.002000',),
 ('0:00:00.081000',), ('0:00:00.095000',), ('0:00:00.195000',),
 ('0:00:00.294000',), ...

这是我的代码:

col1 = numpy.genfromtxt("mycsv.csv",usecols=(1),delimiter=',',dtype=None, names=True)

我有这种格式的问题是字符串,但我需要它在几秒钟内(我们可以被忽略或不被忽略)。我怎样才能做到这一点?

【问题讨论】:

  • 你的问题是什么?
  • 如何将字符串格式改为秒
  • 请更新您的问题。
  • 看起来这些时间戳是h:m:s.ms - 纪元是多少?

标签: python-2.7 csv numpy


【解决方案1】:

如果可以,在 python 中处理csv 文件的最佳方法是使用pandas。它会为您解决这个问题。我将假设时间列的名称是time,将其更改为您使用的任何名称:

>>> import numpy as np
>>> import pandas as pd
>>> 
>>> df = pd.read_csv('test.csv', parse_dates=[1])  # read time as date
>>> print(df)
   test1                    time  test2  test3
0      5 2015-08-20 00:00:00.000     10   11.7
1      5 2015-08-20 00:00:00.001     11   11.6
2      5 2015-08-20 00:00:00.002     12   11.5
3      5 2015-08-20 00:00:00.081     13   11.4
4      5 2015-08-20 00:00:00.095     14   11.3
5      5 2015-08-20 00:00:00.195     15   11.2
6      5 2015-08-20 00:00:00.294     16   11.1
>>> df['time'] -= pd.datetime.now().date()  # convert to timedelta
>>> print(df)
   test1            time  test2  test3
0      5        00:00:00     10   11.7
1      5 00:00:00.001000     11   11.6
2      5 00:00:00.002000     12   11.5
3      5 00:00:00.081000     13   11.4
4      5 00:00:00.095000     14   11.3
5      5 00:00:00.195000     15   11.2
6      5 00:00:00.294000     16   11.1
>>> df['time'] /= np.timedelta64(1,'s')  # convert to seconds
>>> print(df)    
   test1   time  test2  test3
0      5  0.000     10   11.7
1      5  0.001     11   11.6
2      5  0.002     12   11.5
3      5  0.081     13   11.4
4      5  0.095     14   11.3
5      5  0.195     15   11.2
6      5  0.294     16   11.1

您可以使用与 numpy 数组相同的大部分方式(包括绘图)来使用 pandas 数据框(您在此处拥有的)和系列(您将获得单个列,例如 df['time'])。但是,如果您真的非常需要将其转换为 numpy 数组,那么就像arr = df['time'].values 一样简单。

【讨论】:

  • 这个模块 pandas 很棒。谢谢!
【解决方案2】:

使用datetime

 import datetime

 for x in array:
     for y .... # it's not realy obvious what the nesting is here...
        timestamp = datetime.strptime(y, '%H:%M:%S.%f')

【讨论】:

  • 你知道,如果你问一个明确的问题,你会更快得到一个好的答案
  • @scytale:你自己试过吗?看起来您对 strptime 的参数颠倒了。此外,strptime 返回一个 struct_time (docs.python.org/2/library/time.html#time.struct_time),它不支持小数秒。
  • 我在考虑日期时间。是的,参数是相反的。
  • 示例中的第一个字符串是'0:00:00'。使用此输入,您对strptime 的调用将失败,因为没有小数点。
【解决方案3】:

您可以对时间戳字段使用转换器。

例如,假设times.dat 包含:

time
0:00:00
0:00:00.001000
0:00:00.002000
0:00:00.081000
0:00:00.095000
0:00:00.195000
0:00:00.294000

定义一个转换器,将时间戳字符串转换为作为浮点值的秒数:

In [5]: def convert_timestamp(s):
   ...:     h, m, s = [float(w) for w in s.split(':')]
   ...:     return h*3600 + m*60 + s
   ...: 

然后使用genfromtxt中的转换器:

In [21]: genfromtxt('times.dat', skiprows=1, converters={0: convert_timestamp})
Out[21]: array([ 0.   ,  0.001,  0.002,  0.081,  0.095,  0.195,  0.294])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多