1

此处的数据适用于具有流动余额的银行账户。我想对数据进行重新采样以仅使用日终余额,因此给出了一天的最后一个值。一天可以有多个数据点,代表多个交易。

In [1]: from StringIO import StringIO

In [2]: import pandas as pd

In [3]: import numpy as np

In [4]: print "Pandas version", pd.__version__
Pandas version 0.12.0

In [5]: print "Numpy version", np.__version__
Numpy version 1.7.1

In [6]: data_string = StringIO(""""Date","Balance"
   ...: "08/09/2013","1000"
   ...: "08/09/2013","950"
   ...: "08/09/2013","930"
   ...: "08/06/2013","910"
   ...: "08/02/2013","900"
   ...: "08/01/2013","88"
   ...: "08/01/2013","87"
   ...: """)

In [7]: ts = pd.read_csv(data_string, parse_dates=[0], index_col=0)

In [8]: print ts
            Balance
Date               
2013-08-09     1000
2013-08-09      950
2013-08-09      930
2013-08-06      910
2013-08-02      900
2013-08-01       88
2013-08-01       87

我预计“2013-08-09”为 1000,但绝对不是“中间”数字 950。

In [10]: ts.Balance.resample('D', how='last')
Out[10]: 
Date
2013-08-01     88
2013-08-02    900
2013-08-03    NaN
2013-08-04    NaN
2013-08-05    NaN
2013-08-06    910
2013-08-07    NaN
2013-08-08    NaN
2013-08-09    950
Freq: D, dtype: float64

我预计“2013-08-09”为 930,或“2013-08-01”为 88。

In [12]: ts.Balance.resample('D', how='first')
Out[12]: 
Date
2013-08-01      87
2013-08-02     900
2013-08-03     NaN
2013-08-04     NaN
2013-08-05     NaN
2013-08-06     910
2013-08-07     NaN
2013-08-08     NaN
2013-08-09    1000
Freq: D, dtype: float64

我在这里错过了什么吗?使用“第一个”和“最后一个”重新采样是否按我期望的方式工作?

4

2 回答 2

2

为了能够重新采样您的数据,Pandas 首先必须对其进行排序。因此,如果您加载数据并按索引对其进行排序,您会得到以下内容:

>>> pd.read_csv(data_string, parse_dates=[0], index_col=0).sort_index()
            Balance
Date               
2013-08-01       87
2013-08-01       88
2013-08-02      900
2013-08-06      910
2013-08-09     1000
2013-08-09      930
2013-08-09      950

这就解释了为什么你得到了你得到的结果。@Jeff 解释了为什么订单是“任意的”,根据您的评论,解决方案是mergesort在操作之前对数据使用算法......

>>> df = pd.read_csv(data_string, parse_dates=[0],
                     index_col=0).sort_index(kind='mergesort')
>>> df.Balance.resample('D',how='last')
2013-08-01      88
2013-08-02     900
2013-08-03     NaN
2013-08-04     NaN
2013-08-05     NaN
2013-08-06     910
2013-08-07     NaN
2013-08-08     NaN
2013-08-09    1000
>>> df.Balance.resample('D', how='first')
2013-08-01     87
2013-08-02    900
2013-08-03    NaN
2013-08-04    NaN
2013-08-05    NaN
2013-08-06    910
2013-08-07    NaN
2013-08-08    NaN
2013-08-09    930
于 2013-08-23T20:42:53.530 回答
0

问题是由于您的日期是重复的,因此实际上可能是任意顺序;不保证使用 dups 订购。

In [24]: ts.Balance.resample('D',how='last')
Out[24]: 
Date
2013-08-01     87
2013-08-02    900
2013-08-03    NaN
2013-08-04    NaN
2013-08-05    NaN
2013-08-06    910
2013-08-07    NaN
2013-08-08    NaN
2013-08-09    930
Freq: D, dtype: float64

In [25]: ts.Balance.order().resample('D',how='last')
Out[25]: 
Date
2013-08-01      88
2013-08-02     900
2013-08-03     NaN
2013-08-04     NaN
2013-08-05     NaN
2013-08-06     910
2013-08-07     NaN
2013-08-08     NaN
2013-08-09    1000
Freq: D, dtype: float64

最简单的方法是sort数据,但不清楚实际排序是什么(例如,您需要一个外生参数来决定它)。

传递sort=False给 groupby (虽然你不能用 resample 做到这一点)

In [29]: ts.groupby(ts.index,sort=False).last().reindex(date_range(ts.index.min(),ts.index.max()))
Out[29]: 
            Balance
2013-08-01       87
2013-08-02      900
2013-08-03      NaN
2013-08-04      NaN
2013-08-05      NaN
2013-08-06      910
2013-08-07      NaN
2013-08-08      NaN
2013-08-09      930

你可以这样做以获得你所追求的

In [52]: df = DataFrame(ts.values,index=ts.index,columns=['values']).reset_index()

In [53]: df
Out[53]: 
                 Date  values
0 2013-08-09 00:00:00    1000
1 2013-08-09 00:00:00     950
2 2013-08-09 00:00:00     930
3 2013-08-06 00:00:00     910
4 2013-08-02 00:00:00     900
5 2013-08-01 00:00:00      88
6 2013-08-01 00:00:00      87

In [54]: df.groupby('Date').apply(lambda x: x.iloc[-1]['values']).reindex(date_range(ts.index.min(),ts.index.max()))

Out[54]: 
2013-08-01     87
2013-08-02    900
2013-08-03    NaN
2013-08-04    NaN
2013-08-05    NaN
2013-08-06    910
2013-08-07    NaN
2013-08-08    NaN
2013-08-09    930
Freq: D, dtype: float64
于 2013-08-23T20:39:14.660 回答