Python 日期时间格式为 yyyy-mm

发布于 2025-01-14 12:26:18 字数 278 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文