Skip to content

tz-aware DatetimeIndex intersection raises AttributeError when values are the same but tz differs #24362

Closed
@TomAugspurger

Description

@TomAugspurger
In [25]: a = pd.date_range('2000', periods=4, tz="US/Central")

In [26]: a & a.tz_convert("US/Eastern")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-d8e6c109715c> in <module>
----> 1 a & a.tz_convert("US/Eastern")

~/sandbox/pandas/pandas/core/indexes/base.py in __and__(self, other)
   2221
   2222     def __and__(self, other):
-> 2223         return self.intersection(other)
   2224
   2225     def __or__(self, other):

~/sandbox/pandas/pandas/core/indexes/datetimes.py in intersection(self, other)
    575             result = Index.intersection(self, other)
    576             result = self._shallow_copy(result._values, name=result.name,
--> 577                                         tz=result.tz, freq=None)
    578             if result.freq is None:
    579                 result.freq = to_offset(result.inferred_freq)

AttributeError: 'Index' object has no attribute 'tz'

If the values differ, no error is raised

In [27]: a & a._time_shift(1).tz_convert("US/Eastern")
Out[27]:
DatetimeIndex(['2000-01-02 01:00:00-05:00', '2000-01-03 01:00:00-05:00',
               '2000-01-04 01:00:00-05:00'],
              dtype='datetime64[ns, US/Eastern]', freq='D')

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugTimezonesTimezone data dtypesetopsunion, intersection, difference, symmetric_difference

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions