简体   繁体   English

熊猫to_datetime断言错误引发错误

[英]Pandas to_datetime assertion error is throwing an error

My python is throwing an assertion error when converting a date in string format to a datetime format. 将字符串格式的日期转换为日期时间格式时,我的python引发断言错误。 This is being used in 'read_csv" as a converter. 这在'read_csv'中用作转换器。

For example my data looks like this: "01-SEP-18 01.30.30.000000 AM" 例如,我的数据如下所示: "01-SEP-18 01.30.30.000000 AM"

As far as I can tell the format should be the below. 据我所知,格式应为以下格式。 This is not my exact code, but I included the string rather than expressing my converter. 这不是我的确切代码,但是我包含了字符串而不是表示我的转换器。 I am aware that to_datetime is relatively smart and tried without a format only to receive a similar/same error. 我知道to_datetime相对来说比较聪明,尝试不使用格式只是为了接收相似/相同的错误。

pn.to_datetime('01-SEP-18 01.30.30.000000 AM','%d-%b-%y %I.%M.%S.%f %p')
pn.to_datetime('01-SEP-18 01.30.30.000000 AM')


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 469, in to_datetime
    result = _convert_listlike(np.array([arg]), box, format)[0]
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py", line 368, in _convert_listlike
    require_iso8601=require_iso8601
  File "pandas\_libs\tslib.pyx", line 492, in pandas._libs.tslib.array_to_datetime
  File "pandas\_libs\tslib.pyx", line 513, in pandas._libs.tslib.array_to_datetime

AssertionError
import pandas as pd
pd.to_datetime('01-SEP-18 01.30.30.000000 AM',format='%d-%b-%y %I.%M.%S.%f %p')

You're right there - the format you pass isn't an argument, but a keyword argument, so it needs to be specified as the format. 您就在那里-您传递的格式不是参数,而是关键字参数,因此需要将其指定为格式。 This should get the result you need (assuming pandas is imported as pn): 这应该得到您需要的结果(假设将熊猫作为pn导入):

pn.to_datetime('01-SEP-18 01.30.30.000000 AM', format='%d-%b-%y %I.%M.%S.%f %p')

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

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