问题
I want to implement a fillna method over a pandas dataframes with the method='bfill' and a limit
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()
but I have the following error
<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
I found in this discussion that a bug existed but I didn't find another answer since then. https://github.com/pandas-dev/pandas/issues/1892
I used the keyword method but it still doesn't work Bug in pandas.Series/DataFrame.fillna limit?
if i remove the limit it works fine but then next line throws error
labeled_features = labeled_features.fillna('none')
ValueError: fill value must be in categories
回答1:
Please use bfill
with axis=1
and limit=7
:
labeled_features = labeled_features.bfill(axis=1, limit=7)
来源:https://stackoverflow.com/questions/59566168/specifying-a-limit-for-fillna-has-not-been-implemented-yet