Skip to content

Commit f000a4e

Browse files
amolkahatjreback
authored andcommitted
BUG: Fix index for datetime64 conversion. Fixes pandas-dev#13937
closes pandas-dev#13937 Author: Amol Kahat <[email protected]> Closes pandas-dev#14446 from amolkahat/bug_fixes and squashes the following commits: 3806983 [Amol Kahat] Modify test cases.
1 parent 37fe2c4 commit f000a4e

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ Bug Fixes
612612
- Bug in ``GroupBy.get_group()`` failing with a categorical grouper (:issue:`15155`)
613613
- Bug in ``pandas.tools.utils.cartesian_product()`` with large input can cause overflow on windows (:issue:`15265`)
614614

615+
- Bug in ``DataFrame.to_records()`` with converting a ``DatetimeIndex`` with a timezone (:issue:`13937`)
615616

616617

617618
- Bug in ``.groupby(...).rolling(...)`` when ``on`` is specified and using a ``DatetimeIndex`` (:issue:`15130`)

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
is_object_dtype,
3737
is_extension_type,
3838
is_datetimetz,
39-
is_datetime64_dtype,
39+
is_datetime64_any_dtype,
4040
is_datetime64tz_dtype,
4141
is_bool_dtype,
4242
is_integer_dtype,
@@ -1103,7 +1103,7 @@ def to_records(self, index=True, convert_datetime64=True):
11031103
y : recarray
11041104
"""
11051105
if index:
1106-
if is_datetime64_dtype(self.index) and convert_datetime64:
1106+
if is_datetime64_any_dtype(self.index) and convert_datetime64:
11071107
ix_vals = [self.index.to_pydatetime()]
11081108
else:
11091109
if isinstance(self.index, MultiIndex):

pandas/tests/frame/test_convert_to.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
# -*- coding: utf-8 -*-
22

3-
from __future__ import print_function
4-
5-
from numpy import nan
3+
import pytest
64
import numpy as np
75

86
from pandas import compat
97
from pandas import (DataFrame, Series, MultiIndex, Timestamp,
108
date_range)
119

1210
import pandas.util.testing as tm
13-
1411
from pandas.tests.frame.common import TestData
1512

1613

@@ -41,13 +38,13 @@ def test_to_dict(self):
4138

4239
recons_data = DataFrame(test_data).to_dict("sp")
4340
expected_split = {'columns': ['A', 'B'], 'index': ['1', '2', '3'],
44-
'data': [[1.0, '1'], [2.0, '2'], [nan, '3']]}
41+
'data': [[1.0, '1'], [2.0, '2'], [np.nan, '3']]}
4542
tm.assert_dict_equal(recons_data, expected_split)
4643

4744
recons_data = DataFrame(test_data).to_dict("r")
4845
expected_records = [{'A': 1.0, 'B': '1'},
4946
{'A': 2.0, 'B': '2'},
50-
{'A': nan, 'B': '3'}]
47+
{'A': np.nan, 'B': '3'}]
5148
tm.assertIsInstance(recons_data, list)
5249
self.assertEqual(len(recons_data), 3)
5350
for l, r in zip(recons_data, expected_records):
@@ -192,3 +189,18 @@ def test_to_records_with_unicode_column_names(self):
192189
"formats": ['<i8', '<f8']}
193190
)
194191
tm.assert_almost_equal(result, expected)
192+
193+
194+
@pytest.mark.parametrize('tz', ['UTC', 'GMT', 'US/Eastern'])
195+
def test_to_records_datetimeindex_with_tz(tz):
196+
# GH13937
197+
dr = date_range('2016-01-01', periods=10,
198+
freq='S', tz=tz)
199+
200+
df = DataFrame({'datetime': dr}, index=dr)
201+
202+
expected = df.to_records()
203+
result = df.tz_convert("UTC").to_records()
204+
205+
# both converted to UTC, so they are equal
206+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)