在Excel Export上从Pandas DateTime中删除时间 [英] Remove Time From Pandas DateTime on Excel Export

查看:56
本文介绍了在Excel Export上从Pandas DateTime中删除时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用pyODBC查询的Pandas Dataframe,它以浮点数返回日期".在转换为ymd格式的字符串后,我将数据类型更改为datetime,然后使用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 12017-01-192016-12-292017-01-042016-12-292017-01-04writer = ExcelWriter('MyData.xlsx',date_format ='yyyy mm dd')df.to_excel(writer,'DATA')writer.save()2017-01-19 00:00:002016-12-29 00:00:002017-01-04 00:00:002016-12-29 00:00:002017-01-04 00:00:002017-01-04 00:00:002017-01-16 00:00:00 

解决方案

我认为 pandas 使用 datetime 存在问题,因此需要定义 datetime_format

I have a Pandas Dataframe queried with pyODBC that returns 'dates' as floats. I change the data types to datetime after converting to a string with ymd formatting and then create an Excel File with ExcelWriter. The resulting Excel data keeps the yyyymmdd 00:00:00 format. Some posts suggest creating 'helper' columns in Pandas and using dt.normalize (?) but I would like to do it all on export ... possible? Better way in general?

Note [date] is a list of three columns

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

解决方案

I think there is problem pandas use datetime, so need define datetime_format also.

Docs.

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

这篇关于在Excel Export上从Pandas DateTime中删除时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆