Skip to content

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

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
TomAugspurger opened this issue Dec 19, 2018 · 4 comments · Fixed by #45357
Labels
Bug setops union, intersection, difference, symmetric_difference Timezones Timezone data dtype
Milestone

Comments

@TomAugspurger
Copy link
Contributor

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')
@TomAugspurger TomAugspurger added the Timezones Timezone data dtype label Dec 19, 2018
@cbertinato
Copy link
Contributor

cbertinato commented Dec 27, 2018

It looks like the issue here is that, initially, the DTIs are not equal nor are their dtypes because of the different timezones, so they both converted to Index.

if not is_dtype_equal(self.dtype, other.dtype):

This actually starts in the intersection method for DTI, but gets dropped into the Index intersection method when it is determined that they're not equal.

Is the expected behavior that, in this case of the intersection, the DTIs are considered equal if the underlying indices are equal, regardless of timezone?

If so, then we can make a change to the equality check in the DTI intersection method.

The other option I see is to return an Index rather than a DTI in the case that they're equal and avoid or modify the offending shallow copy.

@jbrockmendel
Copy link
Member

for other intersection-like operations with mixed timezones we convert to UTC (in particular im thinking of NDFrame._align_frame). would that be a better behavior than returning object-dtype?

@jbrockmendel
Copy link
Member

or even convert other to self's tz might make more sense. when i patch this to just return object dtype:

>>> a & a.tz_convert("US/Eastern")
Index([2000-01-01 00:00:00-06:00, 2000-01-02 00:00:00-06:00,
       2000-01-03 00:00:00-06:00, 2000-01-04 00:00:00-06:00],
      dtype='object')

i.e. we get an object index of Timestamps that are all the same tz

@jbrockmendel jbrockmendel added the setops union, intersection, difference, symmetric_difference label Jun 17, 2021
@mroeschke mroeschke added the Bug label Jun 25, 2021
@jbrockmendel
Copy link
Member

This now returns an object-dtype Index. The analogous union also returns object dtype, but has a deprecation saying it will be UTC in a future version. We probably should do the same for all setops right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug setops union, intersection, difference, symmetric_difference Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants