Repository: pandas-dev/pandas

Issue:
COMPAT: recent numpy deprecation warnings
getting lots of deprecation warnings from numpy.

```
(pandas) bash-3.2$ pytest pandas/tests/test_resample.py pandas/tests/io/json/test_json_table_schema.py 
============================================================================================================ test session starts ============================================================================================================
platform darwin -- Python 3.6.3, pytest-3.3.0, py-1.5.2, pluggy-0.6.0
rootdir: /Users/jreback/pandas, inifile: setup.cfg
plugins: xdist-1.20.1, forked-0.2, cov-2.5.1, hypothesis-3.38.5
collected 350 items                                                                                                                                                                                                                         

pandas/tests/test_resample.py ...................................................................................................................ssss................................................................................ [ 56%]
...............................ssss..................................                                                                                                                                                                 [ 76%]
pandas/tests/io/json/test_json_table_schema.py ...................................................x..x..x..x..xxxx..x..x..X..X...                                                                                                     [100%]

============================================================================================================= warnings summary ==============================================================================================================
pandas/tests/io/json/test_json_table_schema.py::TestTableOrientReader::()::test_read_json_table_orient[vals2-None]
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)

pandas/tests/io/json/test_json_table_schema.py::TestTableOrientReader::()::test_read_json_table_orient[vals2-idx]
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)

pandas/tests/io/json/test_json_table_schema.py::TestTableOrientReader::()::test_read_json_table_orient[vals2-index]
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)

pandas/tests/io/json/test_json_table_schema.py::TestTableOrientReader::()::test_comprehensive
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)
  /Users/jreback/pandas/pandas/core/dtypes/cast.py:706: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
    return arr.astype(dtype)

-- Docs: http://doc.pytest.org/en/latest/warnings.html
================================================================================ 330 passed, 8 skipped, 10 xfailed, 2 xpassed, 16 warnings in 12.85 seconds =================================================================================
```


```
In [1]: arr = np.array(['2016-01-01T00:00:00.000Z', '2016-01-02T00:00:00.000Z',
   ...:        '2016-01-03T00:00:00.000Z', '2016-01-04T00:00:00.000Z'], dtype=object)
   ...: 

In [2]: arr.astype('<M8[ns]')
/Users/jreback/miniconda3/envs/pandas/bin/ipython:1: DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future
  #!/Users/jreback/miniconda3/envs/pandas/bin/python
Out[2]: 
array(['2016-01-01T00:00:00.000000000', '2016-01-02T00:00:00.000000000',
       '2016-01-03T00:00:00.000000000', '2016-01-04T00:00:00.000000000'], dtype='datetime64[ns]')
```

I haven't seen these before, so a recent change must have triggered


Discussion:
cc @jbrockmendel cc @jschendel cc @WillAyd 
  
Just ran on upstream/master and did not get any warnings - are we running the same version?

**pd.show_versions()**
<details>

INSTALLED VERSIONS
------------------
commit: 16d02621212cd7d83f89533265fc039df1d7be10
python: 3.6.3.final.0
python-bits: 64
OS: Darwin
OS-release: 17.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0.dev0+79.g16d026212
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: 0.26.1
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.8.0
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.1
openpyxl: 2.5.0b1
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.1.13
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: 0.1.2
fastparquet: 0.1.3
pandas_gbq: None
pandas_datareader: None

</details>
export PANDAS_TESTING_MODE=deprecate
first ; DeprecationWarnings are normally suppressed
we enable them in CI
Hmm OK. It appears the line

```python
pd.date_range('2016-01-01', freq='d', periods=4)
```

Is triggering this within the tests. Not immediately apparent to me why that would be the case but will take a look
So it looks like ``write_json`` is outputting the date range in the format that you provided above. Are we on the same page that we would have to change that behavior to support this?

This is without digging into the json code, but I suspect rather than changing the output format, we should instead replace uses of numpy conversion `.astype(<datetime>)` with our internal parser `pd.to_datetime(...)`
Odd how this is case, since I think that warning was created partially in response to `pandas`:

https://github.com/numpy/numpy/blob/2bacec9ecb0fc665117a22027b73812509a99f56/doc/release/1.11.0-notes.rst#compatibility-notes
shoot. #19224 reversed this :<

Resolving patch:
diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py
--- a/pandas/core/dtypes/cast.py
+++ b/pandas/core/dtypes/cast.py
@@ -649,10 +649,12 @@ def astype_nansafe(arr, dtype, copy=True):
     if issubclass(dtype.type, text_type):
         # in Py3 that's str, in Py2 that's unicode
         return lib.astype_unicode(arr.ravel()).reshape(arr.shape)
+
     elif issubclass(dtype.type, string_types):
         return lib.astype_str(arr.ravel()).reshape(arr.shape)
+
     elif is_datetime64_dtype(arr):
-        if dtype == object:
+        if is_object_dtype(dtype):
             return tslib.ints_to_pydatetime(arr.view(np.int64))
         elif dtype == np.int64:
             return arr.view(dtype)
@@ -666,10 +668,10 @@ def astype_nansafe(arr, dtype, copy=True):
                                                  to_dtype=dtype))
 
     elif is_timedelta64_dtype(arr):
-        if dtype == np.int64:
-            return arr.view(dtype)
-        elif dtype == object:
+        if is_object_dtype(dtype):
             return tslib.ints_to_pytimedelta(arr.view(np.int64))
+        elif dtype == np.int64:
+            return arr.view(dtype)
 
         # in py3, timedelta64[ns] are int64
         if ((PY3 and dtype not in [_INT64_DTYPE, _TD_DTYPE]) or
@@ -696,9 +698,21 @@ def astype_nansafe(arr, dtype, copy=True):
             raise ValueError('Cannot convert non-finite values (NA or inf) to '
                              'integer')
 
-    elif is_object_dtype(arr.dtype) and np.issubdtype(dtype.type, np.integer):
+    elif is_object_dtype(arr):
+
         # work around NumPy brokenness, #1987
-        return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
+        if np.issubdtype(dtype.type, np.integer):
+            return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
+
+        # if we have a datetime/timedelta array of objects
+        # then coerce to a proper dtype and recall astype_nansafe
+
+        elif is_datetime64_dtype(dtype):
+            from pandas import to_datetime
+            return astype_nansafe(to_datetime(arr).values, dtype, copy=copy)
+        elif is_timedelta64_dtype(dtype):
+            from pandas import to_timedelta
+            return astype_nansafe(to_timedelta(arr).values, dtype, copy=copy)
 
     if dtype.name in ("datetime64", "timedelta64"):
         msg = ("Passing in '{dtype}' dtype with no frequency is "
@@ -709,7 +723,6 @@ def astype_nansafe(arr, dtype, copy=True):
         dtype = np.dtype(dtype.name + "[ns]")
 
     if copy:
-
         return arr.astype(dtype, copy=True)
     return arr.view(dtype)
 


Test patch:
diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py
--- a/pandas/tests/frame/test_dtypes.py
+++ b/pandas/tests/frame/test_dtypes.py
@@ -640,6 +640,22 @@ def test_astype_categoricaldtype_class_raises(self, cls):
         with tm.assert_raises_regex(TypeError, xpr):
             df['A'].astype(cls)
 
+    @pytest.mark.parametrize("dtype", ["M8", "m8"])
+    @pytest.mark.parametrize("unit", ['ns', 'us', 'ms', 's', 'h', 'm', 'D'])
+    def test_astype_from_datetimelike_to_objectt(self, dtype, unit):
+        # tests astype to object dtype
+        # gh-19223 / gh-12425
+        dtype = "{}[{}]".format(dtype, unit)
+        arr = np.array([[1, 2, 3]], dtype=dtype)
+        df = DataFrame(arr)
+        result = df.astype(object)
+        assert (result.dtypes == object).all()
+
+        if dtype.startswith('M8'):
+            assert result.iloc[0, 0] == pd.to_datetime(1, unit=unit)
+        else:
+            assert result.iloc[0, 0] == pd.to_timedelta(1, unit=unit)
+
     @pytest.mark.parametrize("arr_dtype", [np.int64, np.float64])
     @pytest.mark.parametrize("dtype", ["M8", "m8"])
     @pytest.mark.parametrize("unit", ['ns', 'us', 'ms', 's', 'h', 'm', 'D'])