编辑错过的日期20140103和20140104是预期的,我不希望它们被修补自动。
我不想使用xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
代替xs = pd.Series(data=range(len(ts)), index=pd.to_datetime(ts))
,因为我想使用某些操作
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
不适用于:
In [26]: xs = pd.Series(data=range(len(ts)), index=ts)
In [27]: xs['2014']
---------------------------------------------------------------------------
KeyError: '2014'
例如:
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()
将绘制此图像,在20140103,20140104上添加额外数据。
虽然我想得到这样的图像:
import matplotlib.pyplot as plt
plt.plot(xs.values)
答案 0 :(得分:3)
感谢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()
答案 1 :(得分:2)
无法测试,但你可能想使用use_index = False和/或xticks
答案 2 :(得分:1)
由于您将日期转换为日期时间,因此pandas会在x轴上绘制日期本身。由于系列中没有包含天数,因此pandas会在x轴上留下适当的空格。如果,由于某种原因,你不想要这个(例如,如果你计算工作日和缺少点是周末),我认为最明确的解决方案是使索引不是日期而是数字(第1天,第2天等)。据我所知,大熊猫不会让你扭曲轴。
答案 3 :(得分:0)
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
您缺少2个日期。