微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ValueError: 时间数据与格式“%Y/%m/%d %H:%M:%S”不匹配

如何解决ValueError: 时间数据与格式“%Y/%m/%d %H:%M:%S”不匹配

我需要将数千条日期时间记录转换为时间戳。当我运行以下代码时:

     Date      Time
0  2018/02/11  02:36:59
1  2018/02/12  00:47:11
2  2018/02/12  01:36:36
3  2018/02/12  03:27:51
4  2018/02/12  03:48:29
5  2018/02/12  03:50:49
Traceback (most recent call last):
  File "epoch.py",line 9,in <module>
    element = datetime.datetime.strptime(cepo,'%Y/%m/%d %H:%M:%s')
  File "/Users/joaofontiela/opt/anaconda3/envs/pywork/lib/python3.8/_strptime.py",line 568,in _strptime_datetime
    tt,fraction,gmtoff_fraction = _strptime(data_string,format)
  File "/Users/joaofontiela/opt/anaconda3/envs/pywork/lib/python3.8/_strptime.py",line 349,in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '         Date      Time\n0  2018/02/11  02:36:59\n1  2018/02/12  00:47:11\n2  2018/02/12  01:36:36\n3  2018/02/12  03:27:51\n4  2018/02/12  03:48:29\n5  2018/02/12  03:50:49' does not match format '%Y/%m/%d %H:%M:%s'

我收到以下错误

Date,Time
2018/02/11,02:36:59
2018/02/12,00:47:11
2018/02/12,01:36:36
2018/02/12,03:27:51
2018/02/12,03:48:29
2018/02/12,03:50:49

输入文件(test.csv)看起来像:

public function display($channel_id,$entry_id = '')
{
    $settings = array();

    $settings = array(
        'custom_field' => array(
            'field_id'      => 'custom_field','field_label'       => 'custom_field','field_type'        => 'text'
        )
    );

    return $settings;
}

如何修复错误“时间数据与格式'%Y/%m/%d %H:%M:%s”不匹配?

解决方法

您可以轻松地将 csv 中的“日期”和“时间”列组合起来

# load the csv file content into a dataframe
df = pd.read_csv(filename)

# combine date and time columns and parse to datetime
df['datetime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])

print(df['datetime'])
0   2018-02-11 02:36:59
1   2018-02-12 00:47:11
2   2018-02-12 01:36:36
3   2018-02-12 03:27:51
4   2018-02-12 03:48:29
5   2018-02-12 03:50:49
Name: datetime,dtype: datetime64[ns]

您可以计算 Unix 时间(自 1970-01-01 UTC 以来的 Posix / 秒)

df['posix[s]'] = df['datetime'].astype(int) / 1e9

print(df['posix[s]'])
0    1.518317e+09
1    1.518396e+09
2    1.518399e+09
3    1.518406e+09
4    1.518407e+09
5    1.518407e+09
Name: posix[s],dtype: float64

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。