gpt4 book ai didi

python - 在日期时间中转换 DataFrame 列类型

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:18 24 4
gpt4 key购买 nike

如何将 DataFrame 字符串列(以正时间增量的 d.days、d.seconds、d.microseconds 格式)转换为日期时间。 ?

提前致谢!

最佳答案

此回答由unutbu提供我认为特别有用。

def combine64(years, months=1, days=1, weeks=None, hours=None, minutes=None,
seconds=None, milliseconds=None, microseconds=None, nanoseconds=None):
"""
* Function: combine64
* Usage: df['combined']= combine64(df['Year'], df['Month'], df['Day']) . . .
* -------------------------------
* This function returns Datetime in the format YYYY-MM-DD:HH:mm:ss:ms:µs:ns
"""
years = np.asarray(years) - 1970
months = np.asarray(months) - 1
days = np.asarray(days) - 1
types = ('<M8[Y]', '<m8[M]', '<m8[D]', '<m8[W]', '<m8[h]',
'<m8[m]', '<m8[s]', '<m8[ms]', '<m8[us]', '<m8[ns]')
vals = (years, months, days, weeks, hours, minutes, seconds,
milliseconds, microseconds, nanoseconds)
return sum(np.asarray(v, dtype=t) for t, v in zip(types, vals)
if v is not None)

关于python - 在日期时间中转换 DataFrame 列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39289012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com