I have a DataFrame with two columns of time information. The first is the epoch time in seconds, and the second is the corresponding formatted str time like "2015-06-01T09:00:00+08:00"
where "+08:00"
denotes the timezone.
I'm aware that time formats are in a horrible mess in Python, and that matplotlib.pyplot
seems to only recognise the datetime
format. I tried several ways to convert the str time to datetime
but none of them would work. When I use pd.to_datetime
it will convert to datetime64
, and when using pd.Timestamp
it converts to Timestamp
, and even when I tried using combinations of these two functions, the output would always be either datetime64
or Timestamp
but NEVER for once datetime
. I also tried the method suggested in this answer. Didn't work. It's kind of driving me up the wall now.
Could anybody kindly figure out a quick way for this? Thanks!
I post a minimal example below:
import matplotlib.pyplot as plt
import time
import pandas as pd
df = pd.DataFrame([[1433120400, "2015-06-01T09:00:00+08:00"]], columns=["epoch", "strtime"])
# didn't work
df["usable_time"] = pd.to_datetime(df["strtime"])
# didn't work either
df["usable_time"] = pd.to_datetime(df["strtime"].apply(lambda s: pd.Timestamp(s)))
# produced a strange type called "struct_time". Don't think it'd be compatible with pyplot
df["usable_time"] = df["epoch"].apply(lambda x: time.localtime(x))
# attempted to plot with pyplot
df["usable_time"] = pd.to_datetime(df["strtime"])
plt.plot(x=df["usable_time"], y=[0.123])
plt.show()