pandas.Series / DataFrame.fillna中的错误限制?

时间:2012-09-11 15:14:09

标签: python pandas

我一直在尝试使用fillnavalue关键字使用limit填充数据框和系列。不包括limit时会尊重value,但只要包含value,限制就不再受到尊重。以下是使用DataFrame的示例:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],columns=['one', 'two','three'])
df2 = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g','h','i', 'j','k'])

In [7]: df2
Out[7]:
        one       two     three
a -0.942695  0.465658 -0.966754
b       NaN       NaN       NaN
c -1.208036  0.287274 -1.116466
d       NaN       NaN       NaN
e  0.041212  0.065966 -1.895570
f  0.869104 -3.481962 -0.280699
g       NaN       NaN       NaN
h -1.151732 -0.310296 -1.701202
i       NaN       NaN       NaN
j       NaN       NaN       NaN
k       NaN       NaN       NaN

In [8]: df2.fillna(method='pad', limit=1)
Out[8]:
        one       two     three
a -0.942695  0.465658 -0.966754
b -0.942695  0.465658 -0.966754
c -1.208036  0.287274 -1.116466
d -1.208036  0.287274 -1.116466
e  0.041212  0.065966 -1.895570
f  0.869104 -3.481962 -0.280699
g  0.869104 -3.481962 -0.280699
h -1.151732 -0.310296 -1.701202
i -1.151732 -0.310296 -1.701202
j       NaN       NaN       NaN
k       NaN       NaN       NaN

In [9]: df2.fillna(value=999,method='pad', limit=1)
Out[9]:
          one         two       three
a   -0.942695    0.465658   -0.966754
b  999.000000  999.000000  999.000000
c   -1.208036    0.287274   -1.116466
d  999.000000  999.000000  999.000000
e    0.041212    0.065966   -1.895570
f    0.869104   -3.481962   -0.280699
g  999.000000  999.000000  999.000000
h   -1.151732   -0.310296   -1.701202
i  999.000000  999.000000  999.000000
j  999.000000  999.000000  999.000000
k  999.000000  999.000000  999.000000

我在这里遗漏了什么,或者这是一个错误?

干杯

编辑:使用pumpas 0.8.1在python 2.7上使用numpy 1.6.1

1 个答案:

答案 0 :(得分:2)

这实际上是设计的。 limit关键字设计为与method关键字一起使用,因为您必须指定排序(即前向填充或回填),而您没有value