I want to plot RESULT vs TIME based on a testresult.csv
file that has following format, and I have trouble to get the TIME column's datatype defined properly.
TIME,RESULT
03/24/2016 12:27:11 AM,2
03/24/2016 12:28:41 AM,76
03/24/2016 12:37:23 AM,19
03/24/2016 12:38:44 AM,68
03/24/2016 12:42:02 AM,44
...
To read the csv file, this is the code I wrote:
raw_df = pd.read_csv('testresult.csv', index_col=None, parse_dates=['TIME'], infer_datetime_format=True)
This code works, but it is extremely slow, and I assume that the infer_datetime_format
takes time. So I tried to read in the csv by default first, and then convert the object dtype 'TIME' to datetime dtype by using to_datetime()
, and I hope by defining the format, it might expedite the speed.
raw_df = pd.read_csv('testresult.csv')
raw_df.loc['NEWTIME'] = pd.to_datetime(raw_df['TIME'], format='%m/%d%Y %-I%M%S %p')
This code complained error:
"ValueError: '-' is a bad directive in format '%m/%d%Y %-I%M%S %p'"
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…