将日期的数字表示形式(excel格式)转换为python日期和时间,然后将它们分成两个单独的 pandas 数据框列 [英] convert numerical representation of date (excel format) to python date and time, then split them into two seperate dataframe columns in pandas

查看:122
本文介绍了将日期的数字表示形式(excel格式)转换为python日期和时间,然后将它们分成两个单独的 pandas 数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在电子表格中导入一些数据.在数据框中,但日期以数字表示或格式

I'm importing some data in a spreadsheet. It's in a dataframe, but the date is in a numerical representation or format

41516.43

首先,我想将其转换为日期和时间对象

First, I want to convert it to a date and time object

date_val = 30/08/2013  10:13:26 AM

然后我想将date_val分别拆分为日期和时间,并将它们放在数据框(df)的单独列中

Then I would like to split the date_val into date and time separately, and place them in seperate columns in my dataframe (df)

| **original date** | **date**     | **time** | 
41516.43              30/08/2013      10:13:26 AM

推荐答案

注意:大概略有差异是由于您给定的浮点数舍入了.

Note: presumably slight difference is due to rounding of the float you gave.

裸露指尖的安全带使用风险" 版本:

In [21]: pd.Timestamp('1899-12-30') + (pd.offsets.Day(1).nanos * s).astype(np.timedelta64)
Out[21]:
0   2013-08-30 10:19:12
dtype: datetime64[ns]

我认为通常最好在使用 read_excel .

这篇关于将日期的数字表示形式(excel格式)转换为python日期和时间,然后将它们分成两个单独的 pandas 数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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