Repository navigation
Closed
Description
What happened:
Repartitioning a dask dataframe indexed with a TZ-aware datetime index of type 'datetime64[ns, UTC]' fails with: TypeError: Cannot interpret 'datetime64[ns, UTC]' as a data type
What you expected to happen:
Repartitioning should work as expected with the TZ-naive index.
Minimal Complete Verifiable Example:
import pandas as pd
from datetime import datetime, timedelta
import dask.dataframe
# Create some test data with a naive datetime index
vals = range(10)
dts = [datetime(2020,1,1,12,0) + timedelta(minutes=x) for x in val]
idx = pd.to_datetime(dts, utc=False)
s = pd.Series(vals, idx)
# Turn into dask dataframe
ddf = dask.dataframe.from_pandas(s, npartitions=1)
# This works as expected
ddf.repartition(5)
# Let's convert the index to a TZ-aware datetime index with TZ set to UTC
ddf.index = dask.dataframe.to_datetime(ddf.index, utc=True)
# No trouble so far
ddf.head()
ddf.repartition(3)
# TypeError: Cannot interpret 'datetime64[ns, UTC]' as a data typeAnything else we need to know?:
Full error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_3646785/374576154.py in <module>
17 ddf.index = dask.dataframe.to_datetime(ddf.index, utc=True)
18
---> 19 ddf.repartition(3)
~/.conda/envs/dwc2/lib/python3.9/site-packages/dask/dataframe/core.py in repartition(self, divisions, npartitions, partition_size, freq, force)
1317 return repartition_size(self, partition_size)
1318 elif npartitions is not None:
-> 1319 return repartition_npartitions(self, npartitions)
1320 elif divisions is not None:
1321 return repartition(self, divisions, force=force)
~/.conda/envs/dwc2/lib/python3.9/site-packages/dask/dataframe/core.py in repartition_npartitions(df, npartitions)
6977 original_divisions = divisions = pd.Series(df.divisions)
6978 if df.known_divisions and (
-> 6979 np.issubdtype(divisions.dtype, np.datetime64)
6980 or np.issubdtype(divisions.dtype, np.number)
6981 ):
~/.conda/envs/dwc2/lib/python3.9/site-packages/numpy/core/numerictypes.py in issubdtype(arg1, arg2)
417 """
418 if not issubclass_(arg1, generic):
--> 419 arg1 = dtype(arg1).type
420 if not issubclass_(arg2, generic):
421 arg2 = dtype(arg2).type
TypeError: Cannot interpret 'datetime64[ns, UTC]' as a data type
Environment:
- Dask version: 2022.2.1
- Python version: 3.9.9
- Operating System: Linux Centos 8
- Install method (conda, pip, source): conda
Activity
[-]Cannot repartition dataframe with TZ-aware datetime64[/-][+]Cannot repartition dataframe with TZ-aware datetime64 index[/+]jaj42 commentedon Mar 8, 2022
I think the key issue is that timezone-aware datetime64 dtypes are a feature of Pandas which is not supported by NumPy.
Here is the relevant Pandas documentation:
https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-timezone-series
Trying to interpret the dtype from numpy will result in an error.
One workaround would be to only support UTC and apply .to_numpy(dtype="datetime64[ns]") are explained in documentation above. This will convert to UTC and remove timezone information making the array usable by numpy.
jcrist commentedon Mar 8, 2022
Yeah, this is the same issue as #8779 , our partitioning code needs to be updated to work with the pandas custom dtypes.
scharlottej13 commentedon Mar 9, 2022
Thanks @jcrist and @jaj42! I closed #8779 since we can address the larger issue of updating the partitioning code (specifically
percentiles_summary) to work with both pandas"string"and pandasdatetimehere.Maybe we could edit the title of this issue to something like "Update partitioning to work with custom
pandas dtypes"?[-]Cannot repartition dataframe with TZ-aware datetime64 index[/-][+]Update dataframe partitioning to work wth pandas custom dtypes[/+]jorloplaz commentedon May 12, 2022
Possible duplicate of this issue by @mrocklin and another issue by @jrbourbeau. Should we close those ones and unify them in this one?
[-]Update dataframe partitioning to work wth pandas custom dtypes[/-][+]Update dataframe partitioning to work with pandas custom dtypes[/+]