Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Why is pd.Index.union not commutative? #23525
Code Sample, a copy-pastable example if possible
>>> ix = pd.Index([1,2]) >>> eix = pd.Index() >>> pi = pd.PeriodIndex(['19910905', '19910906'], freq='D') # Pair 1 >>> pi.union(eix) ValueError: can only call with other PeriodIndex-ed objects >>> eix.union(pi) PeriodIndex(['1991-09-05', '1991-09-06'], dtype='period[D]', freq='D') # Pair 2 >>> pi.union(ix) ValueError: can only call with other PeriodIndex-ed objects >>> ix.union(pi) Index([1, 2, 1991-09-05, 1991-09-06], dtype='object')
Conceptually I would imagine a union operation to be commutative. I was just wondering if there was an deliberate rationale behind not implementing pd.Index._assert_can_do_setop to only fail if the complementary self._assert_can_do_setop also fails.
This behavior also leads to some unexpected behaviors in
Additionally (and perhaps this should be raised as a separate issue) should the specific implementation of
@jreback - just to clarify - are you suggesting that:
>>> pd.Index().union(pd.period_range('19910905', periods=2)) Current: PeriodIndex(['1991-09-05', '1991-09-06'], dtype='period[D]', freq='D') Desired: Index([1991-09-05, 1991-09-06], dtype='object') >>> pd.Index().union(pd.interval_range(start=0, end=5)) Current: IntervalIndex([(0, 1], (1, 2]] closed='right', dtype='interval[int64]') Desired: Index([(0, 1], (1, 2]], dtype='object')
>>> pd.period_range('19910905', periods=2).union(pd.Index([1,2,3])) Current: ValueError Desired: Index([1991-09-05, 1991-09-06, 1, 2, 3], dtype='object')
2b) And should this only be the case for which the
referenced this issue
Nov 7, 2018
RangeIndex should always be just an optimization of Int64Index. So you would return a RangeIndex if possible, else an Int64Index.…
On Thu, Nov 8, 2018 at 9:03 PM ArtinSarraf ***@***.***> wrote: @jreback <https://github.com/jreback> - how should the union of Int64Index and RangeIndex behave ideally? Should they also result in an object dtype or behave the same as they do now? — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#23525 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ABQHIu9_YxMmMlL0xNVPSc523ZESwKX1ks5utPCKgaJpZM4YPp62> .