【问题标题】:Unknown string format on pd.to_datetimepd.to_datetime 上的未知字符串格式
【发布时间】:2019-05-01 10:09:27
【问题描述】:

我有一个数据集,其中包含这样的列日期:

cod           date              value 
0   1O8        2015-01-01 00:00:00    2.1
1   1O8        2015-01-01 01:00:00    2.3
2   1O8        2015-01-01 02:00:00    3.5
3   1O8        2015-01-01 03:00:00    4.5
4   1O8        2015-01-01 04:00:00    4.4
5   1O8        2015-01-01 05:00:00    3.2
6   1O9        2015-01-01 00:00:00    1.4
7   1O9        2015-01-01 01:00:00    8.6
8   1O9        2015-01-01 02:00:00    3.3
10  1O9        2015-01-01 03:00:00    1.5
11  1O9        2015-01-01 04:00:00    2.4
12  1O9        2015-01-01 05:00:00    7.2

日期列的dtypes 是一个对象,用于在我需要将日期列类型更改为数据时间之后应用一些功能。我尝试了不同的解决方案,例如:

pd.to_datetime(df['date'], errors='raise', format ='%Y-%m-%d HH:mm:ss')
pd.to_datetime(df['date'], errors='coerce', format ='%Y-%m-%d HH:mm:ss')
df['date'].apply(pd.to_datetime, format ='%Y-%m-%d HH:mm:ss')

但错误只是一样的:

TypeError: Unrecognized value type: <class 'str'>
ValueError: Unknown string format

直接的事情是,如果我将 te 函数应用于数据集样本,该函数会正确响应,但如果我将其应用于所有数据集,则会退出错误。在数据中没有缺失值,并且所有值的 dtype 都相同。

我该如何解决这个错误?

【问题讨论】:

  • 您是否尝试过不指定任何格式,即pd.to_datetime(df.date)
  • 是的,这是我尝试的第一件事
  • 因为它通常会推断格式。正如@jpp 所提到的,您在字符串格式的开头缺少%

标签: python python-3.x pandas string datetime


【解决方案1】:

存在三个问题:

  1. pd.to_datetimepd.Series.apply 不能正常工作,因此您的解决方案不会修改您的系列。转换后重新分配。
  2. 您的第三个解决方案需要errors='coerce' 以保证没有错误。
  3. 对于时间组件,您需要使用以% 开头的特定字符串格式。

所以你可以使用:

df = pd.DataFrame({'date': ['2015-01-01 00:00:00', '2016-12-20 15:00:20',
                            '2017-08-05 00:05:00', '2018-05-11 00:10:00']})

df['date'] = pd.to_datetime(df['date'], errors='coerce', format='%Y-%m-%d %H:%M:%S')

print(df)

                  date
0  2015-01-01 00:00:00
1  2016-12-20 15:00:20
2  2017-08-05 00:05:00
3  2018-05-11 00:10:00

在这种特殊情况下,格式是标准的,可以省略:

df['date'] = pd.to_datetime(df['date'], errors='coerce')

【讨论】:

  • 我尝试使用这种格式:format='%Y-%m-%d %H:%M:%S.%f' ,但现在的错误是:ValueError: time data 'dVal' doesn't match format specified。理论上格式是正确的
  • @jjgasse,您的输入与您的格式不符,请参阅我的示例,没有.%f。看起来你仍然没有使用errors='coerce'
【解决方案2】:

我了解到您从 csv 文件中读取了这些数据。

df=pd.read_csv('c:/1/comptagevelo2012.csv', index_col=0, parse_dates=True)

检查:

print(df.index) 

比 pd.to_datetime 更好用!!我查过了!

> DatetimeIndex(['2012-01-01', '2012-02-01', '2012-03-01', '2012-04-01',
>                '2012-05-01', '2012-06-01', '2012-07-01', '2012-08-01',
>                '2012-09-01', '2012-10-01',
>                ...
>                '2012-12-22', '2012-12-23', '2012-12-24', '2012-12-25',
>                '2012-12-26', '2012-12-27', '2012-12-28', '2012-12-29',
>                '2012-12-30', '2012-12-31'],
>               dtype='datetime64[ns]', length=366, freq=None)

另一种方法不适用于此文件。

df=pd.read_csv('c:/1/comptagevelo2012.csv',index_col=0)
pd.to_datetime(df['Date'],  errors='coerce', format ='%d/%m/%Y')
print(df.index)

Index(['01/01/2012', '02/01/2012', '03/01/2012', '04/01/2012', '05/01/2012',
       '06/01/2012', '07/01/2012', '08/01/2012', '09/01/2012', '10/01/2012',
       ...
       '22/12/2012', '23/12/2012', '24/12/2012', '25/12/2012', '26/12/2012',
       '27/12/2012', '28/12/2012', '29/12/2012', '30/12/2012', '31/12/2012'],
      dtype='object', length=366)

来源:https://keyrus-gitlab.ml/gfeuillen/keyrus-training/blob/5f0076e3c61ad64336efc9bc3fd862bfed53125c/docker/data/python/Exercises/02%20pandas/comptagevelo2012.csv

【讨论】:

    猜你喜欢
    • 2021-01-08
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2021-10-07
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    相关资源
    最近更新 更多