简体   繁体   English

将带字母的时间戳转换为日期时间

[英]converting timestamp with letters into datetime

I have a txt file with data and values like this one:我有一个 txt 文件,其中包含如下数据和值:

PP  C   timestamp   HR  RMSSD   SCL 
PP1 1   20120918T131600000  NaN NaN 80.239727 
PP1 1   20120918T131700000  61  0.061420    77.365127

and I am importing it like that:我正在像这样导入它:

df = pd.read_csv('data.txt','\t', header=0)

which gives me a nice looking dataframe:这给了我一个漂亮的数据框: 在此处输入图像描述

Running跑步

df.columns

shows this result Index(['PP', 'C', 'timestamp', 'HR', 'RMSSD', 'SCL'], dtype='object') .显示此结果Index(['PP', 'C', 'timestamp', 'HR', 'RMSSD', 'SCL'], dtype='object')

Now when I am trying to convert the timestamp column into a datetime column:现在,当我尝试将时间戳列转换为日期时间列时:

df["datetime"] = pd.to_datetime(df["timestamp"], format='%Y%m%dT%H%M%S%f')

I get this: ValueError: time data 'timestamp' does not match format '%Y%m%dT%H%M%S%f' (match)我明白了: ValueError: time data 'timestamp' does not match format '%Y%m%dT%H%M%S%f' (match)

Any ideas would be appreciated.任何想法,将不胜感激。

First, the error message you're quoting is from the header row.首先,您引用的错误消息来自标题行。 It's trying to parse the literal string 'timestamp' as a timestamp, which is failing.它试图将文字字符串'timestamp'解析为时间戳,但失败了。 If you're getting an error on an actual data row, show us that message.如果您在实际数据行中遇到错误,请向我们显示该消息。

All three of your posted data rows parse fine with your format in my testing:在我的测试中,您发布的所有三个数据行都可以按照您的格式解析:

>>> [pandas.to_datetime(s, format='%Y%m%dT%H%M%S%f') 
    for s in ['20120918T131600000', '20120918T131700000', 
              '20120918T131800000']]
[Timestamp('2012-09-18 13:16:00'), Timestamp('2012-09-18 13:17:00'), Timestamp('2012-09-18 13:18:00')]

I have no idea where you got format='%Y%m%dT%H%M%S%f'[:-3] , which just removes the S%f from the format string, leaving it invalid.我不知道你从哪里得到format='%Y%m%dT%H%M%S%f'[:-3] ,它只是从格式字符串中删除S%f ,使其无效。 If you want to remove the last three digits of the data so that you ca just use %H%M%S instead of %H%M%S%f , you need to put the [:-3] on the timestamp data value, not the format.如果要删除数据的最后三位以便只使用%H%M%S而不是%H%M%S%f ,则需要将[:-3]放在时间戳数据值上,而不是格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM