[ad_1]
This submit demonstrates the functionalities to cope with time zones in Python by way of comparative evaluation of hourly photo voltaic irradiance knowledge for 4 cities in 2020 primarily based on totally different time zones.
After I begin my work in Bonn, Germany on the primary day of October at 9 am, it’s already afternoon at 12:45 pm in my hometown in Chitwan, Nepal. My buddy in Sydney, Australia has already completed his work schedule at 6 pm on the identical day. One other buddy in New York, the USA continues to be sleeping as it’s 3 am in morning there. This suggests that these 4 locations have totally different time zones.
The time zone is an space, which observes uniform normal time for authorized, social, or business functions. The world will not be uniformly divided into totally different time zones primarily based on longitudes. Time zones are inclined to relatively comply with boundaries between and inside international locations for differentiation.
All time zones are outlined as an offset from Coordinated Common Time (UTC). And these values can vary from UTC-12:00 to UTC+14:00. Whereas the offsets are often an entire variety of hours, a number of zones are additionally offset by an extra 30 or 45 minutes. For instance, the time zone of Nepal has a time offset of UTC+05:45. In whole, there are 38 time zones on the earth.
If I’ve knowledge on photo voltaic irradiance for the 4 cities in Nepal, Germany, Australia, and the USA within the UTC time zone, it doesn’t replicate the info for a similar hour of the day in every of those international locations. On this submit, I’m going to debate how the time zones of the info could be dealt with for datetime objects together with pandas dataframe in Python.
For this objective, I’m going to obtain photo voltaic irradiance knowledge for 2020 of those 4 cities/international locations, examine and analyze the info when:
- The info of every nation is within the UTC time zone and
- The info refers back to the respective time zone of the nation.
Let’s get began.
Geocoding to retrieve the coordinates of 4 cities
In step one, I retrieve the coordinates of the 4 cities in 4 international locations as a result of I would like them to extract the photo voltaic irradiance knowledge. The method of extracting the geographical coordinates by offering the title of the place is named geocoding.
As proven beneath, I wrote a operate for geocoding utilizing the geopy bundle. The operate makes use of Nominatim, which is an open-source service for geocoding that makes use of OpenStreetMap knowledge to search out areas on the earth by title and tackle.
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="app")def get_coordinates(place):
"""Return the latitude and longitude of the place."""
place_details = geolocator.geocode(place)
coordinates = (place_details[1][0], place_details[1][1])
return coordinates
I used the operate to extract the coordinates of particular person cities and create a pandas dataframe out of it as depicted within the screenshot beneath.
Accessing knowledge utilizing NASA Energy API
The Purposes Programming Interface (API) service of NASA Energy permits to retrieve Evaluation Prepared Knowledge (NASA Energy, 2023a). For this submit, I obtain the photo voltaic irradiance knowledge for 4 cities in hourly decision fom NASA Energy Knowledge(NASA Energy, 2023b). The parameter I take advantage of is All Sky Floor Shortwave Downward Irradiance (ALLSKY_SFC_SW_DWN
) for 2020, which is described in additional element within the part beneath.
The info is named in UTC time zone format, though the hourly API additionally permits calling the info in Native Photo voltaic Time (LST) format by default.
The base_url
configuration appears to be like as follows:
base_url = r”https://energy.larc.nasa.gov/api/temporal/hourly/level?parameters=ALLSKY_SFC_SW_DWN&group=RE&time-standard=UTC&longitude={longitude}&latitude={latitude}&format=JSON&begin=2020&finish=2020"
Subsequent, I loop by means of the longitude and latitude of every place outlined by geocoding in a listing referred to as locations
and request the hourly photo voltaic irradiance knowledge for 2020. The complete code for this step is given within the GitHub gist beneath:
Parameter description
The photo voltaic irradiance knowledge refers back to the whole energy (direct + subtle) obtained from the solar per unit space per hour (Wh/m²) on a horizontal aircraft on the floor of the earth beneath all sky situations (NASA Energy, 2023c).
This parameter, additionally known as International Horizontal Irradiance (GHI), is related to calculate the scale of photo voltaic PV module wanted to satisfy the given electrical energy demand as given within the components beneath:
Fundamental statistics of given knowledge
The downloaded knowledge is depicted within the plot above. The info exhibits greater photo voltaic irradiance in Sydney in the direction of the start and finish of the 12 months, and decrease in the direction of the center of the 12 months. This sample is reverse within the different three cities, which could be defined by the placement of Sydney within the Southern hemisphere and different cities within the Northern hemisphere of the globe.
It’s noticed that Chitwan, Nepal acquired the very best annual photo voltaic irradiance (1669 kWh/m²) in 2020 adopted by Sydney, Australia (1631 kWh/m²), New York, the USA (1462 kWh/m²), and Bonn, Germany acquired the least (1193 kWh/m²).
Nevertheless, the utmost photo voltaic irradiance acquired at a selected hour is highest for Sydney (1061.3 W/m²) adopted by Chitwan (997 W/m²).
The minimal photo voltaic irradiance and the twenty fifth percentile values for every metropolis is zero as a result of there isn’t any photo voltaic irradiance throughout night time hours.
Time zone Dealing with
1. Default pandas dataframe with out “datetime” format index
As 2020 was a intercalary year, there have been three hundred and sixty six days and in consequence, the info was obtained for 8784 hours.
When the info is first downloaded, its index is of integer (int64) sort as proven beneath:
2. Changing integer sort index to “naive” datetime index
The dataframe index could be transformed into datetime sort utilizing pd.to_datetime()
and specifying the format %YpercentmpercentdpercentH
for 12 months, month, day and hours respectively.
This modification can be mirrored when the dataframe is plotted because the months Jan to Dec of 2020 are seen in xticks as proven beneath:
Though this dataframe has a datetime index, it doesn’t have any details about time zones and daylight saving. Therefore, the dataframe index is a naive datetime object. That is evident by checking the time zone information of one of many index of the pandas dataframe.
3. Localizing “naive” datetime object to “time zone conscious” datetime object
The datetime module of Python can be utilized to entry, retrieve and manipulate the date and time info.
By default, the datetime.now()
operate returns the present “native” date and time info. Nevertheless, it doesn’t have any time zone and daylight saving info as time_now.tzinfo
returns None within the code snippet beneath, implying it’s a naive datetime object.
As of now (21 April 2023), I’m in Nepal. Subsequently, I localize the present time to “Asia/Kathmandu” time zone utilizing the timezone.localize()
module of pytz bundle. Now, the time_in_nepal
is a time zone conscious datetime object.
To get the present native time in Germany, I can use time_in_nepal.astimezone(timezone("Europe/Berlin"))
, which can be a time zone conscious datetime object.
4. Localizing timezone of pandas dataframe
Subsequent, I localize the naive index of pandas dataframe to UTC time zone utilizing df.tz_localize(tz = "UTC")
as proven within the code screenshot beneath.
It’s noticed that the index of df
is transformed from naive index to time zone conscious index of UTC time zone as proven above.
5. Listing of all potential time zone addresses
The checklist of all potential time zone addresses that may be referred can be found utilizing all_timezones
module of pytz bundle. There are 594 such addresses. Some addresses can discuss with identical time zone. For instance, Europe/Berlin, Europe/Amsterdam, Europe/Copenhagen all discuss with identical time zone.
6. Create new dataframe for every metropolis and convert UTC time zone to corresponding native time zone
df
comprises the photo voltaic irradiance knowledge of the 4 cities in UTC time zone. On this step, I create 4 dataframes out of every column of df
. After which I convert the time zone of latest dataframe from UTC to the native time zone of every metropolis or nation it belongs to. For instance, the time zone of df_chitwan
is transformed utilizing
df_chitwan.tz_convert(tz = "Asia/Kathmandu")
.
It’s to be famous that for international locations which have daylight financial savings, that is mechanically accounted for within the time zone conversion. For instance, Nepal time is according to UTC + 05:45 all year long. Nevertheless, for Sydney, Python mechanically offers with daylight saving because the offset with UTC time zone could be 10 or 11 hours relying on time of 12 months.
7. Evaluating the plots of photo voltaic irradiance knowledge in several time zones
On this closing step, I wished to match how the photo voltaic irradiance regarded like within the 4 cities when the info corresponded to:
a. The UTC time zone and
b. The native time zone of every metropolis.
Within the code snippet beneath, I create two sub-plots to plot the photo voltaic irradiance in 4 cities. Within the left subplot, the photo voltaic irradiance knowledge for October 1, 2020 primarily based on UTC time zone is plotted. And in the correct subplot, the photo voltaic irradiance knowledge for October 1, 2020 primarily based on the native time of every metropolis is plotted.
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (20, 6))
fig.suptitle("Photo voltaic irradiance on October 1, 2020")ax1.plot(df.loc["2020–10–01"])
ax1.set_title("Primarily based on UTC time zone")
ax1.xaxis.set_ticks(ticks = df.loc["2020–10–01"].index[::4], labels = np.arange(0, 24, 4))
cities = df.columns.tolist()
handles = ax1.get_legend_handles_labels()[0]
ax1.legend(handles, labels = cities, loc = "higher proper")
ax1.set_xlabel("Hour of day")
ax1.set_ylabel("W/m$^2$")
ax2.plot(df_chitwan.loc["2020–10–01"].values.tolist())
ax2.plot(df_newyork.loc["2020–10–01"].values.tolist())
ax2.plot(df_bonn.loc["2020–10–01"].values.tolist())
ax2.plot(df_sydney.loc["2020–10–01"].values.tolist())
ax2.xaxis.set_ticks(ticks = np.arange(0, 24, 4), labels = np.arange(0, 24, 4))
handles = ax2.get_legend_handles_labels()[0]
ax2.legend(handles, labels = cities)
ax2.set_title("Primarily based on native time zone of every metropolis/nation")
ax2.set_xlabel("Hour of day")
ax2.set_ylabel("W/m$^2$")
plt.savefig("output/photo voltaic irradiance on october 1.jpeg",
dpi = 300)
plt.present()
The plot appears to be like as proven beneath:
As of October 1, 2020, the time zones of 4 cities as in comparison with UTC time zone are: Chitwan (UTC+05:45), New York (UTC- 04:00), Bonn (UTC + 02:00), and Sydney (UTC+10:00). Thus, we see the photo voltaic irradiance peak round 4 am, 3 pm, 10 am and three am of UTC time zone for Chitwan, New York, Bonn, and Sydney respectively on the plot on the left.
The plot on the correct exhibits that photo voltaic irradiance has an identical form primarily based on native hours all through the day in every metropolis. The photo voltaic irradiance begins to extend from zero at round 5 or 6 am in every metropolis, it peaks round midday and continues to say no earlier than reaching zero once more at 5 or 6 pm. On today of the 12 months, Sydney acquired the very best photo voltaic irradiance, adopted by Chitwan, New York, and Bonn.
On this submit, I demonstrated the strategies to cope with time zones whereas working with datetime objects together with dataframe in Python. I used the instance of working with photo voltaic irradiance knowledge for 4 cities the world over. These methodologies could possibly be very useful whereas working with time collection knowledge, the place time zones matter similar to meteorological knowledge. I’ve summarized the important thing methods learnt from this submit to cope with time zones in Python within the following numbered bullets:
- It’s potential to verify the time zone of a datetime object utilizing tzinfo module.
2. When the datetime object doesn’t comprise any details about time zones and daylight saving, it’s referred to as naive datetime object.
3. Utilizing the timezone module of pytz bundle, it’s potential to transform naive time to native time. For instance,
time_in_nepal = timezone("Asia/Kathmandu”).localize(datetime.now())
4. The brand new object is now time zone conscious. It’s potential to get the time in a distinct time zone utilizing astimezone
module of datetime object. For instance,
german_timezone = timezone(“Europe/Berlin”)
time_in_germany = time_in_nepal.astimezone(german_timezone)
5. To work with time collection knowledge, it is sensible to transform the index of pandas dataframe to datetime index.
6. The naive dataframe index could be localized utilizing tz_localize
module in df
and specifying the time zone. For instance,
df_utc = df.tz_localize(tz = “UTC”)
7. The dataframe object may also be transformed to totally different time zone utilizing tz_convert
module of df
.
df_nepal = df_utc.tz_convert(tz = “Asia/Kathmandu”)
The info, code and output plots for this submit can be found in notebooks/Timezone_handling
folder on this GitHub repository. Thanks for studying!
References
OpenStreetMap, 2023. Copyright and license.
NASA Energy, 2023a. NASA Power APIs.
NASA Energy, 2023b. POWER|Data Access Viewer.
NASA Energy, 2023c. Parameters definitions.
[ad_2]
Source link