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

BUG: error in concatting Series with numpy scalar / tuple names #21015

Closed
jorisvandenbossche opened this Issue May 12, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@jorisvandenbossche
Member

jorisvandenbossche commented May 12, 2018

In [2]: s1 = pd.Series({'a': 1.5}, name=np.int64(190))

In [3]: s2 = pd.Series([], name=(43, 0))

In [4]: pd.concat([s1, s2])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-5f754290d56c> in <module>()
----> 1 pd.concat([s1, s2])

~/scipy/pandas/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    224                        verify_integrity=verify_integrity,
    225                        copy=copy, sort=sort)
--> 226     return op.get_result()
    227 
    228 

~/scipy/pandas/pandas/core/reshape/concat.py in get_result(self)
    385             # stack blocks
    386             if self.axis == 0:
--> 387                 name = com._consensus_name_attr(self.objs)
    388 
    389                 mgr = self.objs[0]._data.concat([x._data for x in self.objs],

~/scipy/pandas/pandas/core/common.py in _consensus_name_attr(objs)
     56     name = objs[0].name
     57     for obj in objs[1:]:
---> 58         if obj.name != name:
     59             return None
     60     return name

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

It looks maybe a bit of a strange corner case, but you can get both cases rather easily (the one from selecting from Int64Index axis (not default RangeIndex), the other from MultiIndex axis), for example:

In [15]: df1 = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'], index=[0, 1])

In [16]: df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['c', 'd'], index=pd.MultiIndex.from_tuples([(0, 0), (1, 1)]))

In [17]: pd.concat([df1.iloc[0], df2.iloc[0]])
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The reason for the error is that if you compare a tuple and a numpy scalar, it gives an array as result:

In [18]: (0, 0) == np.int64(0)
Out[18]: array([ True,  True], dtype=bool)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment