Skip to content

implement _local_timestamps in DatetimeArrayMixin #21721

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 3 commits into from
Jul 5, 2018
Merged
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
22 changes: 21 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import warnings

import numpy as np
from pytz import utc

from pandas._libs import tslib
from pandas._libs.tslib import Timestamp, NaT, iNaT
from pandas._libs.tslibs import timezones
from pandas._libs.tslibs import conversion, timezones

from pandas.util._decorators import cache_readonly

Expand Down Expand Up @@ -110,6 +111,25 @@ def _sub_datelike_dti(self, other):
new_values[mask] = iNaT
return new_values.view('timedelta64[ns]')

# -----------------------------------------------------------------
# Timezone Conversion and Localization Methods

def _local_timestamps(self):
"""
Convert to an i8 (unix-like nanosecond timestamp) representation
while keeping the local timezone and not using UTC.
This is used to calculate time-of-day information as if the timestamps
were timezone-naive.
"""
values = self.asi8
indexer = values.argsort()
result = conversion.tz_convert(values.take(indexer), utc, self.tz)
Copy link
Member Author

Choose a reason for hiding this comment

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

Does this only make sense if tz_convert requires its input to be sorted? I don't think it does.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah not sure why that indexer is there

Copy link
Member Author

Choose a reason for hiding this comment

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

In the next pass I'll see if this can be simplified.


n = len(indexer)
reverse = np.empty(n, dtype=np.int_)
reverse.put(indexer, np.arange(n))
return result.take(reverse)

# ----------------------------------------------------------------
# Conversion Methods - Vectorized analogues of Timedelta methods

Expand Down
9 changes: 1 addition & 8 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,7 @@ def _local_timestamps(self):
if self.is_monotonic:
return conversion.tz_convert(self.asi8, utc, self.tz)
else:
values = self.asi8
indexer = values.argsort()
result = conversion.tz_convert(values.take(indexer), utc, self.tz)

n = len(indexer)
reverse = np.empty(n, dtype=np.int_)
reverse.put(indexer, np.arange(n))
return result.take(reverse)
return DatetimeArrayMixin._local_timestamps(self)

@classmethod
def _simple_new(cls, values, name=None, freq=None, tz=None,
Expand Down