python - 从 Excel 导出的 Pandas DateTime 中删除时间

标签 python pandas datetime

我有一个使用 pyODBC 查询的 Pandas Dataframe,它返回“日期”作为 float 。在转换为具有 ymd 格式的字符串后,我将数据类型更改为日期时间,然后使用 ExcelWriter 创建一个 Excel 文件。生成的 Excel 数据保留 yyyymmdd 00:00:00 格式。一些帖子建议在 Pandas 中创建“helper”列并使用 dt.normalize (?),但我想在导出时完成这一切......可能吗?一般来说更好的方法?

注意[日期]是三列的列表

df[date] = df[date].apply(lambda x: pd.to_datetime(x.astype(str), format = '%Y%m%d'))

df Col 1

2017-01-19 
2016-12-29 
2017-01-04
2016-12-29
2017-01-04  

writer = ExcelWriter('MyData.xlsx', date_format = 'yyyy mm dd')

df.to_excel(writer, 'DATA')

writer.save()

2017-01-19 00:00:00
2016-12-29 00:00:00
2017-01-04 00:00:00
2016-12-29 00:00:00
2017-01-04 00:00:00
2017-01-04 00:00:00
2017-01-16 00:00:00

最佳答案

我认为pandas使用datetime有问题,所以还需要定义datetime_format

Docs .

writer = pd.ExcelWriter('MyData.xlsx', 
                        date_format = 'yyyy mm dd', 
                        datetime_format='yyyy mm dd')

pic

关于python - 从 Excel 导出的 Pandas DateTime 中删除时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691675/

相关文章:

python - 编写测试运行程序时是否可以进行 TDD?

python - 何时在 python 中使用 unicode(string) 和 string.encode ('utf-8' )

python - 扩展一个以 C 对象作为参数的 Python 函数

python - 将重复数据从行转换为列

python - 使用相同的键、多个值追加到字典

python - 加载前 100 行 excel

python - Pandas :TypeError: float() argument must be a string or a number, 不是 'pandas._libs.interval.Interval'

sql - Oracle:避免 to_date 中出现 NULL 值

php - 将 30 分钟添加到 datetime php/mysql

python - 比较python中的日期