Skip to content

Dask.dataframe metadata handling loses information #1209

Closed

Description

@jcrist

Currently metadata is passed into dataframe objects as either a list of columns/column name, or a pandas object. If it's a pandas object, the dtype is said to be known, and the object is stored as a _pd attribute. Otherwise, the dtype is unknown, and a dummy pandas object with all object dtypes is created. Using the scheme, metadata is bubbled throughout, and most things work. However, this fails in the following situation:

  1. Create a dask dataframe with column input, and unknown dtype. This means that: _pd is a dataframe with all object dtype, and _known_dtype is False.
  2. Do any operation that doesn't first compute the dtype if it's unknown (for example, extract a column). In this case, the pandas dataframe would be converted to a pandas series (still with object dtype), and passed in as metadata to the dd.Series object.
  3. Because dd.Series received a pandas object instead of a string/list, it sets _known_dtype = True, which causes issues later.

Example in code:

In [1]: import dask.dataframe as dd

In [2]: import pandas as pd

In [3]: df = pd.DataFrame({'x': [1, 2, 2, 2, 4], 'y': ['a', 'b', 'b', 'b', 'c']})

In [4]: dsk = {('test', 0): df}

In [5]: ddf = dd.DataFrame(dsk, 'test', ['x', 'y'], [0, 4])

In [6]: ddf._known_dtype    # dtype is unknown here
Out[6]: False

In [7]: ddf._pd
Out[7]:
Empty DataFrame
Columns: [x, y]
Index: []

In [8]: x = ddf.x

In [9]: x._known_dtype    # But after selecting a column, `_known_dtype` is incorrectly True
Out[9]: True

In [10]: x._pd
Out[10]: Series([], Name: x, dtype: object)

In [11]: x.dtype    # wrong result :/
Out[11]: dtype('O')

In [12]: x.compute().dtype
Out[12]: dtype('int64')

Proposal:

  • Track state as a tuple (or namedtuple) of (pandas object, known_dtype). This will be stored as a _meta attribute. We could forward the _pd and _known_dtype attributes as properties to just index this tuple. I like the use of a tuple, as it ties the two things together, since both are part of the state of the object.
  • Constructors can also accept a tuple of this form, and will set the state appropriately
  • Rigorous testing to track down and squash all the places where we're being too lose with dtype inference (this is the non-fun part :/)

Activity

  1. jcrist commented on Jun 2, 2016

    @jcrist
    MemberAuthor

    Ping @sinhrks , @mrocklin for thoughts.

  2. mrocklin commented on Jun 2, 2016

    @mrocklin
    Member

    I'm in favor of a rather expensive option of dropping known_dtype entirely and forcing the knowledge of a full typed dataframe. While somewhat annoying I suspect that this is doable by most users and I suspect that it will significantly reduce maintenance costs and surprises going forward.

  3. jcrist commented on Jun 2, 2016

    @jcrist
    MemberAuthor

    That sounds reasonable to me. It should really only affect the creation functions - after that everything should be fairly easily inferred.

  4. sinhrks commented on Jun 3, 2016

    @sinhrks
    Member

    My idea is to change known_dtype to a property:

       @property
       def known_dtype(self):
          return isinstance(self._pd, pd.DataFrame)
    

    @mrocklin 's idea is good if we can have transition period.

  5. jcrist commented on Jun 3, 2016

    @jcrist
    MemberAuthor

    Right now it's always a dataframe or series (lists of columns are converted to an empty frame in __init__). Are you suggesting not doing that?

    What do you mean by transition period? Keep the property, but always set it to true?

  6. sinhrks commented on Jun 12, 2016

    @sinhrks
    Member

    Misunderstood, forget about my comment about property.

    What I meant with "transition period" is not to change current behavior in next version to show warning for future change. Though I understand it doesn't affects to most of users except who directly creates dd.DataFrame.

  7. added this to the 0.11.0 milestone on Aug 22, 2016
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions