Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 735
krewsayder Avatar asked May 01 '26 16:05

krewsayder


2 Answers

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')
like image 111
AlecZ Avatar answered May 03 '26 04:05

AlecZ


import pandas as pd
pd.to_datetime('01-SEP-18 01.30.30.000000 AM',format='%d-%b-%y %I.%M.%S.%f %p')
like image 32
artona Avatar answered May 03 '26 05:05

artona



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!