I am trying to convert a pandas dataframe column to datetime. It was working perfectly until I uninstall and install python again. I am using Python 2.7.10, Anaconda 2.4.0 (64-bit). Pandas version '0.17.1'. Here is my code:
import os, sys
from datetime import date, timedelta
import pandas as pd
import numpy as np
fdf = pd.read_csv(f,delimiter='\t')
fdf['ORDER_DATETIME']=pd.to_datetime(fdf['ORDER_DATETIME'],"%Y/%m/%d %H:%M")
I get the following error message:
File "...\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)
File ".../filename.py", line 245, in fdf['ORDER_DATETIME']=pd.to_datetime(fdf['ORDER_DATETIME'],"%Y/%m/%d %H:%M")
File "C...\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\util\decorators.py", line 89, in wrapper return func(*args, **kwargs)
File "...\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\tseries\tools.py", line 276, in to_datetime unit=unit, infer_datetime_format=infer_datetime_format)
File "...\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\tseries\tools.py", line 390, in _to_datetime values = _convert_listlike(arg._values, False, format)
File "...\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\tseries\tools.py", line 372, in _convert_listlike require_iso8601=require_iso8601)
File "pandas\tslib.pyx", line 1847, in pandas.tslib.array_to_datetime (pandas\tslib.c:37155)
File "pandas\tslib.pyx", line 1864, in pandas.tslib.array_to_datetime (pandas\tslib.c:33604)
AssertionError
Just figured it out:
fdf['ORDER_DATETIME']=pd.to_datetime(fdf.loc[:,'ORDER_DATETIME'],format="%Y/%m/%d %H:%M")
Adding "format=" solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With