Repository navigation
Open
Labels
dataframeneeds attentionIt's been a while since this was pushed on. Needs attention from the owner or a maintainer.
Description
What happened:
So I stumbled upon a very random bug. Essentially, when running the head-method with delayed execution on a dask Series with the pandas string type, I stumble upon this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-a1acb387bb63> in <module>
----> 1 test = dask_df["d"].head(compute=False).values
~/opt/miniconda3/envs/wiyd/lib/python3.8/site-packages/dask/dataframe/core.py in values(self)
2739 will not work.
2740 """
-> 2741 return self.map_partitions(methods.values)
2742
2743 def _validate_chunks(self, arr, lengths):
~/opt/miniconda3/envs/wiyd/lib/python3.8/site-packages/dask/dataframe/core.py in map_partitions(self, func, *args, **kwargs)
676 None as the division.
677 """
--> 678 return map_partitions(func, self, *args, **kwargs)
679
680 @insert_meta_param_description(pad=12)
~/opt/miniconda3/envs/wiyd/lib/python3.8/site-packages/dask/dataframe/core.py in map_partitions(func, meta, enforce_metadata, transform_divisions, *args, **kwargs)
5460
5461 graph = HighLevelGraph.from_collections(name, dsk, dependencies=dependencies)
-> 5462 return new_dd_object(graph, name, meta, divisions)
5463
5464
~/opt/miniconda3/envs/wiyd/lib/python3.8/site-packages/dask/dataframe/core.py in new_dd_object(dsk, name, meta, divisions)
6499 for i in range(len(chunks[0])):
6500 layer[(name, i) + suffix] = layer.pop((name, i))
-> 6501 return da.Array(dsk, name=name, chunks=chunks, dtype=meta.dtype)
6502 else:
6503 return get_parallel_type(meta)(dsk, name, meta, divisions)
~/opt/miniconda3/envs/wiyd/lib/python3.8/site-packages/dask/array/core.py in __new__(cls, dask, name, chunks, dtype, meta, shape)
1114 self.dask = dask
1115 self.name = str(name)
-> 1116 meta = meta_from_array(meta, dtype=dtype)
1117
1118 if (
~/opt/miniconda3/envs/wiyd/lib/python3.8/site-packages/dask/array/utils.py in meta_from_array(x, ndim, dtype)
68
69 if isinstance(x, type):
---> 70 x = x(shape=(0,) * (ndim or 0), dtype=dtype)
71
72 if (
TypeError: Cannot interpret 'StringDtype' as a data type
So what fails? (See full example below):
dask_df["d"].head(compute=False).valuesInterestingly enough, running the delayed head-method on the whole dataframe works, so there must be a connection to Series with this bug. Also running the above statement without values works fine too:
dask_df.head(compute=False).values
dask_df["d"].head(compute=False)What you expected to happen:
I would expect the behaviour to not error out and show the values when computed.
Minimal Complete Verifiable Example:
import dask.dataframe as dd
import pandas as pd
df = pd.DataFrame.from_dict(
{
"d": ["a", "aa", "v", "aa", "aa", "aa"],
}
)
df["d"] = df["d"].astype("string")
dask_df = dd.from_pandas(df, npartitions=2)
test = dask_df["d"].head(compute=False).valuesEnvironment:
- Dask version: 2021.02.0
- Python version: 3.8.6
- Operating System: MacOS
- Install method (conda, pip, source): pip
Metadata
Metadata
Assignees
Labels
dataframeneeds attentionIt's been a while since this was pushed on. Needs attention from the owner or a maintainer.
Activity
TomAugspurger commentedon Feb 11, 2021
Is the
.valuesnecessary to trigger the bug? There's another issue with.valuescausing issues with extension arrays.jborchma commentedon Feb 12, 2021
Yeah,
dask_df["d"].head(compute=False)doesn't error.gjoseph92 commentedon Mar 12, 2021
So this doesn't have anything to do with
head(); in fact, it's just about.valueson a delayed Series where the dtype is a pandas extension dtype:When constructing the dask Array's
metaobject, we're currently assuming the underlying array type is a NumPy array, when in this case, it's actually going to be a pandas StringArray. But unlike pandas, NumPy doesn't know how to handle a StringDtype.I'll push up a potential fix for this in a moment.
devyzr commentedon Apr 8, 2021
I'm getting a similar error when using a string column as an index for set_index on a DataFrame:
TypeError: Cannot interpret 'StringDtype' as a data typeMy code is basically the following:
And my pip freeze file is the following:
Is this a bug or expected behavior? Changing the index to an object type solved my issue.
dhirschfeld commentedon Aug 8, 2021
I have
pd.DataFramewith string columns and am hitting this bug when simply trying to convert it to a dask dataframe:In my case it looks like it's casting to
numpywith.valueshere:dask/dask/dataframe/partitionquantiles.py
Line 411 in 758f4e4
jorloplaz commentedon Feb 24, 2022
This is similar to what we're discussing in #5720 . The problem appears to be in
partitionquantiles.py, which thinks that numpydtypesare being used, instead of extended Pandas ones. However, I'm not sure about how to proceed.