Skip to content

ENH: Change DatetimeIndex.time to also return timezone info #21865

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion doc/source/timeseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ There are several time/date properties that one can access from ``Timestamp`` or
microsecond,"The microseconds of the datetime"
nanosecond,"The nanoseconds of the datetime"
date,"Returns datetime.date (does not contain timezone information)"
time,"Returns datetime.time (does not contain timezone information)"
time,"Returns datetime.time (contains timezone information)"
dayofyear,"The ordinal day of year"
weekofyear,"The week ordinal of the year"
week,"The week ordinal of the year"
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Other Enhancements
- :meth:`Series.nlargest`, :meth:`Series.nsmallest`, :meth:`DataFrame.nlargest`, and :meth:`DataFrame.nsmallest` now accept the value ``"all"`` for the ``keep`` argument. This keeps all ties for the nth largest/smallest value (:issue:`16818`)
- :class:`IntervalIndex` has gained the :meth:`~IntervalIndex.set_closed` method to change the existing ``closed`` value (:issue:`21670`)
- :func:`~DataFrame.to_csv` and :func:`~DataFrame.to_json` now support ``compression='infer'`` to infer compression based on filename (:issue:`15008`)
-
- Changed DatetimeIndex.time to also return timezone information. (:issue:`21358)
Copy link
Member

Choose a reason for hiding this comment

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

Missing a closing backtick for the issue number. Can you also add an :attr: ref for DatetimeIndex.time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does that look good?


.. _whatsnew_0240.api_breaking:

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ cdef inline object create_time_from_ts(
int64_t value, pandas_datetimestruct dts,
object tz, object freq):
""" convenience routine to construct a datetime.time from its parts """
return time(dts.hour, dts.min, dts.sec, dts.us)
return time(dts.hour, dts.min, dts.sec, dts.us, tz)


def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None,
Expand Down
10 changes: 1 addition & 9 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,7 @@ def time(self):
"""
Returns numpy array of datetime.time. The time part of the Timestamps.
Copy link
Member

Choose a reason for hiding this comment

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

Could you expand this docstring to also mention that the datetime.time objects are in local time with associated timezone information.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does that look good?

Copy link
Member

Choose a reason for hiding this comment

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

Small nit: could you replace , and contains with with. Otherwise LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem

"""
# If the Timestamps have a timezone that is not UTC,
# convert them into their i8 representation while
# keeping their timezone and not using UTC
if self.tz is not None and self.tz is not utc:
timestamps = self._local_timestamps()
else:
timestamps = self.asi8

return tslib.ints_to_pydatetime(timestamps, box="time")
return tslib.ints_to_pydatetime(self.asi8, self.tz, box="time")

@property
def date(self):
Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,15 +718,16 @@ def test_date_accessor(self, dtype):

tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize("dtype", [
None, 'datetime64[ns, CET]',
'datetime64[ns, EST]', 'datetime64[ns, UTC]'
@pytest.mark.parametrize("tz", [
None, pytz.timezone('CET'), pytz.timezone('EST'),
pytz.timezone('UTC')
])
def test_time_accessor(self, dtype):
def test_time_accessor(self, tz):
# Regression test for GH#21267
expected = np.array([time(10, 20, 30), pd.NaT])
# Changed test to account for GH#21358
expected = np.array([time(10, 20, 30, tzinfo=tz), pd.NaT])

index = DatetimeIndex(['2018-06-04 10:20:30', pd.NaT], dtype=dtype)
index = DatetimeIndex(['2018-06-04 10:20:30', pd.NaT], tz=tz)
result = index.time

tm.assert_numpy_array_equal(result, expected)
Expand Down