Pandas 如何访问0.15 DataFrame.resample方法中的多个列?

Pandas 如何访问0.15 DataFrame.resample方法中的多个列?,pandas,Pandas,在Pandas 0.12中,如果对具有自定义重采样函数的数据帧使用重采样方法,则它会对每个数据帧行调用一次自定义函数,从而访问所有列中的值。在0.15中,重采样方法对每个数据帧条目调用一次我的自定义函数,唯一可用的值是该条目,而不是整行 如何恢复0.12行为并在自定义函数中查看整行 区别在于: 初始设置: In [1]: import pandas In [2]: import datetime In [3]: import sys In [4]: dt = datetime.dateti

在Pandas 0.12中,如果对具有自定义重采样函数的数据帧使用重采样方法,则它会对每个数据帧行调用一次自定义函数,从而访问所有列中的值。在0.15中,重采样方法对每个数据帧条目调用一次我的自定义函数,唯一可用的值是该条目,而不是整行

如何恢复0.12行为并在自定义函数中查看整行

区别在于:

初始设置:

In [1]: import pandas

In [2]: import datetime

In [3]: import sys

In [4]: dt = datetime.datetime(2014,1,1)

In [5]: idx = [dt + datetime.timedelta(days=i) for i in [0,2]]

In [6]: df = pandas.DataFrame({'a': [1.0, 2.0], 'b': ['x', 'y']}, index=idx)

In [7]: foo = lambda data: sys.stdout.write("***\n" + str(data) + "\n")
0.12行为注意,有3个对foo的调用:

In [8]: pandas.__version__
Out[8]: '0.12.0'

In [9]: df.resample(rule='D', how=foo, fill_method='ffill')
***
            a  b
2014-01-01  1  x
***
Empty DataFrame
Columns: [a, b]
Index: []
***
            a  b
2014-01-03  2  y
Out[9]: 
               a     b
2014-01-01  None  None
2014-01-02  None  None
2014-01-03  None  None
In [8]: pandas.__version__
Out[8]: '0.15.0'

In [9]: df.resample(rule='D', how=foo, fill_method='ffill')
***
2014-01-01    1
Name: a, dtype: float64
***
Series([], name: a, dtype: float64)
***
2014-01-03    2
Name: a, dtype: float64
***
2014-01-01    x
Name: b, dtype: object
***
Series([], name: b, dtype: object)
***
2014-01-03    y
Name: b, dtype: object
Out[9]: 
             a     b
2014-01-01 NaN  None
2014-01-02 NaN  None
2014-01-03 NaN  None
0.15行为注意,有6个对foo的调用:

In [8]: pandas.__version__
Out[8]: '0.12.0'

In [9]: df.resample(rule='D', how=foo, fill_method='ffill')
***
            a  b
2014-01-01  1  x
***
Empty DataFrame
Columns: [a, b]
Index: []
***
            a  b
2014-01-03  2  y
Out[9]: 
               a     b
2014-01-01  None  None
2014-01-02  None  None
2014-01-03  None  None
In [8]: pandas.__version__
Out[8]: '0.15.0'

In [9]: df.resample(rule='D', how=foo, fill_method='ffill')
***
2014-01-01    1
Name: a, dtype: float64
***
Series([], name: a, dtype: float64)
***
2014-01-03    2
Name: a, dtype: float64
***
2014-01-01    x
Name: b, dtype: object
***
Series([], name: b, dtype: object)
***
2014-01-03    y
Name: b, dtype: object
Out[9]: 
             a     b
2014-01-01 NaN  None
2014-01-02 NaN  None
2014-01-03 NaN  None

我不知道为什么行为会改变,但我认为使用TimeGrouper和groupby可以让您返回到旧的结果,尽管除非给foo一个返回值,否则会出错

In [496]: df.groupby(pd.TimeGrouper('D')).apply(foo)
***
            a  b
2014-01-01  1  x
***
Empty DataFrame
Columns: [a, b]
Index: []
***
            a  b
2014-01-03  2  y
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
.....
ValueError: All objects passed were None

嗯,0.12.0的行为是错误的。它提供了一个混合的dtype数据帧,对于一般定义的函数基本上是无用的。更有用的是传递不同数据类型的函数。你到底想做什么?