Repository: pandas-dev/pandas

Issue:
DataFrame.to_records() converts DatetimeIndex to datetime object, not np.datetime64
This code: 
```python
df = pd.DataFrame([pd.to_datetime('2017-11-07')], [pd.to_datetime('2017-11-08')])
df.to_records()
```

Produces a totally different column type for the index vs the data:
```
rec.array([(datetime.datetime(2017, 11, 8, 0, 0), '2017-11-07T00:00:00.000000000')], 
          dtype=[('index', 'O'), ('0', '<M8[ns]')])
```

It should produce:
```
rec.array([('2017-11-08T00:00:00.000000000', '2017-11-07T00:00:00.000000000')], 
          dtype=[('index', '<M8[ns]'), ('0', '<M8[ns]')])
```

I'm using Pandas 0.20.3, NumPy 1.13.1, and Python 3.5.3.



Discussion:
seems buggy
would appreciate a PR
@jreback I just discovered this was broken by #1908 (commit 38333f42) which added the `to_records(convert_datetime64)` option with the horrible default of `True`!

It looks like the option was added in order to work around some now-ancient NumPy bug.  Setting `convert_datetime64=False` now works fine (for my test case and for the one in #1908).

What do you think about changing the default from `True` to `False`?  The current default loses precision and is inefficient.
i would be in favor of deprecating that option entirely 
Does that require a DeprecationWarning first, then later removal, or should we just remove the option at once?
FutureWarning

Resolving patch:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -1311,7 +1311,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
 
         return cls(mgr)
 
-    def to_records(self, index=True, convert_datetime64=True):
+    def to_records(self, index=True, convert_datetime64=None):
         """
         Convert DataFrame to a NumPy record array.
 
@@ -1322,7 +1322,9 @@ def to_records(self, index=True, convert_datetime64=True):
         ----------
         index : boolean, default True
             Include index in resulting record array, stored in 'index' field.
-        convert_datetime64 : boolean, default True
+        convert_datetime64 : boolean, default None
+            .. deprecated:: 0.23.0
+
             Whether to convert the index to datetime.datetime if it is a
             DatetimeIndex.
 
@@ -1376,6 +1378,13 @@ def to_records(self, index=True, convert_datetime64=True):
                    ('2018-01-01T09:01:00.000000000', 2, 0.75)],
                   dtype=[('index', '<M8[ns]'), ('A', '<i8'), ('B', '<f8')])
         """
+
+        if convert_datetime64 is not None:
+            warnings.warn("The 'convert_datetime64' parameter is "
+                          "deprecated and will be removed in a future "
+                          "version",
+                          FutureWarning, stacklevel=2)
+
         if index:
             if is_datetime64_any_dtype(self.index) and convert_datetime64:
                 ix_vals = [self.index.to_pydatetime()]


Test patch:
diff --git a/pandas/tests/frame/test_convert_to.py b/pandas/tests/frame/test_convert_to.py
--- a/pandas/tests/frame/test_convert_to.py
+++ b/pandas/tests/frame/test_convert_to.py
@@ -79,10 +79,23 @@ def test_to_records_dt64(self):
         df = DataFrame([["one", "two", "three"],
                         ["four", "five", "six"]],
                        index=date_range("2012-01-01", "2012-01-02"))
-        assert df.to_records()['index'][0] == df.index[0]
 
-        rs = df.to_records(convert_datetime64=False)
-        assert rs['index'][0] == df.index.values[0]
+        # convert_datetime64 defaults to None
+        expected = df.index.values[0]
+        result = df.to_records()['index'][0]
+        assert expected == result
+
+        # check for FutureWarning if convert_datetime64=False is passed
+        with tm.assert_produces_warning(FutureWarning):
+            expected = df.index.values[0]
+            result = df.to_records(convert_datetime64=False)['index'][0]
+            assert expected == result
+
+        # check for FutureWarning if convert_datetime64=True is passed
+        with tm.assert_produces_warning(FutureWarning):
+            expected = df.index[0]
+            result = df.to_records(convert_datetime64=True)['index'][0]
+            assert expected == result
 
     def test_to_records_with_multindex(self):
         # GH3189