且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:在数据框列中将秒转换为日期时间格式

更新时间:2023-11-07 18:38:40

我要添加一个比原始解决方案快得多的新解决方案,因为它依赖于熊猫矢量化函数而不是循环(pandas apply函数本质上是经过优化的循环)数据).

I'm adding a new solution which is much faster than the original since it relies on pandas vectorized functions instead of looping (pandas apply functions are essentially optimized loops on the data).

我用大小与您相似的样本对其进行了测试,其差异为778ms至21.3ms.因此,我绝对推荐新版本.

I tested it with a sample similar in size to yours and the difference is from 778ms to 21.3ms. So I definitely recommend the new version.

这两种解决方案都基于将秒整数转换为timedelta格式并将其添加到参考日期时间.然后,我只需捕获结果日期时间的时间部分.

Both solutions are based on transforming your seconds integers into timedelta format and adding it to a reference datetime. Then, I simply capture the time component of the resulting datetimes.

新的(更快的)选项:

import datetime as dt

seconds = pd.Series(np.random.rand(50)*100).astype(int) # Generating test data

start = dt.datetime(2019,1,1,0,0) # You need a reference point

datetime_series = seconds.astype('timedelta64[s]') + start

time_series = datetime_series.dt.time

time_series

原始(较慢)答案:

这不是最优雅的解决方案,但是可以解决问题.

Not the most elegant solution, but it does the trick.

import datetime as dt

seconds = pd.Series(np.random.rand(50)*100).astype(int) # Generating test data

start = dt.datetime(2019,1,1,0,0) # You need a reference point

time_series = seconds.apply(lambda x: start + pd.Timedelta(seconds=x)).dt.time