Repository navigation
Closed
Description
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:
- Create a dask dataframe with column input, and unknown dtype. This means that:
_pdis a dataframe with all object dtype, and_known_dtypeis False. - 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.Seriesobject. - Because
dd.Seriesreceived 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
_metaattribute. We could forward the_pdand_known_dtypeattributes 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
jcrist commentedon Jun 2, 2016
Ping @sinhrks , @mrocklin for thoughts.
mrocklin commentedon Jun 2, 2016
I'm in favor of a rather expensive option of dropping
known_dtypeentirely 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.jcrist commentedon Jun 2, 2016
That sounds reasonable to me. It should really only affect the creation functions - after that everything should be fairly easily inferred.
sinhrks commentedon Jun 3, 2016
My idea is to change
known_dtypeto a property:@mrocklin 's idea is good if we can have transition period.
jcrist commentedon Jun 3, 2016
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?
sinhrks commentedon Jun 12, 2016
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.