如何在python

时间:2019-03-31 15:24:13

标签: python date

如何正确定义日期列?

我正在读取包含日期列的csv文件,并将其写到Excel文件中。

使用pd.to_datetime我可以获取表示为datetime的值。但是,由于我的输入仅是日期,因此时间值显示为零。

我尝试使用 format ='%d /%m /%Y ,但是失败了,因为我的日期和月份值不是零填充–

ValueError: time data '07/18/2016' does not match format '%d/%m/%Y' (match)

import pandas as pd
from datetime import datetime, date

jobs_file = "test01.csv"
output_file = jobs_file.replace('csv', 'xlsx')

jobs_path = "C:\\Users\\Downloads\\" + jobs_file
output_path = "C:\\Users\\Downloads\\" + output_file

jobs = pd.read_csv(jobs_path,)

jobs['LAST RUN'] = pd.to_datetime(jobs['LAST RUN'])
#jobs['LAST RUN'] = pd.to_datetime(jobs['LAST RUN'], format='%d/%m/%Y')

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(output_path, engine='xlsxwriter',
                        date_format='mm/dd/yyyy')

# Convert the dataframe to an XlsxWriter Excel object.
jobs.to_excel(writer, sheet_name='Jobs', index=False)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

我的输入值如下-

7/18/2016

输出看起来像这样-

7/18/2016  12:00:00 AM

我希望它只是日期-

7/18/2016

0 个答案:

没有答案