简体   繁体   中英

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. This is being used in 'read_csv" as a converter.

For example my data looks like this: "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.

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.to_datetime('01-SEP-18 01.30.30.000000 AM', format='%d-%b-%y %I.%M.%S.%f %p')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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