首页 > 解决方案 > Astral 封装和矢量化

问题描述

我有一个DataFrame,我们就叫它吧f

当我执行 .head() 时,它看起来像这样:

             DateTime   M     m_vol        0        1        2     trend  \
0 2020-01-01 00:00:00  01  0.627109  4.54369  5.43625  4.61789 -0.283433   
1 2020-01-01 01:00:00  01  0.627109  3.97237  4.74114  5.41678 -0.283402   
2 2020-01-01 02:00:00  01  0.627109  3.97531   4.9563  4.98747 -0.283371   
3 2020-01-01 03:00:00  01  0.627109  4.41567  4.83747  5.60839 -0.283339   
4 2020-01-01 04:00:00  01  0.627109  5.55491  3.87717  5.88752 -0.283308   

  seasonal_t  cdd_0  cdd_1  cdd_2      hdd_0      hdd_1      hdd_2        Date  
0        4.6    0.0    0.0    0.0  13.456306  12.563752  13.382110  2020-01-01  
1        4.6    0.0    0.0    0.0  14.027634  13.258855  12.583221  2020-01-01  
2        4.6    0.0    0.0    0.0  14.024687  13.043696  13.012531  2020-01-01  
3        4.5    0.0    0.0    0.0  13.584329  13.162528  12.391608  2020-01-01  
4        4.5    0.0    0.0    0.0  12.445088  14.122825  12.112478  2020-01-01  

我想从列日期计算每个日期的日出和日落,如果它们是阳光的话,所以如果 DateTime 高于日出时间且小于该日期的日落时间,我想添加一个指示符 1。

我正在使用这样的 for 循环来执行此操作:

def round_to_hour(dt):
  import datetime
  dt_start_of_hour = dt.replace(minute=0, second=0, microsecond=0)
  dt_half_hour = dt.replace(minute=30, second=0, microsecond=0)
  if dt >= dt_half_hour:
        # round up
    dt = dt_start_of_hour + datetime.timedelta(hours=1)
  else:
        # round down
    dt = dt_start_of_hour
  return dt
  
def get_sunrise_time(d):
    #d = datetime.datetime.strptime(d, "%Y-%m-%d")
    from astral import LocationInfo
    fr = LocationInfo("France", "France", "Europe/Paris", 46, 2)
    from astral.sun import sun
    s = sun(fr.observer, date = d,  tzinfo = "Europe/Paris")
    return round_to_hour(s["sunrise"])

def get_sunset_time(d):
    #d = datetime.datetime.strptime(d, "%Y-%m-%d")
    from astral import LocationInfo
    fr = LocationInfo("France", "France", "Europe/Paris", 46, 2)
    from astral.sun import sun
    s = sun(fr.observer, date = d,  tzinfo = "Europe/Paris")
    return round_to_hour(s["sunset"])

  for i in range(len(f)):
      
    f["sunrise"].loc[i] = get_sunrise_time(f["Date"].loc[i])
    f["sunset"].loc[i] = get_sunset_time(f["Date"].loc[i])
    if(f["DateTime"].loc[i] >= f["sunrise"].loc[i].replace(tzinfo=None) and f["DateTime"].loc[i]  < f["sunset"].loc[i].replace(tzinfo=None)):
        f["DL"].loc[i] = 1

我需要对其进行矢量化/优化,因为运行时间太长,但是当我这样做时:

get_sunrise_time(f["Date"])

AttributeError: 'Series' object has no attribute 'year'

我尝试使用 np.vectorize(sun) 但它给了我同样的错误。

请帮忙 !

谢谢

标签: pythonpandasvectorization

解决方案


推荐阅读