python - Pandas,如何引用时间序列项目?

标签 python time-series pandas

我正在尝试处理一些股票市场数据。我有以下数据框:

>>> ticker
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 707 entries, 2010-01-04 00:00:00 to 2012-10-19 00:00:00
Data columns:
Open         707  non-null values
High         707  non-null values
Low          707  non-null values
Close        707  non-null values
Volume       707  non-null values
Adj Close    707  non-null values
dtypes: float64(5), int64(1)

我将引用随机收盘价:

>>> ticker ['Close'] [704]
21.789999999999999

获取第 704 项的日期的语法是什么?

同样,如何获取以下项目在数组中的位置?:

>>> ticker.Close.min ()
17.670000000000002

我知道这看起来很基本,但我花了很多时间搜索文档。如果它在那里,我绝对会错过它。

最佳答案

这应该回答你的两个问题:

注意:如果您想要第 704 个元素,则应使用“703”作为索引从零开始。如您所见,df['A'].argmin() 也返回 1,即 df 中的第二行。

In [682]: print df
                   A         B         C
2000-01-01  1.073247 -1.784255  0.137262
2000-01-02 -0.797483  0.665392  0.692429
2000-01-03  0.123751  0.532109  0.814245
2000-01-04  1.045414 -0.687119 -0.451437
2000-01-05  0.594588  0.240058 -0.813954
2000-01-06  1.104193  0.765873  0.527262
2000-01-07 -0.304374 -0.894570 -0.846679
2000-01-08 -0.443329 -1.437305 -0.316648


In [683]: df.index[3]
Out[683]: <Timestamp: 2000-01-04 00:00:00>

In [684]: df['A'].argmin()
Out[684]: 1

关于python - Pandas,如何引用时间序列项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12992958/

相关文章:

python - 使用 sklearn KNN 显示最近邻居

python - 子集数据框 Pandas 时间序列

python - 在数据框列上应用 curve_fit

python - 如何旋转数据框

python - ElementTree 解析简单 KML 文档时不返回任何节点

python - 我可以在 python 上暂停 itertools,然后再继续吗?

r - 如何使用data.table在日期范围内执行联接?

r - 如何说服 ggplot2 geom_text 在时间序列图中标记指定日期?

python - Pandas - 从列表中的字典值映射列

python - 使用公式更新单元格值会导致带有 Sheets API 的撇号前缀