Why is pd.Index.union not commutative?
See original GitHub issueCode 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:
- Created 5 years ago
- Comments:9 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
we don’t ignore empties. actually these should all convert to object dtype (and work).