繁体   English   中英

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

[英]Python: Converting a seconds to a datetime format in a dataframe column

目前我正在使用一个大数据框(12x47800)。 十二列之一是由整数秒组成的列。 我想将此列更改为由 datetime.time 格式组成的列。 Schedule 是我的数据框,我在其中尝试更改名为“depTime”的列。 因为我希望它是一个 datetime.time 并且它可能跨越午夜,所以我添加了 if 语句。 这“有效”,但确实像人们想象的那样慢。 有没有更快的方法来做到这一点? 我当前的代码,我唯一可以工作的是:

for i in range(len(schedule)):
    t_sec = schedule.iloc[i].depTime
    [t_min, t_sec] = divmod(t_sec,60)
    [t_hour,t_min] = divmod(t_min,60)
    if t_hour>23:
        t_hour -= 23
    schedule['depTime'].iloc[i] = dt.time(int(t_hour),int(t_min),int(t_sec))

在此先感谢各位。

Ps:我对 Python 还很陌生,所以如果有人能帮助我,我将不胜感激 :)

我正在添加一个比原始解决方案快得多的新解决方案,因为它依赖于熊猫矢量化函数而不是循环(熊猫应用函数本质上是对数据进行优化的循环)。

我使用与您的大小相似的样本对其进行了测试,差异是从 778 毫秒到 21.3 毫秒。 所以我绝对推荐新版本。

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

新(更快)选项:

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

原始(较慢)答案:

不是最优雅的解决方案,但它确实有效。

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

您应该尽量不要对数据帧进行全面扫描,而是使用矢量化访问,因为它通常效率更高。

幸运的是,pandas 有一个函数可以完全满足您的要求, to_timedelta

schedule['depTime'] = pd.to_timedelta(schedule['depTime'], unit='s')

这是不是一个真正的日期时间格式,但它是一个等价的大熊猫datetime.timedelta ,是一种方便型的处理时间。 您可以使用to_datetime但将以接近 1970-01-01 的完整日期时间结束...

如果你真的需要datetime.time对象,你可以这样得到它们:

schedule['depTime'] = pd.to_datetime(schedule['depTime'], unit='s').dt.time

但它们在熊猫数据框中使用起来不太方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM