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.asi8, box="date")
else:
return libts.ints_to_pydatetime(
self._local_timestamps(), box="date")

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

@pytest.mark.parametrize("dtype", [
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to go in test_timezones (same subdir)

Copy link
Contributor

Choose a reason for hiding this comment

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

place it near other tests that look at .date

None, 'datetime64[ns, CET]',
'datetime64[ns, EST]', 'datetime64[ns, UTC]'
])
def test_date_accessor(self, dtype):
# GH 21230

index = DatetimeIndex(['2013-01-24 15:01:00'], dtype=dtype)
tm.assert_numpy_array_equal(index.date,
Copy link
Contributor

Choose a reason for hiding this comment

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

use

result = 
expected = 

Copy link
Contributor

Choose a reason for hiding this comment

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

can you add another date that is not ambiguous (e.g. its on the same date regardless of tz)
can you add a NaT as well

np.array([date(2013, 1, 24)], ndmin=1))

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