gpt4 book ai didi

python - numpy 数组中的时间值到日期时间数组

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

有一个大数组,其中 column[0] 对应于日期,col[1]=month,col[2]=year 和 col[3]=hours(后者是一个 float ,还包含有关分钟的信息和分数中的秒),将这些列转换为日期时间数组的最有效方法是什么?

更新如下:我修改了 dt.datetime 函数,因此它可以处理数组输入以及小数年、月等等。我还没有对此进行彻底的测试,可能有更优雅的方法可以做到这一点,但这里是。

from __future__import division

def getrem(input):
"this function yields the value behind the decimal point"
import numpy as np
output=abs(input-np.fix(input))
return output

def datenum(Yr,Mo=1,Da=1,Hr=0,Mi=0,Se=0,Ms=0):
"this function works as regular datetime.datetime, but allows for float input"
import numpy as np
import datetime as dt
import calendar

#correct faulty zero input
if Mo<1:
Mo+=1
if Da<1:
Da+=1

#distribute the year fraction over days
if getrem(Yr)>0:
if calendar.isleap(np.floor(Yr)):
fac=366
else:
fac=365
Da=Da+getrem(Yr)*fac
Yr=int(Yr)
#if months exceeds 12, pump to years
while int(Mo)>12:
Yr=Yr+1
Mo=Mo-12
#distribute fractional months to days
if getrem(Mo)>0:
Da=Da+getrem(Mo)*calendar.monthrange(Yr,int(Mo))[1]
Mo=int(Mo)
#datetime input for 28 days always works excess is pumped to timedelta
if Da>28:
extraDa=Da-28
Da=28
else:
extraDa=0
# sometimes input is such that you get 0 day or month values, this fixes this anomaly
if int(Da)==0:
Da+=1
if int(Mo)==0:
Mo+=1

#datetime calculation
mytime=dt.datetime(int(Yr),int(Mo),int(Da))+dt.timedelta(days=extraDa+getrem(Da),hours=Hr,minutes=Mi,seconds=Se,microseconds=Ms)
return mytime

def araydatenum(*args):
mydatetimes=[datenum(*[a.squeeze()[x] for a in args]) for x in range(len(args[0].squeeze()))]
return mydatetimes

最佳答案

不能说效率最高,但可以像这样轻松完成:

import datetime as dt
mydatetimes = [dt.datetime(x[2], x[1], x[0]) + dt.timedelta(hours=x[3]) for x in myarray]

这会创建一个常规的 python 列表,而不是一个 numpy 数组。只需在右侧添加 numpy.array( ... ) 使其成为具有 dtype=object 的数组。

关于python - numpy 数组中的时间值到日期时间数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19769576/

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