你真的有几个选择。我不完全确定当你试图直接用一个'pd.read_csv文件'但正如上面建议的那样,您可以定义一组格式字符串,您可以尝试使用这些字符串来解析数据。在
另一个选择是将date列作为字符串读入,然后自己解析它。如果希望列类似于“YYYY-MM-DD”,则解析字符串以仅包含该数据,然后将其保存回,如下所示。在import pandas as prandas
import datetime
df = prandas.read_csv('supa_kewl_data.dis_fmt_rox', dtype={'start_date': str})
print df.head()
# we are interested in start_date
date_strs = df['start_date'].values
#YYYY-MM-DD
#012345678910
filter_date_strs = [x[0:10] for x in date_strs]
df['filter_date_strs] = filter_date_strs
# sometimes i've gotten complained at by pandas for doing this
# try doing df.loc[:,'filter_date_strs'] = filter_date_strs
# if you get some warning thing
# if you want you can convert back to date time using a
dobjs = [datetime.datetime.strptime(x,'%Y-%m-%d') for x in filter_date_strs]
df['dobj_start_date'] = dobjs
df.to_csv('even_better_data.csv', index=False)