Pandas type error trying to plot
Thanks @martinvseticka. I think your assessment is correct based on the numpy code you pointed me to. I was able to simplify your tweaks a bit more (and added a third sample point) to get
t1 = pd.to_datetime('2015-11-01 00:00:00')
t2 = pd.to_datetime('2015-11-02 00:00:00')
t3 = pd.to_datetime('2015-11-03 00:00:00')
Time = pd.Series([t1, t2, t3])
r = pd.Series([-1, 1, 0.5])
df = pd.DataFrame({'Time': Time, 'Value': r})
fig = plt.figure(figsize=(x_size,y_size))
ax = fig.add_subplot(111)
ax.plot_date(x=df.Time, y=df.Value, marker='o')
The key seems to be calling 'plot_date' rather than 'plot'. This seems to inform mapplotlib to not try to concatenate the arrays.
Is this what you are looking for?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
t1 = pd.to_datetime('2015-11-01 00:00:00')
t2 = pd.to_datetime('2015-11-02 00:00:00')
idx = pd.Series([t1, t2])
s = pd.Series([-1, 1], index=idx)
fig, ax = plt.subplots()
ax.plot_date(idx, s, 'v-')
plt.tight_layout()
plt.show()
I'm new to Python so hopefully I'm not wrong. Basically, I tried to adapt your example according to https://stackoverflow.com/a/13674286/99256.
The problem with your script is that numpy
tries to concatenate df.Time
and df.Value
series and it can't find a suitable type for the new array because one array is numeric and the second one is composed of Timestamp
instances.
There is another way, that we should drop uses Series. Just use list for time.
t1 = pd.to_datetime('2015-11-01 00:00:00')
t2 = pd.to_datetime('2015-11-02 00:00:00')
Time = pd.Series([t1, t2])
r = pd.Series([-1, 1])
df = pd.DataFrame({'Time': Time, 'Value': r})
print(df)
print(type(df.Time))
print(type(df.Time[0]))
x_size = 800
y_size = 600
fig = plt.figure(figsize=(x_size,y_size))
ax = fig.add_subplot(111)
ax.scatter(list(df.Time.values), list(df.Value.values), marker='o')