Python 对于在整个时间间隔内有效的数据,我应该使用什么表示法?
我有一系列的小时价格。每个价格在整个1小时内有效用熊猫来表示这些价格的最佳方式是什么,使我能够以任意更高的频率(如分钟或秒)对它们进行索引,并对它们进行算术运算? 数据细节 样品价格可能是:Python 对于在整个时间间隔内有效的数据,我应该使用什么表示法?,python,pandas,time-series,Python,Pandas,Time Series,我有一系列的小时价格。每个价格在整个1小时内有效用熊猫来表示这些价格的最佳方式是什么,使我能够以任意更高的频率(如分钟或秒)对它们进行索引,并对它们进行算术运算? 数据细节 样品价格可能是: >>> prices = Series(randn(5), pd.date_range('2013-01-01 12:00', periods = 5, freq='H')) >>> prices 2013-01-01 12:00:00 -1.001692 2013-
>>> prices = Series(randn(5), pd.date_range('2013-01-01 12:00', periods = 5, freq='H'))
>>> prices
2013-01-01 12:00:00 -1.001692
2013-01-01 13:00:00 -1.408082
2013-01-01 14:00:00 -0.329637
2013-01-01 15:00:00 1.005882
2013-01-01 16:00:00 1.202557
Freq: H
现在,如果我想要13:37:42处的值(我希望它与13:00处的值相同),那么使用什么表示法
但是时间跨度系列没有提供我所期望的系列
的其他一些功能。假设我有另一个系列金额
,说明我在某一时刻购买了多少物品。如果我想计算价格,我想乘以两个系列的
>>> amounts = Series([1,2,2], pd.DatetimeIndex(['2013-01-01 13:37', '2013-01-01 13:57', '2013-01-01 14:05']))
>>> amounts*price_periods
但这会产生一个例外,有时甚至会冻结我的IPy笔记本。索引也没有帮助
>>> ts_periods[amounts.index]
PeriodIndex
结构是否仍在进行中,或者这些功能将不会被添加?是否有其他我应该使用的结构(或者现在应该使用,在PeriodIndex
成熟之前)?我正在使用Pandas版本0.9.0.dev-1e68fd9
检查
返回上一个可用日期时间的值:
prices['2013-01-01 13:00:00']
要进行计算,可以使用:
prices.asof(amounts.index) * amounts
它返回一个包含金额索引和相应值的序列:
>>> prices
2013-01-01 12:00:00 0.943607
2013-01-01 13:00:00 -1.019452
2013-01-01 14:00:00 -0.279136
2013-01-01 15:00:00 1.013548
2013-01-01 16:00:00 0.929920
>>> prices.asof(amounts.index)
2013-01-01 13:37:00 -1.019452
2013-01-01 13:57:00 -1.019452
2013-01-01 14:05:00 -0.279136
>>> prices.asof(amounts.index) * amounts
2013-01-01 13:37:00 -1.019452
2013-01-01 13:57:00 -2.038904
2013-01-01 14:05:00 -0.558272
这解决了我遇到的第一个问题(与
PeriodIndex
相同),但我是否可以用它对序列进行算术运算?如果没有for循环,我不知道如何实现amounts*prices
,这不是熊猫的方式。谢谢。的确,它很管用,不过对我的口味来说有点不对劲。让我们看看是否会有其他的意见。只是一个评论:如果有官方的0.10.0,为什么要使用0.9.0.dev?注意。我没有注意到有更新的版本。几个月前,我通过SciPySuperpack安装了熊猫。我现在升级到了0.10.0,但现在即使在价格周期['2013-01-01 13:37:42']
的情况下,我也遇到了一个关键错误。
prices['2013-01-01 13:00:00']
prices.asof(amounts.index) * amounts
>>> prices
2013-01-01 12:00:00 0.943607
2013-01-01 13:00:00 -1.019452
2013-01-01 14:00:00 -0.279136
2013-01-01 15:00:00 1.013548
2013-01-01 16:00:00 0.929920
>>> prices.asof(amounts.index)
2013-01-01 13:37:00 -1.019452
2013-01-01 13:57:00 -1.019452
2013-01-01 14:05:00 -0.279136
>>> prices.asof(amounts.index) * amounts
2013-01-01 13:37:00 -1.019452
2013-01-01 13:57:00 -2.038904
2013-01-01 14:05:00 -0.558272