Python 日期时间格式为 yyyy-mm
我有一个 csv 文件,其中包含日期数据,格式为“yyyy-mm-dd”或“yyyy-mm”。
当我导入 csv 文件并转换为数据框时,日期列的数据类型是对象。 我试图将其转换为日期时间,但每当我这样做时,格式为“yyyy-mm”的日期都会转换为格式“yyyy-mm-dd”。
如何将数据从对象转换为日期但保留“yyyy-mm”格式? 我尝试过“pd.to_datetime”、“strptime”和“strftime”方法,这些方法都会导致我的数据更改为“yyyy-mm-dd”格式。
感谢您的帮助
Im have a csv file with date data which is formatted as 'yyyy-mm-dd' or 'yyyy-mm'.
When i import the csv file and convert to a dataframe, the datatype of my date column is object.
Im trying to convert this to datetime, but whenever i do, the dates which are formatted 'yyyy-mm' get converted to format 'yyyy-mm-dd'.
How can i convert the data from object to date but preserve the 'yyyy-mm' format?
Ive tried the methods 'pd.to_datetime', 'strptime' and 'strftime' which all result in my data changing to 'yyyy-mm-dd' format.
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论