pandas


Two unaligned Pandas Series: concat raises error, adding does not but it returns a weird answer


Note: I am working with a rather old-ish Pandas 0.16.2, in Python 2.7.11.
My simplistic conceptual model for the adding of two Series was that it would involve an index-matching step that is similar to what goes on in a pd.concat(..., axis=1), ie. the Series indexes are lined up and then the values are added.
Therefore (modulo NaN handling I guess), I would expect u+v to work if, and only if concat([u, v], axis=1) works.
In the example below I build two Series with 'unalignable' indexes. My confusion is that concat does raise an error (as expected) but the adding does not -- and even more confusing is that the result of adding comes back with everything duplicated.
First I create a couple of series which have equal indexes (containing duplicates):
import string, pandas as pd
# Create a series with an index that has duplicates
u = pd.Series(range(5), index=list(string.ascii_lowercase)[:5])
u = pd.concat([u, u])
# Create another, same index but values reversed
v = pd.Series(range(5)[::-1], index=list(string.ascii_lowercase)[:5])
v = pd.concat([v, v])
Here they are:
In [2]: u
Out[2]:
a 0
b 1
c 2
d 3
e 4
a 0
b 1
c 2
d 3
e 4
dtype: int64
In [3]: v
Out[3]:
a 4
b 3
c 2
d 1
e 0
a 4
b 3
c 2
d 1
e 0
dtype: int64
They can be added since the indices are equal:
In [4]: u+v
Out[4]:
a 4
b 4
c 4
d 4
e 4
a 4
b 4
c 4
d 4
e 4
dtype: int64
If we sort v then its index gets reordered and since there is no longer an obvious way to line up v with u any more it is not surprising that concat raises an error:
In [5]: v.sort()
In [6]: v
Out[6]:
e 0
e 0
d 1
d 1
c 2
c 2
b 3
b 3
a 4
a 4
dtype: int64
In [7]: pd.concat([u, v], axis=1)
....
ValueError: cannot reindex from a duplicate axis
However, adding still works but bizarrely returns a longer series:
In [8]: u+v
Out[8]:
a 4
a 4
a 4
a 4
b 4
b 4
b 4
b 4
c 4
c 4
c 4
c 4
d 4
d 4
d 4
d 4
e 4
e 4
e 4
e 4
dtype: int64
What happened here?

Related Links

Plotting separate plots for each decile in time series data
how to convert pandas dataframe to libsvm format?
Merging two dataframes based on a date between two other dates without a common column
Why I can't change the series format?
Copy numpy array into Panda multiindex (same size)
Average Previous and Current Row - Pandas
seaborn pointplot above swarmplot
Edit field and append value to a python dataframe column
column_stack returns non cotiguous array
pandas: conditionally select a row cell for each column based on a mask
pandas custom function apply on melted dataframe
How to check for boolean codition in pandas dataframe
Reading batches of data from BigQuery into Datalab
Jupyter/ipywidgets sorting dataframe on two levels
Groupby.sum() giving ValueError: overflow in timedelta operation
Why does DataFrameGroupBy.boxplot method throw error when given argument “subplots=True/False”?

Categories

HOME
compiler-construction
clips
listview
deezer
heroku
plone
relative-path
iot
relayjs
cmd
ip
angular-ui-bootstrap
indesign
webrequest
contact
fancybox-3
izpack
dtrace
telephony
tostring
lucene.net
amazonsellercentral
paging
tokenize
orleans
sox
crystal-reports-2010
ping
bpel
bluestacks
frame
preg-match-all
jspresso
code-contracts
subset-sum
gammu
rotational-matrices
wpf-controls
jmonkeyengine
dosbox
jquery-bootgrid
dynamics-crm-2013
android-browser
unspecified
email-templates
mapzen
grid.mvc
dynamic-reports
google-closure
arena-simulation
midl
mime
unixodbc
slick-3.0
qcombobox
apple-news
wso2carbon
user-accounts
osx-mavericks
alphabet
knockout-components
pg-dump
libusb-win32
windows-iot-core-10
capacity
vtigercrm
crosswalk-runtime
nested-sets
wdf
static-ip-address
android-fonts
websitepanel
angstrom-linux
suffix-tree
nessus
ios4
independentsoft
jms2
google-places
pundit
deis
jsapi
website-monitoring
gwidgets
dd
qcodo
titanium-modules
ceil
typeof
meteor-velocity
srs
intentservice
mysqltuner
tidy
sitemesh
inbox
quartz-graphics
easy-install
buster.js
eventlistener
robotics-studio
anonymous-methods
libstdc++
cisco-jtapi
gdata-api
dsn
objective-c-2.0
osql
nsmanagedobject
infobox
hamachi
oncheckedchanged
createwindow
.nettiers
gin
nintendo-ds
virtual-functions
ubuntu-9.04
.net-1.0

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App