指定 fillna 的限制尚未实施

specifying a limit for fillna has not been implemented yet

我想在 pandas 数据帧上使用方法='bfill' 和限制

实现 fillna 方法
labeled_features = final_feat.merge(failures, on=['datetime', 'machineID'], how='left')
print(type(labeled_features))
labeled_features = labeled_features.bfill(limit=7) # fill backward up to 24h
labeled_features = labeled_features.fillna('none')
labeled_features.head()

但是我有以下错误

<class 'pandas.core.frame.DataFrame'>

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-40-f9d11ab337a0> in <module>
      1 labeled_features = final_feat.merge(failures, on=['datetime', 'machineID'], how='left')
      2 print(type(labeled_features))
----> 3 labeled_features = labeled_features.bfill(limit=7) # fill backward up to 24h
      4 labeled_features = labeled_features.fillna('none')
      5 labeled_features.head()

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in bfill(self, axis, inplace, limit, downcast)
   6312         """
   6313         return self.fillna(
-> 6314             method="bfill", axis=axis, inplace=inplace, limit=limit, downcast=downcast
   6315         )
   6316 

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\core\frame.py in fillna(self, value, method, axis, inplace, limit, downcast, **kwargs)
   4257             limit=limit,
   4258             downcast=downcast,
-> 4259             **kwargs
   4260         )
   4261 

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\core\generic.py in fillna(self, value, method, axis, inplace, limit, downcast)
   6235                 inplace=inplace,
   6236                 coerce=True,
-> 6237                 downcast=downcast,
   6238             )
   6239         else:

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\core\internals\managers.py in interpolate(self, **kwargs)
    567 
    568     def interpolate(self, **kwargs):
--> 569         return self.apply("interpolate", **kwargs)
    570 
    571     def shift(self, **kwargs):

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
    436                     kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
    437 
--> 438             applied = getattr(b, f)(**kwargs)
    439             result_blocks = _extend_blocks(applied, result_blocks)
    440 

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\core\internals\blocks.py in interpolate(self, method, axis, inplace, limit, fill_value, **kwargs)
   1963         values = self.values if inplace else self.values.copy()
   1964         return self.make_block_same_class(
-> 1965             values=values.fillna(value=fill_value, method=method, limit=limit),
   1966             placement=self.mgr_locs,
   1967         )

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    206                 else:
    207                     kwargs[new_arg_name] = new_arg_value
--> 208             return func(*args, **kwargs)
    209 
    210         return wrapper

c:\users\risha\appdata\local\programs\python\python37\lib\site-packages\pandas\core\arrays\categorical.py in fillna(self, value, method, limit)
   1842         if limit is not None:
   1843             raise NotImplementedError(
-> 1844                 "specifying a limit for fillna has not " "been implemented yet"
   1845             )
   1846 

NotImplementedError: specifying a limit for fillna has not been implemented yet

我发现在这个讨论中存在一个错误,但从那以后我没有找到另一个答案。 https://github.com/pandas-dev/pandas/issues/1892

我使用了关键字方法,但仍然无效 pandas.Series/DataFrame.fillna limit?

中的错误

如果我删除限制它工作正常但下一行抛出错误

labeled_features = labeled_features.fillna('none')

ValueError: 填充值必须在类别中

请使用 bfillaxis=1limit=7:

labeled_features = labeled_features.bfill(axis=1, limit=7)

遇到了同样的问题。 显然,这是因为 'failure' 列属于 DataType Categorical。 使用以下方式更改数据类型:

labeled_features.failure = labeled_features.failure.astype(str)

然后执行下面的代码:

labeled_features.failure = labeled_features.failure.fillna(method='bfill', limit=7)