星体太阳计算

2024-10-02 18:20:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手,我想从数据帧的datetime列计算太阳的位置(方位角和天顶角)

我为这项任务找到了Astral模块,并使用各个时间戳进行了一些正确的计算。 但是当我试图处理整个datetime列时,我得到了一个错误

我还尝试将dataframe(datetime列)本地化为特定的时区,但错误保持不变

谢谢你的帮助

代码如下:

import datetime
from datetime import datetime
import pytz
pytz.all_timezones
from astral import Astral

d1=pd.to_datetime(d1)
d1=d1.dt.tz_localize("Europe/Berlin")

输出如下所示:

0   2015-02-04 09:10:21
1   2016-03-05 11:30:25
dtype: datetime64[ns]

方位角计算:

print(Astral().solar_azimuth(d1, 52.52, -13.40))

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-95-2a14f69465f4> in <module>
----> 1 print(Astral().solar_azimuth(d1, 52.52, -13.40))

~\Anaconda3\lib\site-packages\astral.py in solar_azimuth(self, dateandtime, latitude, longitude)
   2276             latitude = -89.8
   2277 
-> 2278         if dateandtime.tzinfo is None:
   2279             zone = 0
   2280             utc_datetime = dateandtime

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5137             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5138                 return self[name]
-> 5139             return object.__getattribute__(self, name)
   5140 
   5141     def __setattr__(self, name: str, value) -> None:

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

Tags: nameinfromimportselfdatetime错误d1
1条回答
网友
1楼 · 发布于 2024-10-02 18:20:56

不确定您的代码示例是如何工作的,但基本上,您可以applysolar_azimuth函数转换为pandas.Series函数,例如

import pandas as pd
from astral.location import Location, LocationInfo

# define your location
l = LocationInfo('name', 'region', 'timezone/name', 52.52, -13.40)

# assuming input datetimes are in the form of a pandas Series
d1 = pd.to_datetime(["2015-02-04 09:10:21", "2016-03-05 11:30:25"]).to_series().dt.tz_localize('Europe/Berlin')

azim = d1.apply(Location(l).solar_azimuth)

print(azim)
# 2015-02-04 09:10:21    112.190070
# 2016-03-05 11:30:25    137.505145
# dtype: float64

请注意,您也可以在没有熊猫的情况下执行此操作,例如

from datetime import datetime
from zoneinfo import ZoneInfo # Python 3.9

dt = ["2015-02-04 09:10:21", "2016-03-05 11:30:25"]
# set time zone
dt = [datetime.fromisoformat(t).replace(tzinfo=ZoneInfo('Europe/Berlin')) for t in dt]
# azimut calc
azim = [Location(l).solar_azimuth(t) for t in dt]

print(azim)
# [112.19006967766785, 137.50514457536954]

相关问题 更多 >