Skip to content

ElemWise DataFrame operations by Series #1759

Closed

Description

@simonkamronn

It is not possible to perform trivial operations like standardization of data e.g.

ddf = (ddf - ddf.mean(axis=0))  /  ddf.std(axis=0)

It throws an error saying

ValueError: Not all divisions are known, can't align partitions. 
Please use `set_index` or `set_partition` to set the index.

The output of the reduction operations are Series with one row and same number of columns so it shouldn't need to know the divisions. However, would it make sense to set the divisions of the Series to be the start and end divisions of the DataFrame that was reduced?

Activity

  1. GrierPhillips commented on Mar 10, 2017

    @GrierPhillips

    +1

  2. mrocklin commented on Mar 10, 2017

    @mrocklin
    Member

    Interesting case, I suspect that you're right that it's seeing DataFrame + Series and assuming that the intended operation is row-wise. A good check here would be that the number of partitions of the series is one. I'm in favor of this change. Does anyone have the time to try to implement it?

  3. GrierPhillips commented on Mar 10, 2017

    @GrierPhillips

    Here is a minimal example I constructed from the same example in the documentation for map_partitions. What is interesting that this works with a dask dataframe constructed using from_pandas, but fails when constructed with from_csv.

    import pandas as pd
    import dask.dataframe as dd
    from dask.multiprocessing import get
    
    df = pd.DataFrame({'x': [1, 2, 3, 4, 5],
                       'y': [1., 2., 3., 4., 5.]})
    ddf = dd.from_pandas(df, npartitions=2)
    def myadd(df, a, b=1):
        return df.x + df.y + a + b
    ddf['res'] = ddf.map_partitions(myadd, 1, b=1).compute(get=get)
    # Success!
    df.to_csv('test.csv', index=False)
    ddf2 = dd.read_csv('test.csv')
    ddf2['res'] = ddf2.map_partitions(myadd, 1, b=1).compute(get=get)
    # Fail

    The above code returns

    ValueError: Not all divisions are known, can't align partitions. Please use set_index or set_partition to set the index.

  4. added a commit that references this issue on Mar 14, 2017
  5. added this to the 0.14.1 milestone on Mar 30, 2017
  6. GrierPhillips commented on Mar 31, 2017

    @GrierPhillips

    This still isn't fixed for the minimal example I included. Am I understanding the expected behavior incorrectly? I would assume that whether dask.dataframe is created using from_pandas or read_csv they both should behave the same, but that is clearly not the case.

  7. mrocklin commented on Mar 31, 2017

    @mrocklin
    Member

    You probably don't want to call compute here

    ddf2['res'] = ddf2.map_partitions(myadd, 1, b=1).compute(get=get)
    

    Instead just leave things as a dask dataframe/series

    ddf2['res'] = ddf2.map_partitions(myadd, 1, b=1)
    
  8. mrocklin commented on Mar 31, 2017

    @mrocklin
    Member

    The error here is saying that "OK, you've given me a dask.dataframe with two partitions without any information about how those partitions are aligned. For example I don't know how many rows are in each partition. You've also given me a pandas series with 10 rows (or whatever). I have no idea how to split the pandas series to align with the partitions of the dask dataframe."

  9. GrierPhillips commented on Mar 31, 2017

    @GrierPhillips

    Oh. Interesting. So the values still end up being computed even without calling compute. Is this a lazy execution for example if I tried:

    ddf2['res'] = ddf2.map_partitions(myadd, 1, b=1)
    ddf2.head()

    I would see the 'res' column included. Is the computation executed when head is called? If this is the case is it generally a best practice then to avoid calling compute if possible?

  10. mrocklin commented on Mar 31, 2017

    @mrocklin
    Member

    Yes, dask.dataframe computations build on each other. You want to avoid calling compute for as long as possible.

  11. goyal-neeraj commented on Mar 1, 2018

    @goyal-neeraj

    pandas version 0.22
    dask version 0.17

    The map_partitions is not recognized, am I doing something wrong?

    import pandas as pd
    import dask.dataframe as dd
    df = dd.read_csv('../201712/file*.GZ', compression='gzip', sep='|',  dtype = {'COL1': 'float64', 'COL2' : 'float64', 'COL4': 'str'}, blocksize=None)
    df.repartition(npartitions=df.npartitions*10)
    df['COL3'] = df.map_partitions(lambda x: str(x['COL3']).zfill(11))

    OR

    def func(df, col, z):
         return str(df[col]).zfill(z)
    df['COL3'] = df.map_partitions(func('COL3', 11))
    # Fail
    # File "/usr/local/lib/python3.5/site-packages/pandas/core/generic.py", line 3614, in __getattr__
    #    return object.__getattribute__(self, name)
    # AttributeError: 'DataFrame' object has no attribute 'map_partitions'
  12. mrocklin commented on Mar 1, 2018

    @mrocklin
    Member

    I recommend raising a new issue with a minimal reproducible example.

    https://stackoverflow.com/help/mcve

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions