Skip to content

Value error on groupby bfill() #9970

Closed
Closed
@arijun

Description

@arijun
s = pd.Series([1,1,0], index=[1,2,2])
s.groupby(s).fillna(method='bfill')

I have a dataframe with a column containing some strings and NaNs. I can do do df.col.bfill() but when I try to do df.groupby('col2').col1.bfill() I get a ValueError.
I can do first and apply(lambda x: x is str) without error.

Here is the full traceback:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 558, in wrapper
    return self.apply(curried_with_axis)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 662, in apply
    return self._python_apply_general(f)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 669, in _python_apply_general
    not_indexed_same=mutated)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 2380, in _wrap_applied_output
    not_indexed_same=not_indexed_same)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 1142, in _concat_objects
    result = result.reindex(ax)
  File "C:\Anaconda3\lib\site-packages\pandas\core\series.py", line 2149, in reindex
    return super(Series, self).reindex(index=index, **kwargs)
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1731, in reindex
    method, fill_value, copy).__finalize__(self)
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1749, in _reindex_axes
    allow_dups=False)
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1834, in _reindex_with_indexers
    copy=copy)
  File "C:\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3150, in reindex_indexer
    raise ValueError("cannot reindex from a duplicate axis")
ValueError: cannot reindex from a duplicate axis

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 561, in wrapper
    return self.apply(curried)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 662, in apply
    return self._python_apply_general(f)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 669, in _python_apply_general
    not_indexed_same=mutated)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 2380, in _wrap_applied_output
    not_indexed_same=not_indexed_same)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 1142, in _concat_objects
    result = result.reindex(ax)
  File "C:\Anaconda3\lib\site-packages\pandas\core\series.py", line 2149, in reindex
    return super(Series, self).reindex(index=index, **kwargs)
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1731, in reindex
    method, fill_value, copy).__finalize__(self)
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1749, in _reindex_axes
    allow_dups=False)
  File "C:\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1834, in _reindex_with_indexers
    copy=copy)
  File "C:\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3150, in reindex_indexer
    raise ValueError("cannot reindex from a duplicate axis")
ValueError: cannot reindex from a duplicate axis

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 570, in wrapper
    return self._aggregate_item_by_item(name, *args, **kwargs)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 511, in __getattr__
    (type(self).__name__, attr))
AttributeError: 'SeriesGroupBy' object has no attribute '_aggregate_item_by_item'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 3.4.1\helpers\pydev\pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm 3.4.1\helpers\pydev\pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm 3.4.1\helpers\pydev\_pydev_execfile.py", line 38, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
  File "G:/Users/ari/Documents/Git/Misc/testbross.py", line 5, in <module>
    brwbt = KevinsDB.KevinToWBT(brdb.both[brdb.both.SYMBOL=='VCLT'])
  File "G:\Users\ari\Documents\Git\KevinsDB.py", line 95, in KevinToWBT
    s = grp.OrderTime.ffill().bfill()
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 572, in wrapper
    raise ValueError
ValueError

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDuplicate ReportDuplicate issue or pull requestGroupbyMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions