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
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,7 +2040,7 @@ 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")
return libts.ints_to_pydatetime(self.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.

I think this needs to be self._local_timestamps() instead of self.asi8 since those are essentially epoch (UTC) timestamps and we want to return local values.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. That might be it.


def normalize(self):
"""
Expand Down
15 changes: 15 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,21 @@ def test_roundtrip_pickle_with_tz(self):
unpickled = tm.round_trip_pickle(index)
tm.assert_index_equal(index, unpickled)

def test_date_accessor_with_tz(self):
Copy link
Member

Choose a reason for hiding this comment

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

Instead of 2 separate tests, you can have one test pytest.mark.parameterize over the timezone aware and naive data. (e.g. dtype='datetime64[ns, CET]'' and dtype=None)

# 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

index = DatetimeIndex(['2013-01-24 15:01:00+01:00'],
dtype='datetime64[ns, CET]', freq=None)

assert index.date == np.array(date(2013, 1, 24))

def test_date_accessor_without_tz(self):
# GH 21230
from datetime import date
index = DatetimeIndex(['2013-01-24 15:01:00+01:00'], freq=None)

assert index.date == np.array(date(2013, 1, 24))

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