Skip to content

Add timetz attribute to DatetimeIndex #22132

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
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4265cff
ENH: Added timetz attribute to DatetimeIndex. GH21358
jequinon Jul 28, 2018
6e9ce28
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Jul 28, 2018
2966176
DOC: Changed method, class, and api.rst doc to add timetz to Datetime…
jequinon Jul 28, 2018
c972de7
TST: Added a test for DatetimeIndex.timetz. GH21358
jequinon Jul 28, 2018
4473e7c
Fixed style errors
jequinon Jul 29, 2018
d51e0c1
Fixed spacing on test
jequinon Jul 30, 2018
1dd2c78
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Jul 30, 2018
b4af058
Added whatsnew entry
jequinon Jul 30, 2018
028b05d
Added attribute tag to whatsnew
jequinon Jul 30, 2018
5185552
Fixed another spacing mistake
jequinon Jul 30, 2018
8878613
Updated docs and changed test to use tz_naive_fixture
jequinon Jul 31, 2018
368165b
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Jul 31, 2018
baf8883
Added test comparing Timestamp.tz with datetime.tz
jequinon Jul 31, 2018
9227e35
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Jul 31, 2018
f94cb13
Bump because build is failing unexpectedly
jequinon Jul 31, 2018
18bb0b7
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Aug 2, 2018
659643c
ENH: Added timetz to Series.dt. Also added a test and changed docs
jequinon Aug 2, 2018
ec5a295
Made small change dt accessor test
jequinon Aug 2, 2018
17547ce
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Aug 2, 2018
e711f6c
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Aug 3, 2018
6cfa9a1
Merge remote-tracking branch 'upstream/master' into Add_timetz_to_Dat…
jequinon Aug 3, 2018
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
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,7 @@ Time/Date Components
DatetimeIndex.nanosecond
DatetimeIndex.date
DatetimeIndex.time
Copy link
Contributor

Choose a reason for hiding this comment

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

can add in the api section for Timestamp though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The timetz method was already there for Timestamp, and I added the property for DatetimeIndex

DatetimeIndex.timetz
DatetimeIndex.dayofyear
DatetimeIndex.weekofyear
DatetimeIndex.week
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ Other Enhancements
- :func:`~DataFrame.to_csv` and :func:`~DataFrame.to_json` now support ``compression='infer'`` to infer compression based on filename (:issue:`15008`)
- :func:`to_timedelta` now supports iso-formated timedelta strings (:issue:`21877`)
- :class:`Series` and :class:`DataFrame` now support :class:`Iterable` in constructor (:issue:`2193`)
- :class:`DatetimeIndex` gained :attr:`~DatetimeIndex.timetz` attribute. Returns time with 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.

  1. I don't think you need the ~ before ~DatetimeIndex.timetz
  2. Can you clarify that this returns local time with timezone information


.. _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, npy_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
8 changes: 8 additions & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,14 @@ def time(self):

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

@property
def timetz(self):
"""
Returns numpy array of datetime.time also containing timezone
information. The time part of the Timestamps.
"""
return tslib.ints_to_pydatetime(self.asi8, self.tz, box="time")

@property
def date(self):
"""
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class DatetimeIndex(DatetimeArrayMixin, DatelikeOps, TimelikeOps,
nanosecond
date
time
timetz
dayofyear
weekofyear
week
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/datetimes/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,18 @@ def test_time_accessor(self, dtype):

tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize('tz', [
dateutil.tz.gettz('US/Eastern'), dateutil.tz.gettz('US/Pacific'),
dateutil.tz.gettz('UTC')])
def test_timetz_accessor(self, tz):
Copy link
Member

Choose a reason for hiding this comment

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

Instead could you use the tz_naive_fixture?

def tz_naive_fixture(request):

# GH21358
expected = np.array([time(10, 20, 30, tzinfo=tz), pd.NaT])

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

tm.assert_numpy_array_equal(result, expected)

def test_dti_drop_dont_lose_tz(self):
# GH#2621
ind = date_range("2012-12-01", periods=10, tz="utc")
Expand Down