question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why is pd.Index.union not commutative?

See original GitHub issue

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')

Problem description

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 pd.concat. For example:

>>> df1 = df1 = pd.DataFrame([[1,2,3],[1,2,3]], index=pd.PeriodIndex(['19910905', '19910906'], freq='D'))
>>> df2 = pd.DataFrame()
>>> pd.concat([df1, df2], axis=1, keys=['a', 'b'])
ValueError: can only call with other PeriodIndex-ed objects
>>> pd.concat([df2, df1], axis=1, keys=['a', 'b'])
Works!

Additionally (and perhaps this should be raised as a separate issue) should the specific implementation of pd.PeriodIndex._assert_can_do_setop not raise if the other index is empty? Since pd.Index([]).union(<instance of pd.PeriodIndex>) results in an instance of pd.PeriodIndex.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
TomAugspurgercommented, Nov 9, 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 notifications@github.com 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 https://github.com/pandas-dev/pandas/issues/23525#issuecomment-437234514, or mute the thread https://github.com/notifications/unsubscribe-auth/ABQHIu9_YxMmMlL0xNVPSc523ZESwKX1ks5utPCKgaJpZM4YPp62 .

1reaction
jrebackcommented, Nov 6, 2018

we don’t ignore empties. actually these should all convert to object dtype (and work).

Read more comments on GitHub >

github_iconTop Results From Across the Web

pandas.Index.union — pandas 1.5.2 documentation
Form the union of two Index objects. If the Index objects are incompatible, both Index objects will be cast to dtype('object') first. Changed...
Read more >
python - What are the 'levels', 'keys', and names arguments for ...
We can see that the resulting index is the union of indices and the resulting columns are the extension of columns from d1...
Read more >
CHAPTER 2 RING FUNDAMENTALS 2.1 Basic Definitions ...
An integral domain is a commutative ring with no zero divisors. ... the union is still a proper ideal, because the identity 1R...
Read more >
Commutative Law (Definition, Addition & Multiplication) - BYJU'S
Commutative law states that the change in position of two numbers while adding or multiplying them does not change the result. Learn its...
Read more >
Pullback (category theory) - Wikipedia
The pullback of two morphisms f and g need not exist, but if it does, it is essentially uniquely defined by the two...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found