Repository: dask/dask

Issue:
`dd.to_timedelta` fails
I am trying to convert a column into timedelta, it's possible to do a workaround using pandas, but also wanted to flag the bug here:

```python
from dask.dataframe import to_timedelta
from pandas import to_timedelta as to_timedelta_pandas

test = "1 day 1 hour"
print(to_timedelta_pandas(test))  # 1 days 01:00:00
print(to_timedelta(test))  # fails
```

Here's the traceback:

```pytb
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [108], in <cell line: 6>()
      4 test = "1 day 1 hour"
      5 print(to_timedelta_pandas(test)) # 1 days 01:00:00
----> 6 print(to_timedelta(test))

File ~/miniconda/envs/coiled_to_datetime/lib/python3.10/site-packages/dask/dataframe/core.py:7438, in to_timedelta(arg, unit, errors)
   7435 @wraps(pd.to_timedelta)
   7436 def to_timedelta(arg, unit="ns", errors="raise"):
   7437     meta = pd.Series([pd.Timedelta(1, unit=unit)])
-> 7438     return map_partitions(pd.to_timedelta, arg, unit=unit, errors=errors, meta=meta)

File ~/miniconda/envs/coiled_to_datetime/lib/python3.10/site-packages/dask/dataframe/core.py:6374, in map_partitions(func, meta, enforce_metadata, transform_divisions, align_dataframes, *args, **kwargs)
   6371         simple = False
   6373 if align_dataframes:
-> 6374     divisions = dfs[0].divisions
   6375 else:
   6376     # Unaligned, dfs is a mix of 1 partition and 1+ partition dataframes,
   6377     # use longest divisions found
   6378     divisions = max((d.divisions for d in dfs), key=len)

IndexError: list index out of range
```

My guess is that the bug is in these lines (assuming  default `unit` to be `ns`):

https://github.com/dask/dask/blob/2869a85a032c501db86f781bd6cc990b79475fe3/dask/dataframe/core.py#L7435-L7438


**Environment**:

- Dask version: 2022.4.2
- Python version: 3.10
- Install method (conda, pip, source): conda


Discussion:
A more extended example is here:

This works: 

```python
from pandas import DataFrame, to_datetime, to_timedelta

df = DataFrame({"dates": ["2020-01-01"], "timedeltas": ["1 day 2 hours"]})
df["date_parsed"] = to_datetime(df["dates"])
df["timedelta_parsed"] = to_timedelta(df["timedeltas"])

print(df)
```

This doesn't work:

```python
from dask.dataframe import from_pandas, to_datetime, to_timedelta
from pandas import DataFrame

df = from_pandas(
    DataFrame({"dates": ["2020-01-01"], "timedeltas": ["1 day 2 hours"]}), npartitions=1
)
df["date_parsed"] = to_datetime(df["dates"])
df["timedelta_parsed"] = to_timedelta(df["timedeltas"])

df.compute()  # this fails
```
@SultanOrazbayev Thanks for reporting this!

> My guess is that the bug is in these lines (assuming default unit to be ns):

That sounds right, the [`unit` parameter defaults changed in pandas=1.1.0](https://github.com/pandas-dev/pandas/pull/34634). I'll open a PR. :)

Resolving patch:
diff --git a/dask/dataframe/core.py b/dask/dataframe/core.py
--- a/dask/dataframe/core.py
+++ b/dask/dataframe/core.py
@@ -7433,7 +7433,9 @@ def to_datetime(arg, meta=None, **kwargs):
 
 
 @wraps(pd.to_timedelta)
-def to_timedelta(arg, unit="ns", errors="raise"):
+def to_timedelta(arg, unit=None, errors="raise"):
+    if not PANDAS_GT_110 and unit is None:
+        unit = "ns"
     meta = pd.Series([pd.Timedelta(1, unit=unit)])
     return map_partitions(pd.to_timedelta, arg, unit=unit, errors=errors, meta=meta)
 


Test patch:
diff --git a/dask/dataframe/tests/test_dataframe.py b/dask/dataframe/tests/test_dataframe.py
--- a/dask/dataframe/tests/test_dataframe.py
+++ b/dask/dataframe/tests/test_dataframe.py
@@ -4273,6 +4273,16 @@ def test_to_timedelta():
     ds = dd.from_pandas(s, npartitions=2)
     assert_eq(pd.to_timedelta(s, errors="coerce"), dd.to_timedelta(ds, errors="coerce"))
 
+    s = pd.Series(["1", 2, "1 day 2 hours"])
+    ds = dd.from_pandas(s, npartitions=2)
+    assert_eq(pd.to_timedelta(s), dd.to_timedelta(ds))
+
+    if PANDAS_GT_110:
+        with pytest.raises(
+            ValueError, match="unit must not be specified if the input contains a str"
+        ):
+            dd.to_timedelta(ds, unit="s").compute()
+
 
 @pytest.mark.parametrize("values", [[np.NaN, 0], [1, 1]])
 def test_isna(values):