Repository: dask/dask

Issue:
Update make_timeseries for pandas deprecation
```python
In [3]: dd.demo.make_timeseries()
/Users/taugspurger/sandbox/dask/dask/dataframe/io/demo.py:91: FutureWarning: Creating a DatetimeIndex by passing range endpoints is deprecated.  Use `pandas.date_range` instead.
  freq=partition_freq))
/Users/taugspurger/sandbox/dask/dask/dataframe/io/demo.py:45: FutureWarning: Creating a DatetimeIndex by passing range endpoints is deprecated.  Use `pandas.date_range` instead.
  index = pd.DatetimeIndex(start=start, end=end, freq=freq, name='timestamp')
Out[3]:
Dask DataFrame Structure:
                   id    name        x        y
npartitions=11
2000-01-31      int64  object  float64  float64
2000-02-29        ...     ...      ...      ...
...               ...     ...      ...      ...
2000-11-30        ...     ...      ...      ...
2000-12-31        ...     ...      ...      ...
Dask Name: make-timeseries, 11 tasks
```

Switching to `pd.date_range` should be fine.

Discussion:
HSR05: ref : https://github.com/dask/dask/issues/4467
mrocklin: cc @TomAugspurger or @jrbourbeau in case you have time to review

On Wed, Feb 27, 2019 at 10:41 AM HSR05 <notifications@github.com> wrote:

> ref : #4467 <https://github.com/dask/dask/issues/4467>
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/dask/dask/pull/4530#issuecomment-467980630>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AASszP72pKYGFJvIvXs0TfCeoj29Q2lyks5vRtFQgaJpZM4bVMZ2>
> .
>

TomAugspurger: I see a few more uses of the deprecated pandas behavior throughout dask. Do you mind updating those while you're at it?

```
dask/dataframe/io/tests/test_demo.py
21:    assert df.divisions == tuple(pd.DatetimeIndex(start='2000', end='2015',

dask/dataframe/io/demo.py
45:    index = pd.DatetimeIndex(start=start, end=end, freq=freq, name='timestamp')
90:    divisions = list(pd.DatetimeIndex(start=start, end=end,

dask/dataframe/tests/test_dataframe.py
1393:    ind = pd.DatetimeIndex(start=start, end=end, freq='60s')

dask/dataframe/core.py
4296:    divisions = pd.DatetimeIndex(start=start,
```


```
dask/dataframe/utils.py
393:        return pd.PeriodIndex(start='1970-01-01', periods=2, freq=idx.freq,
```
HSR05: @TomAugspurger yeah sure. I'll look into those and update it asap. 👍 

Patch:
diff --git a/dask/dataframe/core.py b/dask/dataframe/core.py
index 826a2a5ed..341c0698f 100644
--- a/dask/dataframe/core.py
+++ b/dask/dataframe/core.py
@@ -4292,9 +4292,9 @@ def repartition_freq(df, freq=None):
         start = df.divisions[0].ceil(freq)
     except ValueError:
         start = df.divisions[0]
-    divisions = pd.DatetimeIndex(start=start,
-                                 end=df.divisions[-1],
-                                 freq=freq).tolist()
+    divisions = pd.date_range(start=start,
+                              end=df.divisions[-1],
+                              freq=freq).tolist()
     if not len(divisions):
         divisions = [df.divisions[0], df.divisions[-1]]
     else:
diff --git a/dask/dataframe/groupby.py b/dask/dataframe/groupby.py
index b115833b4..7caa0d061 100644
--- a/dask/dataframe/groupby.py
+++ b/dask/dataframe/groupby.py
@@ -240,6 +240,7 @@ def _apply_chunk(df, *index, **kwargs):
 def _var_chunk(df, *index):
     if is_series_like(df):
         df = df.to_frame()
+    df = df._get_numeric_data()
     g = _groupby_raise_unaligned(df, by=index)
     x = g.sum()
 
@@ -934,8 +935,11 @@ class _GroupBy(object):
 
     @derived_from(pd.core.groupby.GroupBy)
     def mean(self, split_every=None, split_out=1):
-        return (self.sum(split_every=split_every, split_out=split_out) /
-                self.count(split_every=split_every, split_out=split_out))
+        s = self.sum(split_every=split_every, split_out=split_out)
+        c = self.count(split_every=split_every, split_out=split_out)
+        if is_dataframe_like(s):
+            c = c[s.columns]
+        return s / c
 
     @derived_from(pd.core.groupby.GroupBy)
     def size(self, split_every=None, split_out=1):
diff --git a/dask/dataframe/indexing.py b/dask/dataframe/indexing.py
index a1e190fd7..48046ec5b 100644
--- a/dask/dataframe/indexing.py
+++ b/dask/dataframe/indexing.py
@@ -8,6 +8,7 @@ import numpy as np
 import pandas as pd
 
 from .core import new_dd_object, Series
+from .utils import is_index_like
 from . import methods
 from ..base import tokenize
 from ..highlevelgraph import HighLevelGraph
@@ -301,7 +302,7 @@ def _maybe_partial_time_string(index, indexer, kind):
     if data has DatetimeIndex/PeriodIndex
     """
     # do not pass dd.Index
-    assert isinstance(index, pd.Index)
+    assert is_index_like(index)
 
     if not isinstance(index, (pd.DatetimeIndex, pd.PeriodIndex)):
         return indexer
diff --git a/dask/dataframe/io/demo.py b/dask/dataframe/io/demo.py
index 36fa0f39a..15d50e923 100644
--- a/dask/dataframe/io/demo.py
+++ b/dask/dataframe/io/demo.py
@@ -42,7 +42,7 @@ make = {float: make_float,
 
 
 def make_timeseries_part(start, end, dtypes, freq, state_data):
-    index = pd.DatetimeIndex(start=start, end=end, freq=freq, name='timestamp')
+    index = pd.date_range(start=start, end=end, freq=freq, name='timestamp')
     state = np.random.RandomState(state_data)
     columns = dict((k, make[dt](len(index), state)) for k, dt in dtypes.items())
     df = pd.DataFrame(columns, index=index, columns=sorted(columns))
@@ -87,8 +87,8 @@ def make_timeseries(start='2000-01-01',
     2000-01-01 06:00:00   960   Charlie  0.788245
     2000-01-01 08:00:00  1031     Kevin  0.466002
     """
-    divisions = list(pd.DatetimeIndex(start=start, end=end,
-                                      freq=partition_freq))
+    divisions = list(pd.date_range(start=start, end=end,
+                                   freq=partition_freq))
     state_data = random_state_data(len(divisions) - 1, seed)
     name = 'make-timeseries-' + tokenize(start, end, dtypes, freq,
                                          partition_freq, state_data)
diff --git a/dask/dataframe/utils.py b/dask/dataframe/utils.py
index 681d070ba..c54825b17 100644
--- a/dask/dataframe/utils.py
+++ b/dask/dataframe/utils.py
@@ -390,8 +390,8 @@ def _nonempty_index(idx):
         return pd.DatetimeIndex(data, start=start, periods=2, freq=idx.freq,
                                 tz=idx.tz, name=idx.name)
     elif typ is pd.PeriodIndex:
-        return pd.PeriodIndex(start='1970-01-01', periods=2, freq=idx.freq,
-                              name=idx.name)
+        return pd.period_range(start='1970-01-01', periods=2, freq=idx.freq,
+                               name=idx.name)
     elif typ is pd.TimedeltaIndex:
         start = np.timedelta64(1, 'D')
         data = [start, start + 1] if idx.freq is None else None


Test patch:
diff --git a/dask/dataframe/io/tests/test_demo.py b/dask/dataframe/io/tests/test_demo.py
index b099c4590..443e744b2 100644
--- a/dask/dataframe/io/tests/test_demo.py
+++ b/dask/dataframe/io/tests/test_demo.py
@@ -18,8 +18,7 @@ def test_make_timeseries():
     assert df['C'].head().dtype == object
     assert df.index.name == 'timestamp'
     assert df.head().index.name == df.index.name
-    assert df.divisions == tuple(pd.DatetimeIndex(start='2000', end='2015',
-                                                  freq='6M'))
+    assert df.divisions == tuple(pd.date_range(start='2000', end='2015',freq='6M'))
 
     tm.assert_frame_equal(df.head(), df.head())
 
diff --git a/dask/dataframe/tests/test_dataframe.py b/dask/dataframe/tests/test_dataframe.py
index 83742dad0..6422db371 100644
--- a/dask/dataframe/tests/test_dataframe.py
+++ b/dask/dataframe/tests/test_dataframe.py
@@ -1390,7 +1390,7 @@ def test_repartition_object_index():
 def test_repartition_freq(npartitions, freq, start, end):
     start = pd.Timestamp(start)
     end = pd.Timestamp(end)
-    ind = pd.DatetimeIndex(start=start, end=end, freq='60s')
+    ind = pd.date_range(start=start, end=end, freq='60s')
     df = pd.DataFrame({'x': np.arange(len(ind))}, index=ind)
     ddf = dd.from_pandas(df, npartitions=npartitions, name='x')
 
diff --git a/dask/dataframe/tests/test_groupby.py b/dask/dataframe/tests/test_groupby.py
index 075fcec68..56e34c22d 100644
--- a/dask/dataframe/tests/test_groupby.py
+++ b/dask/dataframe/tests/test_groupby.py
@@ -1516,3 +1516,22 @@ def test_groupby_select_column_agg():
     actual = ddf.groupby('A')['B'].agg('var')
     expected = pdf.groupby('A')['B'].agg('var')
     assert_eq(actual, expected)
+
+
+@pytest.mark.parametrize('func', [
+    lambda x: x.std(),
+    lambda x: x.groupby('x').std(),
+    lambda x: x.groupby('x').var(),
+    lambda x: x.groupby('x').mean(),
+    lambda x: x.groupby('x').sum(),
+    lambda x: x.groupby('x').z.std(),
+])
+def test_std_object_dtype(func):
+    df = pd.DataFrame({
+        'x': [1, 2, 1],
+        'y': ['a', 'b', 'c'],
+        'z': [11., 22., 33.],
+    })
+    ddf = dd.from_pandas(df, npartitions=2)
+
+    assert_eq(func(df), func(ddf))