Repository: pandas-dev/pandas

Issue:
FutureWarning difficult to identify source
This is a suggestion, not a bug. The following resulted in a `FutureWarning`

    df = df.assign(block=(df.index.to_series().diff() != pd.Timedelta(15,'m')).cumsum())

> FutureWarning: The default of the 'keep_tz' keyword will change to True in a future release. You can set 'keep_tz=True' to obtain the future behaviour and silence this warning.

It wasn't in any way obvious why since the warning doesn't mention the method name and there's no stack trace (and futures warnings are only printed once). I had to search the source (which was also difficult as the message was split to maintain a character limit per line).

It would be nice if future warnings were more explicit, i.e. name the class and method as well as the argument. 

(In this case it's the `to_series()` method, which ideally wouldn't be required but `DateTimeIndex` doesn't yet implement diff())


Discussion:
FYI, you can use the warnings module to elevate warnings to errors so that you get a traceback:

```pytb
In [14]: warnings.simplefilter('error', FutureWarning)

In [15]: pd.date_range(2000, periods=4, tz='utc').to_series()
---------------------------------------------------------------------------
FutureWarning                             Traceback (most recent call last)
<ipython-input-15-0d004c39654b> in <module>
----> 1 pd.date_range(2000, periods=4, tz='utc').to_series()

~/Envs/dask-dev/lib/python3.7/site-packages/pandas/core/indexes/datetimes.py in to_series(self, keep_tz, index, name)
    725                           "to True in a future release. You can set "
    726                           "'keep_tz=True' to obtain the future behaviour and "
--> 727                           "silence this warning.", FutureWarning, stacklevel=2)
    728             keep_tz = False
    729         elif keep_tz is False:

FutureWarning: The default of the 'keep_tz' keyword will change to True in a future release. You can set 'keep_tz=True' to obtain the future behaviour and silence this warning.
```

Changing the message to note something like

    The default of the 'keep_tz' keyword in DatetimeIndex.to_series ...

seems OK. Are you likely to work on this?


Resolving patch:
diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py
--- a/pandas/core/indexes/datetimes.py
+++ b/pandas/core/indexes/datetimes.py
@@ -689,7 +689,8 @@ def to_series(self, keep_tz=None, index=None, name=None):
             name = self.name
 
         if keep_tz is None and self.tz is not None:
-            warnings.warn("The default of the 'keep_tz' keyword will change "
+            warnings.warn("The default of the 'keep_tz' keyword in "
+                          "DatetimeIndex.to_series will change "
                           "to True in a future release. You can set "
                           "'keep_tz=True' to obtain the future behaviour and "
                           "silence this warning.", FutureWarning, stacklevel=2)


Test patch:
diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py
--- a/pandas/tests/frame/test_alter_axes.py
+++ b/pandas/tests/frame/test_alter_axes.py
@@ -460,9 +460,13 @@ def test_convert_dti_to_series(self):
                       name='B')
         tm.assert_series_equal(result, comp)
 
-        with tm.assert_produces_warning(FutureWarning):
+        with tm.assert_produces_warning(FutureWarning) as m:
             result = idx.to_series(index=[0, 1])
         tm.assert_series_equal(result, expected.dt.tz_convert(None))
+        msg = ("The default of the 'keep_tz' keyword in "
+               "DatetimeIndex.to_series will change to True in a future "
+               "release.")
+        assert msg in str(m[0].message)
 
         with tm.assert_produces_warning(FutureWarning):
             result = idx.to_series(keep_tz=False, index=[0, 1])