Rather than a question, this is more of my workaround for a problem I was having when reading tmy3 files. I hope that it can be of use to some of you. I am still a novice when it comes to coding and python, so there may be simpler ways.
PROBLEM
Upon using the iotools.read_tmy3() function, I followed examples and code outlined by others. This included: 1. Reading the tmy3 datafile; 2. Coercing the year to 2015 (or any other year you like); and, 3. Shifting my index by 30 minutes to match the solar radiation simulation.
The code I used was:
tmy_data, metadata = pvlib.iotools.read_tmy3(datapath, coerce_year=2015)
tmy_data = tmy_data.shift(freq='-30Min') ['2015']
tmy_data.index.name = 'Time
By using this code, you lose the final row of your DataFrame. This is resolved by removing the ['2015'] in line two resolves this, but now the final date is set in 2014. For the purpose of my work, I needed the final index to remain consistent.
WHAT I TRIED
- I attempted to shift index of the final row, unsuccessfully, using the shift method.
- I attempted to reindex by setting the index of the final row equal to the DatetimeIndex I wanted.
- I attempted to remove the timezone data from the index, modify the index, then reapply the timezone data. All of these were overly complicated, and did not help resolve my issue.
WHAT WORKED FOR ME
The code for what I did is shown below. These were my steps: 1. Identify the index from my final row, and copy its data; 2. Drop the final row of my tmy_data DataFrame; 3. Create a new dataframe with the shifted date and copied data; and, 4. Append the row to my tmy_data DataFrame
It is a bit tedious, but it an easy fix when reading multiple tmy3 files with multiple timezones.
#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)
After doing this my final indeces are:
2015-01-01 00:30:00-05:00
....
2015-12-31 22:30:00-05:00
2015-12-31 23:30:00-05:00
This way, the DataFrame contains 8760 rows, as opposed to 8759 through the previous method.
I hope this helps others!