Skip to content

DatetimeIndex.union with tz-aware and tz-naive raises TypeError #21671

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
mroeschke opened this issue Jun 29, 2018 · 5 comments · Fixed by #27144
Closed

DatetimeIndex.union with tz-aware and tz-naive raises TypeError #21671

mroeschke opened this issue Jun 29, 2018 · 5 comments · Fixed by #27144
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Timezones Timezone data dtype
Milestone

Comments

@mroeschke
Copy link
Member

xref #11351

In [28]: s = pd.Series([pd.Timestamp('2011-01-01'), pd.NaT])

In [29]: stz = pd.Series(pd.DatetimeIndex(['2012-01-01', '2012-01-02'], tz='Asia/Tokyo'))

In [30]: pd.Index(s).union(pd.Index(stz))

TypeError: Cannot join tz-naive with tz-aware DatetimeIndex

In [32]: pd.__version__
Out[32]: '0.24.0.dev0+192.g0b63e81.dirty'

Problem description

Per #11351 (comment), this operation should just convert the data to object and not raise a TypeError.

Expected

In [33]: pd.Index(s).append(pd.Index(stz))
Out[33]:
Index([2011-01-01 00:00:00, NaT, 2012-01-01 00:00:00+09:00,
       2012-01-02 00:00:00+09:00],
      dtype='object')
@gfyoung
Copy link
Member

gfyoung commented Jul 4, 2018

Don't forget though that the union will do object comparison at the element-level, where that timezone incompatibility still exists so "casting to object" will issue warnings like this:

Traceback (most recent call last):
  File "pandas/_libs/tslibs/timestamps.pyx", line 171, in pandas._libs.tslibs.timestamps._Timestamp.__richcmp__
  File "pandas/_libs/tslibs/timestamps.pyx", line 231, in pandas._libs.tslibs.timestamps._Timestamp._assert_tzawareness_compat
TypeError: Cannot compare tz-naive and tz-aware timestamps
Exception ignored in: 'pandas._libs.lib.array_equivalent_object'
Traceback (most recent call last):
  File "pandas/_libs/tslibs/timestamps.pyx", line 171, in pandas._libs.tslibs.timestamps._Timestamp.__richcmp__
  File "pandas/_libs/tslibs/timestamps.pyx", line 231, in pandas._libs.tslibs.timestamps._Timestamp._assert_tzawareness_compat
TypeError: Cannot compare tz-naive and tz-aware timestamps
sys:1: RuntimeWarning: Cannot compare tz-naive and tz-aware timestamps, sort order is undefined for incomparable objects
Index([2011-01-01 00:00:00, NaT, 2012-01-01 00:00:00+09:00,
       2012-01-02 00:00:00+09:00],
      dtype='object')

Do we just want to swallow those or suppress them? I don't want to overhaul __richcmp__ just for this (corner) case admittedly.

Here is the current attempt:

forking-repos@392bd53

@gfyoung
Copy link
Member

gfyoung commented Jul 4, 2018

Actually, are we even able to redirect the Cython stderr without doing some kind of dance?

See this SO post here.

@gfyoung
Copy link
Member

gfyoung commented Jul 5, 2018

@jreback : Thoughts?

@jbrockmendel
Copy link
Member

@mroeschke this appears to work in master. closeable?

@mroeschke
Copy link
Member Author

Probably could use a unit test. I don't think we have one for this use case.

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Jun 18, 2019
@jreback jreback added this to the 0.25.0 milestone Jul 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions Timezones Timezone data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants