Skip to content

BUG: DataFrame.to_records() bug in converting datetime64 index #14044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
is_object_dtype,
is_extension_type,
is_datetimetz,
is_datetime64_dtype,
is_datetime64_any_dtype,
is_datetime64tz_dtype,
is_bool_dtype,
is_integer_dtype,
Expand Down Expand Up @@ -1084,7 +1084,7 @@ def to_records(self, index=True, convert_datetime64=True):
y : recarray
"""
if index:
if is_datetime64_dtype(self.index) and convert_datetime64:
if is_datetime64_any_dtype(self.index) and convert_datetime64:
ix_vals = [self.index.to_pydatetime()]
else:
if isinstance(self.index, MultiIndex):
Expand Down
39 changes: 33 additions & 6 deletions pandas/tests/frame/test_convert_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

from __future__ import print_function

from numpy import nan
import numpy as np
import datetime

from pandas import compat
from pandas import (DataFrame, Series, MultiIndex, Timestamp,
date_range)
import pytz

import numpy as np
import pandas.util.testing as tm

from numpy import nan
from pandas import DataFrame, MultiIndex, Series, Timestamp, compat, date_range
from pandas.tests.frame.common import TestData


Expand Down Expand Up @@ -179,3 +178,31 @@ def test_to_records_with_unicode_index(self):
.to_records()
expected = np.rec.array([('x', 'y')], dtype=[('a', 'O'), ('b', 'O')])
tm.assert_almost_equal(result, expected)

def test_to_records_with_tz_gmt(self):
data = [datetime.datetime.now(pytz.timezone('UTC')) for _ in range(10)]
df = DataFrame({'datetime': data})
df.set_index('datetime', inplace=True)
dfgmt = df.tz_convert("GMT")
tm.assert_frame_equal(df.to_records()['datetime'][0].tzinfo,
dfgmt.to_records()['datetime'][0].tzinfo)

def test_to_records_with_tz_convert_to_utc(self):
data_utc = [datetime.datetime.now(
pytz.timezone('UTC')) for _ in range(10)]
df = DataFrame({'datetime': data_utc})
data_gmt = [datetime.datetime.now(
pytz.timezone('GMT')) for _ in range(10)]
df2 = DataFrame({'datetime': data_gmt})
df.set_index('datetime', inplace=True)
df2.set_index('datetime', inplace=True)
dfgmt = df2.tz_convert("UTC")
tm.assert_frame_equal(df.to_records()['datetime'][0].tzinfo,
dfgmt.to_records()['datetime'][0].tzinfo)

def test_to_records_with_tzinfo(self):
data = [datetime.datetime.now(pytz.timezone('UTC')) for _ in range(10)]
df = DataFrame({'datetime': data})
df.set_index('datetime', inplace=True)
tm.assert_frame_equal(df.index.tzinfo,
df.to_records()['datetime'][0].tzinfo)