如何转换或格式日期数据?
我有这样的日期格式。
2013-12-11 00:00:00
首先,我想问,尽管我的Excel文件没有任何时间信息,但是当我将Excel文件导入到数据框中时,为什么它显示上述时间和日期信息?这是默认设置吗?其次,我试图给出%y-%m-%d%h:%m:%sz
格式,但给予;
valueerror:时间数据'日期'不匹配格式'%y-%m-%d%h:%m:%sz'(匹配)
我如何按顺序格式化这种日期数据使用它?谢谢
编辑:忽略时间数据对我来说也很有用;
2013-12-11
Edit2:这是我的代码;
df2['group'] = df2.groupby(pd.to_datetime(df2.loc[:,0], format='%Y-%m-%d %H:%M:%SZ').dt.to_period('M')).ngroup() + 1
edit3:这是我的dataframe
它仍然给出value value eReror:time dation numune dimune dimune dimune dimune dimune do t匹配格式指定
错误时,我将代码更改为
df2['group'] = df2.groupby(pd.to_datetime(df2.loc[:,0], format='%Y-%m-%d %H:%M:%S').dt.to_period('M')).ngroup() + 1
I have such a date format.
2013-12-11 00:00:00
Firstly, I would like to ask that although my excel file doesnt have any time information, when I import my excel file into dataframe, why does it show time and date information like above? Is this a default setting? Secondly, I tried to give %Y-%m-%d %H:%M:%SZ
format but it gives;
ValueError: time data 'date' does not match format '%Y-%m-%d %H:%M:%SZ' (match)
How can I format this kind of date data in order to use it? Thanks
Edit: Ignoring time data can also be very usefull for me like below;
2013-12-11
Edit2: Here is my code;
df2['group'] = df2.groupby(pd.to_datetime(df2.loc[:,0], format='%Y-%m-%d %H:%M:%SZ').dt.to_period('M')).ngroup() + 1
Edit3: Here is my dataframe
It still gives ValueError: time data NUMUNE doesn't match format specified
error when I change my codes as
df2['group'] = df2.groupby(pd.to_datetime(df2.loc[:,0], format='%Y-%m-%d %H:%M:%S').dt.to_period('M')).ngroup() + 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用DateTime:
这将创建一个DateTime对象,您可以与其他DateTime对象一起在操作中使用。
You could try using datetime instead:
This will create a datetime object that you can use in operations with other datetime objects.
请问你是如何保存或生成excel文件的?我猜测 Excel 文件中的日期字符串来自您生成它的方式。
按如下方式格式化日期字符串,包括时间(如 Excel 工作表中所示),
这会将字符串转换为可以在代码中使用的日期时间对象(即“date_obj”)
May I ask how you are saving or generating the excel file? I'm guessing that the date string in the excel file is coming from the way you are generating it.
format the date string as follows, including the time (as in you excel sheet)
this converts the string to datetime object (that is "date_obj") that you can use in your code