Skip to content

Dask 2.1.0, KeyError: 'Column not found: 0' ?  #5124

Closed

Description

@apiszcz

I have a data frame with varying types and am getting the subject error when trying to groupby
After review of some posts on meta and am still not certain where to add meta, since both from_pandas(df, meta) and other attempts fail.

for idx,grp in ddf.groupby(['f1','f2',ddf.dt64.dt.dayofweek,ddf.dt64.dt.hour,ddf.dt64.dt.minute]):
....

C:\g\test\lib\dask\dataframe\groupby.py in __getitem__(self, key)
   1523 
   1524         # error is raised from pandas
-> 1525         g._meta = g._meta[key]
   1526         return g
   1527 

C:\g\test\lib\pandas\core\base.py in __getitem__(self, key)
    266         else:
    267             if key not in self.obj:
--> 268                 raise KeyError("Column not found: {key}".format(key=key))
    269             return self._gotitem(key, ndim=1)
    270 

KeyError: 'Column not found: 0'

Activity

  1. apiszcz commented on Jul 20, 2019

    @apiszcz
    Author
    import pandas as pd
    import dask.dataframe as daskdf
    import numpy as np
    pdf=pd.DataFrame(np.random.randint(0,100,size=(10,8)),columns=list('abcdefgh'))
    for idx,grp in pdf.groupby(list('abcdefgh')[:6]):
        print(idx)
        print(grp,'\n')
    
    print("DASK")
    ddf=daskdf.from_pandas(pdf,npartitions=2)
    for idx,grp in ddf.groupby(list('abcdefgh')[:6]):
        print(idx)
        print(grp,'\n')
    
    (8, 2, 5, 36, 35, 43)
       a  b  c   d   e   f   g   h
    0  8  2  5  36  35  43  41  41 
    
    (20, 39, 48, 45, 0, 47)
        a   b   c   d  e   f   g   h
    2  20  39  48  45  0  47  23  44 
    
    (36, 17, 28, 69, 1, 15)
        a   b   c   d  e   f   g   h
    7  36  17  28  69  1  15  10  56 
    
    (39, 57, 99, 10, 33, 13)
        a   b   c   d   e   f   g   h
    4  39  57  99  10  33  13  85  66 
    
    (41, 92, 47, 0, 63, 42)
        a   b   c  d   e   f   g   h
    8  41  92  47  0  63  42  33  28 
    
    (45, 24, 60, 26, 81, 23)
        a   b   c   d   e   f   g   h
    5  45  24  60  26  81  23  30  19 
    
    (62, 43, 57, 94, 89, 67)
        a   b   c   d   e   f   g   h
    9  62  43  57  94  89  67  85  10 
    
    (70, 53, 82, 39, 5, 83)
        a   b   c   d  e   f   g   h
    6  70  53  82  39  5  83  53  95 
    
    (86, 59, 18, 96, 5, 66)
        a   b   c   d  e   f   g   h
    3  86  59  18  96  5  66  91  92 
    
    (90, 68, 65, 17, 21, 14)
        a   b   c   d   e   f   g   h
    1  90  68  65  17  21  14  33  46 
    
    DASK
    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
    <ipython-input-10-aea283cedd26> in <module>
          9 print("DASK")
         10 ddf=daskdf.from_pandas(pdf,npartitions=2)
    ---> 11 for idx,grp in ddf.groupby(list('abcdefgh')[:6]):
         12     print(idx)
         13     print(grp,'\n')
    
    C:\g\via\lib\dask\dataframe\groupby.py in __getitem__(self, key)
       1523 
       1524         # error is raised from pandas
    -> 1525         g._meta = g._meta[key]
       1526         return g
       1527 
    
    C:\g\via\lib\pandas\core\base.py in __getitem__(self, key)
        266         else:
        267             if key not in self.obj:
    --> 268                 raise KeyError("Column not found: {key}".format(key=key))
        269             return self._gotitem(key, ndim=1)
        270 
    
    KeyError: 'Column not found: 0'
    
    
  2. TomAugspurger commented on Jul 22, 2019

    @TomAugspurger
    Member
  3. apiszcz commented on Jul 22, 2019

    @apiszcz
    Author
  4. TomAugspurger commented on Jul 22, 2019

    @TomAugspurger
    Member
  5. apiszcz commented on Jul 29, 2019

    @apiszcz
    Author
  6. TomAugspurger commented on Jul 29, 2019

    @TomAugspurger
    Member
  7. adampl commented on Aug 23, 2019

    @adampl

    How can I then iterate through all groups as separate dataframes?

  8. jrbourbeau commented on Aug 23, 2019

    @jrbourbeau
    Member

    As pointed out in #5124 (comment), Dask's DataFrameGroupBy doesn't currently implement __iter__. But contributions are always welcome!

  9. adampl commented on Aug 23, 2019

    @adampl

    I tried to use get_group method, but then compute produces another KeyError:

    import pandas as pd
    import dask.dataframe
    df = pd.DataFrame(
        [[1, "foo"], [2, "bar"], [3, "foo"], [4, "bar"], [5, "foo"]],
        columns=["id", "type"],
    )
    x = dask.dataframe.from_pandas(df, npartitions=1)
    
    y = x.groupby("type")
    z = y.get_group("foo")
    z.compute()  # KeyError: 't'

    The key mentioned as missing by the KeyError is always the first character of the grouping column.
    However, this works OK:

    y = x.groupby(["type"])  # column name inside list
    z = y.get_group("foo")
    z.compute()  # no error
  10. jrbourbeau commented on Aug 26, 2019

    @jrbourbeau
    Member

    Thanks for the nice example @adampl, I'm able to reproduce the same KeyError. I believe this is a duplicate of #5080 and a fix along the lines of #5080 (comment) would be very welcome if you have an interest in submitting a PR (no obligation though)

  11. PalakHarwani commented on Jun 18, 2020

    @PalakHarwani

    Is there a solution to this yet ? I want to iterate over the dataframes and I can't use the get_group() method as I don't have the group names(and there are a lot of them)

  12. TomAugspurger commented on Jun 18, 2020

    @TomAugspurger
    Member

    This isn't implemented yet.

    Doing so might be somewhat difficult / surprising for users. In order to support something like

    df.groupby("type").__iter__

    We need to fully load the data to discover all the unique values in type. Users might not be expecting something like __iter__ to be that expensive.

    But that said, if people really do want this then we'd accept a contribution as long as the docs clearly spell out the performance costs.

    I can't use the get_group() method as I don't have the group names(and there are a lot of them)

    Does something like df['type'].unique().compute() give you what you need?

  13. PalakHarwani commented on Jun 19, 2020

    @PalakHarwani

    Yes I was saving unique values to a list and iterating over that for the time being but I was hoping to avoid that and just iterate over groups like we do with pandas.
    Doesn't getting one group using get_group() require dask to go through all the data? It seems to me that getting groups again and again from dask would be more costly as dask would have to go through the data each time I call a group by. Does this happen or am I missing something?

  14. TomAugspurger commented on Jun 19, 2020

    @TomAugspurger
    Member
  15. PalakHarwani commented on Jun 22, 2020

    @PalakHarwani

    Iterating using unique values and calling get_group() is indeed much slower than group_by["col_name"].apply(function) (10 times slower in my case)

  16. added a commit that references this issue on Feb 15, 2022
    de88ce9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions