Skip to content

ENH: Add full tz aware support for IntervalIndex #18537

Closed
@jschendel

Description

@jschendel

Problem description

The fixes in #18424 allow for and IntervalIndex to be constructed with tz aware timestamps, but there are a few attributes/methods that still do not work properly with tz aware:

  • Disallow an IntervalIndex to contain elements with differing tz
  • IntervalIndex.mid returns tz naive
  • Anything using _get_next_label or _get_previous_label will raise
    • get_indexer when passed an IntervalIndex
    • get_loc for overlapping/non-monotonic IntervalIndex
    • contains always returns False for overlapping/non-monotonic due to Try/Except
    • etc.

Code Sample, a copy-pastable example if possible

Setup:

In [2]: start = pd.Timestamp('2017-01-01', tz='US/Eastern')
   ...: index = pd.interval_range(start, periods=3)
   ...: index
   ...:
Out[2]:
IntervalIndex([(2017-01-01, 2017-01-02], (2017-01-02, 2017-01-03], (2017-01-03, 2017-01-04]]
              closed='right',
              dtype='interval[datetime64[ns, US/Eastern]]')

IntervalIndex.mid returns tz naive:

In [3]: index.mid
Out[3]:
DatetimeIndex(['2017-01-01 17:00:00', '2017-01-02 17:00:00',
               '2017-01-03 17:00:00'],
              dtype='datetime64[ns]', freq=None)

get_indexer raises when passed an IntervalIndex:

In [4]: index.get_indexer(index[:-1])
---------------------------------------------------------------------------
TypeError: cannot determine next label for type <class 'pandas.core.indexes.datetimes.DatetimeIndex'>

Differing tz:

In [5]: left = pd.date_range('2017-01-01', periods=3, tz='US/Eastern')
   ...: right = pd.date_range('2017-01-02', periods=3, tz='US/Pacific')
   ...: index = pd.IntervalIndex.from_arrays(left, right)
   ...: index
   ...:
Out[5]:
IntervalIndex([(2017-01-01, 2017-01-02], (2017-01-02, 2017-01-03], (2017-01-03, 2017-01-04]]
              closed='right',
              dtype='interval[datetime64[ns, US/Eastern]]')

In [6]: index.left.dtype
Out[6]: datetime64[ns, US/Eastern]

In [7]: index.right.dtype
Out[7]: datetime64[ns, US/Pacific]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions