Skip to content

Update dataframe partitioning to work with pandas custom dtypes #8788

Closed

Description

@jaj42

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 type

Anything 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

  1. added
    needs triage
    on Mar 8, 2022
  2. changed the title [-]Cannot repartition dataframe with TZ-aware datetime64[/-] [+]Cannot repartition dataframe with TZ-aware datetime64 index[/+] on Mar 8, 2022
  3. jaj42 commented on Mar 8, 2022

    @jaj42
    Author

    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.

  4. jcrist commented on Mar 8, 2022

    @jcrist
    Member

    Yeah, this is the same issue as #8779 , our partitioning code needs to be updated to work with the pandas custom dtypes.

  5. added and removed
    needs triage
    on Mar 8, 2022
  6. scharlottej13 commented on Mar 9, 2022

    @scharlottej13
    Contributor

    Yeah, this is the same issue as #8779 , our partitioning code needs to be updated to work with the pandas custom dtypes.

    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 pandas datetime here.

    Maybe we could edit the title of this issue to something like "Update partitioning to work with custom pandas dtypes"?

  7. changed the title [-]Cannot repartition dataframe with TZ-aware datetime64 index[/-] [+]Update dataframe partitioning to work wth pandas custom dtypes[/+] on Mar 9, 2022
  8. added
    p2
    on Mar 16, 2022
  9. jorloplaz commented on May 12, 2022

    @jorloplaz
    Contributor

    Possible duplicate of this issue by @mrocklin and another issue by @jrbourbeau. Should we close those ones and unify them in this one?

  10. changed the title [-]Update dataframe partitioning to work wth pandas custom dtypes[/-] [+]Update dataframe partitioning to work with pandas custom dtypes[/+] on Sep 28, 2022
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

    dataframep2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions