【问题标题】:Plot with pandas seems not to work well as I change the index of a DataFrame当我更改 DataFrame 的索引时,使用 pandas 进行绘图似乎效果不佳
【发布时间】:2016-05-29 16:37:35
【问题描述】:

我是 Python 和 Pandas 的新手。我写了一个小代码来从谷歌财经下载 1 分钟的数据。使用以下命令后:

new = pd.read_csv(string, skiprows=7, names = ("d", "o", "h", "l", "c", "v") )

我得到一个如下的DataFrame:

          d        o        h        l        c       v
0 a1453905960  95.4500  95.4500  95.0900  95.0980  433810
1 a1453906020  95.0500  95.4700  94.9500  95.4500  934980
2 a1453906080  94.9400  95.1000  94.8700  95.0900  791657
3 a1453906140  94.8990  95.0300  94.7000  94.9620  763531
4 a1453906200  94.9300  95.0300  94.8200  94.8918  501298

第一列是unix时间戳。

接下来,我使用以下代码将 unix 时间戳转换为常规日期时间

new['d']=new['d'].apply(lambda x:datetime.fromtimestamp(int(x[1:])).strftime('%Y-%m-%d %H:%M:%S'))

现在我的 d 列包含带日期的字符串。如果我使用以下几行

new.index = new["d"]
del new["d"]

我只是用包含日期时间的字符串组成的新索引替换旧索引。如果我使用以下命令绘制 c 列

new["c"].plot()

我得到了一个不错的情节。

如果我使用以下命令将数据帧的索引转换为日期时间对象

 new.index = pd.to_datetime(new.index)

然后我试试

new["c"].plot()

我得到以下情节

为什么?我误会了什么?

提前谢谢你。

【问题讨论】:

  • 在第一个图表中,如果您的数据有一个月的差距,您甚至不会注意到它,因为数据是按顺序绘制的(时间在 X 轴上不连续)。在第二张图表中,时间间隔很明显(时间在 X 轴上是连续的)。您可能希望在绘图函数中指定 drawstyle = 'steps'
  • 不幸的是,添加这个简单的选项不起作用
  • @Alexander 是的,问题是您指出的问题。我有一个日期时间索引,当我绘图时,它使时间在 x 轴上连续。那么我怎样才能绘制一个有一些差距的时间序列呢?由于周六和周日没有交易活动,因此在金融时间序列中存在缺口是很常见的。

标签: python datetime pandas plot


【解决方案1】:

第一个index来自stringd,因为strftime,第二个是datetimeindex

也许datetime 不正确,但datetime.fromtimestamp 对我不起作用。

new['d']= new['d'].apply(lambda x: datetime.date.fromtimestamp(int(x[1:]))
                                                            .strftime('%Y-%m-%d %H:%M:%S'))
print new
                     d       o      h      l        c       v
0  2016-01-27 00:00:00  95.450  95.45  95.09  95.0980  433810
1  2016-01-27 00:00:00  95.050  95.47  94.95  95.4500  934980
2  2016-01-27 00:00:00  94.940  95.10  94.87  95.0900  791657
3  2016-01-27 00:00:00  94.899  95.03  94.70  94.9620  763531
4  2016-01-27 00:00:00  94.930  95.03  94.82  94.8918  501298

print new.dtypes
d     object
o    float64
h    float64
l    float64
c    float64
v      int64
dtype: object

print type(new.loc[0, 'd'])
<type 'str'>

new.index = new["d"]
del new["d"]

print new.index
Index([u'2016-01-27 00:00:00', u'2016-01-27 00:00:00', u'2016-01-27 00:00:00',
       u'2016-01-27 00:00:00', u'2016-01-27 00:00:00'],
      dtype='object', name=u'd')

new.index = pd.to_datetime(new.index)
print new.index
DatetimeIndex(['2016-01-27', '2016-01-27', '2016-01-27', '2016-01-27',
               '2016-01-27'],
              dtype='datetime64[ns]', name=u'd', freq=None)

也许您可以用于创建列d 使用to_datetime

new['d'] = pd.to_datetime(new['d'].str[1:].astype(int), unit='s')

或者如果你需要字符串使用strftime:

new['d'] = pd.to_datetime(new['d'].str[1:].astype(int), unit='s').dt.strftime('%Y-%m-%d %H:%M:%S')

【讨论】:

  • 我无法完全理解您的回答。但我认为问题是我在问题下方的 cmets 中解释的问题。
  • 我只指出indexes 之间的差异。一个是string,第二个是datetimeindex。所以这就是为什么图表不同的原因。
  • 我认为你的第一张图是绘制日期时间的正确方法 - 将 datetime 转换为 string see - 或者你可以 check
猜你喜欢
  • 2021-07-20
  • 2015-01-21
  • 2022-01-10
  • 2019-07-26
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
相关资源
最近更新 更多