Created
February 4, 2021 07:40
-
-
Save MarcvdSluys/e2a90a409152de8686da49b297fc8d03 to your computer and use it in GitHub Desktop.
Pandas date/time str -> localised datetime object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read file: | |
df = pd.read_csv(inFile, header=0, sep=r'\s*,\s*', engine='python') | |
# Make period_end a datetime object (with tz=UT) and convert the dates to CET: | |
cet = tz.timezone('Europe/Amsterdam') | |
df.date = pd.to_datetime(df.date, utc=True) # utc=True needed because midnight timestamps have no time and no tz! | |
df.date = df.date.dt.tz_convert(cet) # Works when already converted to UT by to_datetime! | |
# Make a plot of DNI and DHI: | |
import matplotlib.pyplot as plt # Get matplotlib.pyplot | |
import matplotlib.dates as mdates | |
fig = plt.figure(figsize=(12.5,7)) # Set png size to 1250x700; savefig has default dpi 100 | |
ax = fig.add_subplot(111) # Create an axis object for the current figure | |
ax.plot(df.date, df.DNI, label='DNI') | |
ax.plot(df.date, df.DHI, label='DHI') | |
plt.legend() | |
ax.xaxis.set_major_formatter( mdates.DateFormatter('%d/%m, %H:%M') ) # Format datetime labels | |
fig.autofmt_xdate() # Rotate date labels automatically | |
ax.grid(True) # Plot a grid | |
plt.xlabel('Date and time') # Label the horizontal axis | |
plt.ylabel(r'Insolation (W/m$2$)') # Label the vertical axis - use raw string with LaTeX for symbols | |
fig.tight_layout() # Use narrow margins | |
fig.savefig('radiation.png') # Save the plot as png | |
plt.close() # Close the plot in order to start a new one later |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment