Reputation: 18859
EDIT The missed dates 20140103 and 20140104 is expected, I don't want them to be patched auto.
And I don't want to use xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
instead of xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
since I want to use some operation as
xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
xs['2014']
Out[24]:
2014-01-01 0
2014-01-02 1
2014-01-05 2
2014-01-06 3
2014-01-07 4
dtype: int64
which doesn't work for:
In [26]: xs = pd.Series(data=range(len(ts)), index=ts)
In [27]: xs['2014']
---------------------------------------------------------------------------
KeyError: '2014'
For example:
In [1]: import pandas as pd
In [2]: ts = ['20140101', '20140102', '20140105', '20140106', '20140107']
In [3]: xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
In [4]: xs.plot()
Will plot this image, which add the extra data on 20140103, 20140104.
While I want to got a image like this:
import matplotlib.pyplot as plt
plt.plot(xs.values)
Upvotes: 1
Views: 2794
Reputation: 2365
Thanks to euri10 for use_index = False
ts = ['20140101', '20140102', '20140105', '20140106', '20140107']
xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
fig, ax = plt.subplots()
xs.plot(use_index=False)
ax.set_xticklabels(pd.to_datetime(ts))
ax.set_xticks(range(len(ts)))
fig.autofmt_xdate()
plt.show()
Upvotes: 3
Reputation: 1726
import pandas as pd
ts = ['20140101', '20140102', '20140105', '20140106', '20140107']
xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
print xs
#output
2014-01-01 0
2014-01-02 1
2014-01-05 2
2014-01-06 3
2014-01-07 4
You are missing 2 dates.
Upvotes: 0
Reputation: 2626
Couldn't test it but you may want to use use_index=False and/or xticks
Upvotes: 2
Reputation: 131
Since you converted the dates to datetime, pandas is plotting the dates themselves on the x-axis. Since there are days that aren't included in the Series, pandas will leave the appropriate spaces on the x-axis where those points are. If, for some reason, you don't want this (for instance, if you are counting business days and the missing points are weekends), I think the solution that makes it most clear is to make the index not a date but a number (Day 1, Day 2, etc.). To my knowledge, pandas won't allow you to distort the axes.
Upvotes: 1