diff --git a/doc/source/timeseries.rst b/doc/source/timeseries.rst index 9e01296d9c9c7..d5824ee2283ea 100644 --- a/doc/source/timeseries.rst +++ b/doc/source/timeseries.rst @@ -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" diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 034a56b2ac0cb..a67eef11649c2 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -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`) -- +- :attr:`DatetimeIndex.time` now also returns timezone information. (:issue:`21358`) .. _whatsnew_0240.api_breaking: diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 281e497945c5f..bdf7808d0bb5d 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -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, diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 0d1c5241c5a93..a6521b8e16ac8 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -576,16 +576,9 @@ def day_name(self, locale=None): def time(self): """ Returns numpy array of datetime.time. The time part of the Timestamps. + Time returned is in local time with associated timezone information. """ - # 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): diff --git a/pandas/tests/indexes/datetimes/test_timezones.py b/pandas/tests/indexes/datetimes/test_timezones.py index 3697d183d2fc6..6f5ed365c55af 100644 --- a/pandas/tests/indexes/datetimes/test_timezones.py +++ b/pandas/tests/indexes/datetimes/test_timezones.py @@ -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)