Skip to content

Commit 3806983

Browse files
amolkahatAmol Kahat
authored and
Amol Kahat
committed
Modify test cases.
Fix index for datetime64 conversion This PR fixes index for datetime64 conversion. Also, update tests - pandas/pandas/tests/tests_convert_to.py Fixes #13937
1 parent d0a281f commit 3806983

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-43
Original file line numberDiff line numberDiff line change
@@ -590,99 +590,57 @@ Bug Fixes
590590
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`, :issue:`15424`)
591591
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a numpy array (:issue:`15434`)
592592
- Bug in ``Rolling.quantile`` function that caused a segmentation fault when called with a quantile value outside of the range [0, 1] (:issue:`15463`)
593-
594-
595593
- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)
596594
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)
597595
- Bug in ``Series.ffill()`` with mixed dtypes containing tz-aware datetimes. (:issue:`14956`)
598-
599-
600-
601596
- Bug in ``Series.where()`` and ``DataFrame.where()`` where array-like conditionals were being rejected (:issue:`15414`)
602597
- Bug in ``Series`` construction with a datetimetz (:issue:`14928`)
603598
- Bug in output formatting of a ``MultiIndex`` when names are integers (:issue:`12223`, :issue:`15262`)
604-
605599
- Bug in compat for passing long integers to ``Timestamp.replace`` (:issue:`15030`)
606600
- Bug in ``.loc`` that would not return the correct dtype for scalar access for a DataFrame (:issue:`11617`)
607601
- Bug in ``GroupBy.get_group()`` failing with a categorical grouper (:issue:`15155`)
608602
- Bug in ``pandas.tools.utils.cartesian_product()`` with large input can cause overflow on windows (:issue:`15265`)
609-
610-
611-
612603
- Bug in ``.groupby(...).rolling(...)`` when ``on`` is specified and using a ``DatetimeIndex`` (:issue:`15130`)
613-
614-
615604
- Bug in ``to_sql`` when writing a DataFrame with numeric index names (:issue:`15404`).
616605
- Bug in ``Series.iloc`` where a ``Categorical`` object for list-like indexes input was returned, where a ``Series`` was expected. (:issue:`14580`)
617-
618-
619-
620606
- Bug in groupby operations with timedelta64 when passing ``numeric_only=False`` (:issue:`5724`)
621-
622-
623607
- Bug in ``DataFrame.to_html`` with ``index=False`` and ``max_rows`` raising in ``IndexError`` (:issue:`14998`)
624-
625608
- Bug in ``Categorical.searchsorted()`` where alphabetical instead of the provided categorical order was used (:issue:`14522`)
626-
627-
628-
629609
- Bug in ``resample``, where a non-string ```loffset`` argument would not be applied when resampling a timeseries (:issue:`13218`)
630-
631-
632-
633610
- Bug in ``.rank()`` which incorrectly ranks ordered categories (:issue:`15420`)
634611
- Bug in ``.corr()`` and ``.cov()`` where the column and index were the same object (:issue:`14617`)
635-
636-
637612
- Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`)
638613
- Bug in ``pd.pivot_table()`` where no error was raised when values argument was not in the columns (:issue:`14938`)
639-
640614
- Bug in ``.to_json()`` where ``lines=True`` and contents (keys or values) contain escaped characters (:issue:`15096`)
641615
- Bug in ``.to_json()`` causing single byte ascii characters to be expanded to four byte unicode (:issue:`15344`)
642616
- Bug in ``.read_json()`` for Python 2 where ``lines=True`` and contents contain non-ascii unicode characters (:issue:`15132`)
643617
- Bug in ``.rolling/expanding()`` functions where ``count()`` was not counting ``np.Inf``, nor handling ``object`` dtypes (:issue:`12541`)
644618
- Bug in ``.rolling()`` where ``pd.Timedelta`` or ``datetime.timedelta`` was not accepted as a ``window`` argument (:issue:`15440`)
645619
- Bug in ``DataFrame.resample().median()`` if duplicate column names are present (:issue:`14233`)
646-
647620
- Bug in ``DataFrame.groupby().describe()`` when grouping on ``Index`` containing tuples (:issue:`14848`)
648621
- Bug in creating a ``MultiIndex`` with tuples and not passing a list of names; this will now raise ``ValueError`` (:issue:`15110`)
649622
- Bug in ``groupby().nunique()`` with a datetimelike-grouper where bins counts were incorrect (:issue:`13453`)
650-
651623
- Bug in catching an overflow in ``Timestamp`` + ``Timedelta/Offset`` operations (:issue:`15126`)
652624
- Bug in the HTML display with with a ``MultiIndex`` and truncation (:issue:`14882`)
653-
654-
655625
- Bug in ``pd.merge_asof()`` where ``left_index``/``right_index`` together caused a failure when ``tolerance`` was specified (:issue:`15135`)
656-
657-
658-
659-
660-
661626
- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)
662627
- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)
663628
- Incorrect dtyped ``Series`` was returned by comparison methods (e.g., ``lt``, ``gt``, ...) against a constant for an empty ``DataFrame`` (:issue:`15077`)
664629
- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
665630
- Bug in ``DataFrame.fillna()`` where the argument ``downcast`` was ignored when fillna value was of type ``dict`` (:issue:`15277`)
666631
- Bug in ``.reset_index()`` when an all ``NaN`` level of a ``MultiIndex`` would fail (:issue:`6322`)
667-
668632
- Bug in ``pd.read_msgpack()`` when deserializing a ``CategoricalIndex`` (:issue:`15487`)
669633
- Bug in ``pd.DataFrame.to_records()`` which failed with unicode characters in column names (:issue:`11879`)
670-
671-
672634
- Bug in ``pd.read_csv()`` with ``float_precision='round_trip'`` which caused a segfault when a text entry is parsed (:issue:`15140`)
673-
674635
- Bug in ``DataFrame.to_stata()`` and ``StataWriter`` which produces incorrectly formatted files to be produced for some locales (:issue:`13856`)
675636
- Bug in ``pd.concat()`` in which concatting with an empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)
676637
- Bug in ``groupby.agg()`` incorrectly localizing timezone on ``datetime`` (:issue:`15426`, :issue:`10668`, :issue:`13046`)
677-
678-
679-
680638
- Bug in ``.read_csv()`` with ``parse_dates`` when multiline headers are specified (:issue:`15376`)
681639
- Bug in ``groupby.transform()`` that would coerce the resultant dtypes back to the original (:issue:`10972`, :issue:`11444`)
682-
683640
- Bug in ``DataFrame.hist`` where ``plt.tight_layout`` caused an ``AttributeError`` (use ``matplotlib >= 0.2.0``) (:issue:`9351`)
684641
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
685642
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
686643
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
687644
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
688645
- Bug in ``pd.read_msgpack`` which did not allow to load dataframe with an index of type ``CategoricalIndex`` (:issue:`15487`)
646+
- Bug in ``DataFrame.to_records()`` with converting datetime64 index with timezone (:issue: `13937`)

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,
@@ -1086,7 +1086,7 @@ def to_records(self, index=True, convert_datetime64=True):
10861086
y : recarray
10871087
"""
10881088
if index:
1089-
if is_datetime64_dtype(self.index) and convert_datetime64:
1089+
if is_datetime64_any_dtype(self.index) and convert_datetime64:
10901090
ix_vals = [self.index.to_pydatetime()]
10911091
else:
10921092
if isinstance(self.index, MultiIndex):

pandas/tests/frame/test_convert_to.py

+42-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
from __future__ import print_function
44

5-
from numpy import nan
65
import numpy as np
7-
8-
from pandas import compat
9-
from pandas import (DataFrame, Series, MultiIndex, Timestamp,
10-
date_range)
6+
from numpy import nan
117

128
import pandas.util.testing as tm
13-
9+
from pandas import DataFrame, MultiIndex, Series, Timestamp, compat, date_range
1410
from pandas.tests.frame.common import TestData
1511

1612

@@ -192,3 +188,43 @@ def test_to_records_with_unicode_column_names(self):
192188
"formats": ['<i8', '<f8']}
193189
)
194190
tm.assert_almost_equal(result, expected)
191+
192+
def test_to_records_with_tz(self):
193+
# GH13937
194+
date_range_tz_utc = date_range('2016-01-01', periods=10,
195+
freq='S', tz='UTC')
196+
date_range_tz_gmt = date_range('2016-01-01', periods=10,
197+
freq='S', tz='GMT')
198+
199+
df_utc = DataFrame({'datetime': date_range_tz_utc},
200+
index=date_range_tz_utc)
201+
df_gmt = DataFrame({'datetime': date_range_tz_gmt},
202+
index=date_range_tz_gmt)
203+
204+
df_utc_expected = df_utc.to_records()
205+
df_gmt_result = df_utc.tz_convert("GMT").to_records()
206+
207+
df_gmt_expected = df_gmt.to_records()
208+
df_utc_result = df_gmt.tz_convert("UTC").to_records()
209+
210+
# Check that it does not shows same time zone after conversion.
211+
tm.assertIsNot(df_utc_expected.index[0].tzinfo,
212+
df_gmt_result.index[0].tzinfo)
213+
214+
# Check df.to_records() are generated properly
215+
tm.assert_numpy_array_equal(df_utc_expected,
216+
df_gmt_result)
217+
218+
# Test DataFrame.to_records() with timezone conversion to UTC
219+
220+
expected = df_utc_expected['datetime']
221+
222+
result = df_gmt_result['datetime']
223+
224+
tm.assert_numpy_array_equal(expected, result)
225+
226+
tm.assertIsNot(df_gmt_expected.index[0].tzinfo,
227+
df_utc_result.index[0].tzinfo)
228+
229+
tm.assert_numpy_array_equal(df_utc_expected,
230+
df_gmt_result)

0 commit comments

Comments
 (0)