Home > database >  How to plot local date and time vs temperature from CSV file that uses UTC for date and time
How to plot local date and time vs temperature from CSV file that uses UTC for date and time

Time:06-28

I have a CSV file where each row has date and time and temperature, where the date and time are UTC.

I can plot temperature as a function of time, but the X axis shows the UTC time.

What is the best way to generate a plot where the X axis shows my local time (which is currently Pacific Daylight Time)?

The current Python code is as follows, so currently I am just plotting temperature as a function of the UTC time and am not using date, which is in a separate column.

I have done some searching and reading, but so far have not found anything that directly addresses this problem. I am a newbie in Pandas, so I am sure that a straightforward solution is out there somewhere.

import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('LFEM_data-.csv',skiprows=1) xvalues='Time' yvalues = ['Temperature'] df.plot(x=xvalues, y=yvalues) plt.show()

CodePudding user response:

Use the tz_convert function on a datetime type pandas column to convert it to PDT.

import pandas as pd
df['Time'] = pd.to_datetime(tdf['Time'])
df['Time_local'] = df['Time'].dt.tz_convert('PST8PDT')

CodePudding user response:

The following worked:

import pandas as pd import matplotlib.pyplot as plt df = pd.read_csv('LFEM_data-.csv',skiprows=1) df['DateTime'] = df['Date'] ' ' df['Time'] df['DateTime'] = pd.to_datetime(df['DateTime']) ts_utc = df['DateTime'].dt.tz_localize('UTC') df['DateTime_local'] = ts_utc.dt.tz_convert('America/Tijuana') xvalues='DateTime_local' yvalues = ['Temperature'] df.plot(x=xvalues, y=yvalues) plt.show()
  • Related