Skip to content

BUG: Using DatetimeIndex.date with timezone returns incorrect date #2… #21281

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

Merged
merged 10 commits into from
Jun 7, 2018
Merged
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Indexing
- Bug in :meth:`Series.reset_index` where appropriate error was not raised with an invalid level name (:issue:`20925`)
- Bug in :func:`interval_range` when ``start``/``periods`` or ``end``/``periods`` are specified with float ``start`` or ``end`` (:issue:`21161`)
- Bug in :meth:`MultiIndex.set_names` where error raised for a ``MultiIndex`` with ``nlevels == 1`` (:issue:`21149`)
-
- Bug in :attr:`DatetimeIndex.date` where an incorrect date is returned when the input date has a timezone (:issue:`21230`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be non-UTC timezone


I/O
^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,7 +2040,11 @@ def date(self):
Returns numpy array of python datetime.date objects (namely, the date
part of Timestamps without timezone information).
"""
return libts.ints_to_pydatetime(self.normalize().asi8, box="date")
if (self.tz is None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prob need
if self.tz is not None and self.tz is not utc
also write it more like

# add a comment here about tz conversions
if self.tz is not None and self.tz is not utc:
    stamps = self.asi8
else:
    stamps = self._local_timestamps()
return libts.init_to_pytdatetime(stamps, box="date")

return libts.ints_to_pydatetime(self.normalize().asi8, box="date")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the .normalize() calls here? I think ints_to_pydatetime will strip the time information away when constructing the date

else:
return libts.ints_to_pydatetime(
self.normalize()._local_timestamps(), box="date")

def normalize(self):
"""
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ def test_roundtrip_pickle_with_tz(self):
unpickled = tm.round_trip_pickle(index)
tm.assert_index_equal(index, unpickled)

@pytest.mark.parametrize("test_input", [
DatetimeIndex(['2013-01-24 15:01:00']),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, you could leave the DatetimeIndex construction in the test, and parametrize over the dtypes and pass that into the constructor.

DatetimeIndex(['2013-01-24 15:01:00'], dtype='datetime64[ns, CET]'),
DatetimeIndex(['2013-01-24 15:01:00'], dtype='datetime64[ns, EST]'),
DatetimeIndex(['2013-01-24 15:01:00'], dtype='datetime64[ns, UTC]')
])
def test_date_accessor(self, test_input):
# GH 21230
from datetime import date
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is imported at the top already


assert test_input.date == np.array(date(2013, 1, 24))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use tm.assert_numpy_array_equal here.


def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
# GH7774
index = date_range('20130101', periods=3, tz='US/Eastern')
Expand Down