1

这不是一个问题,而是我在阅读 tmy3 文件时遇到的问题的解决方法。我希望它可以对你们中的一些人有用。在编码和python方面我还是个新手,所以可能有更简单的方法。

问题

在使用 iotools.read_tmy3() 函数时,我遵循了其他人概述的示例和代码。这包括: 1. 读取 tmy3 数据文件;2. 将年份强制到 2015 年(或您喜欢的任何其他年份);并且,3. 将我的索引移动 30 分钟以匹配太阳辐射模拟。

我使用的代码是:

tmy_data, metadata = pvlib.iotools.read_tmy3(datapath, coerce_year=2015)
tmy_data = tmy_data.shift(freq='-30Min') ['2015']
tmy_data.index.name = 'Time

通过使用此代码,您将丢失 DataFrame 的最后一行。这通过删除第二行中的 ['2015'] 解决了这个问题,但现在最终日期设置在 2014 年。为了我的工作,我需要最终索引保持一致。

我尝试了什么

  1. 我尝试使用shift方法移动最后一行的索引,但未成功。
  2. 我试图通过将最后一行的索引设置为我想要的 DatetimeIndex 来重新索引。
  3. 我试图从索引中删除时区数据,修改索引,然后重新应用时区数据。所有这些都过于复杂,无助于解决我的问题。

什么对我有用

我所做的代码如下所示。这些是我的步骤: 1. 从我的最后一行中识别索引,并复制它的数据;2. 删除我的 tmy_data DataFrame 的最后一行;3. 使用移位的日期和复制的数据创建一个新的数据框;并且,4. 将行附加到我的 tmy_data DataFrame

这有点乏味,但在读取具有多个时区的多个 tmy3 文件时很容易解决。

#Remove the specific year from line 2 in the code above
tmy_data = tmy_data.shift(freq='-30Min')

#Identify the last row, and copy the data
last_date = tmy_data.iloc[[-1]].index

copied_data = tmy_data.iloc[[-1]]
copied_data = copied_data.to_dict(orient='list')

#Drop the row with the incorrect index
tmy_data.drop([last_date][0], inplace = True)

#Shift the date of the last date
last_date = last_date.shift(n = 1, freq = 'A')

#Create a new DataFrame with the shifted date and copied data
last_row = pd.DataFrame(data = copied_data, 
                        index=[last_date][0])

tmy_data = tmy_data.append(last_row)

这样做之后,我的最终指标是:

2015-01-01 00:30:00-05:00
....
2015-12-31 22:30:00-05:00
2015-12-31 23:30:00-05:00

这样,DataFrame 包含 8760 行,而之前的方法是 8759 行。

我希望这对其他人有帮助!

4

0 回答 0