Skip to content

.append drops MultiIndex, .join not supported for MultiIndex #201

Closed
@rsamson

Description

@rsamson

You are probably already aware, but .append and .join are not yet fully functional with MultiIndex.

  1. .append turns the index into numbers

s1=s[:4]
s2=s[4:]
s1
bar one 0.229167564165
two -1.12529475186
baz one 1.03310069555
two -0.859600695252
s2
foo one 1.75545035115
two -1.44073899127
qux one -0.239883854241
two -1.22027857654
s1.append(s2)
0 0.229167564165
1 -1.12529475186
...
6 -0.239883854241
7 -1.22027857654

df1=df[:4]
df2=df[4:]
df1
A B C
bar one -1.165 0.6214 -0.4865
two -0.8845 -0.3048 -0.1469
baz one 1.533 -0.7665 1.133
two -0.2581 2.067 -0.4839

df2
A B C
foo one -1.321 -0.08075 -1.135
two -0.3081 0.8832 0.09399
qux one -0.7685 0.8034 -1.032
two 0.3256 1.158 0.163

df1.append(df2)
A B C
0 -1.165 0.6214 -0.4865
1 -0.8845 -0.3048 -0.1469
...
6 -0.7685 0.8034 -1.032
7 0.3256 1.158 0.163

  1. .join errors out

df1t=df1.T
df2t=df2.T
df1t
bar baz
one two one two
A -1.165 -0.8845 1.533 -0.2581
B 0.6214 -0.3048 -0.7665 2.067
C -0.4865 -0.1469 1.133 -0.4839

df2t
foo qux
one two one two
A -1.321 -0.3081 -0.7685 0.3256
B -0.08075 0.8832 0.8034 1.158
C -1.135 0.09399 -1.032 0.163

df1t.join(df2t)
...
IndexError: list index out of range

  1. both .join and .append are fine when you are lining the data up with MultiIndex, rather than creating an expanded MultiIndex.

    dfa
    A B
    bar one 0.5341 0.5299
    two -0.3412 0.8686
    baz one 0.6352 1.835
    two -0.4022 -0.2686

dfb
C D
bar one -1.066 -1.029
two -0.8317 -0.9012
baz one -0.4223 0.9805
two -1.348 1.204

dfa.join(dfb)
A B C D
bar one 0.5341 0.5299 -1.066 -1.029
two -0.3412 0.8686 -0.8317 -0.9012
baz one 0.6352 1.835 -0.4223 0.9805
two -0.4022 -0.2686 -1.348 1.204

(dfa.T.append(dfb.T)).T
A B C D
bar one 0.5341 0.5299 -1.066 -1.029
two -0.3412 0.8686 -0.8317 -0.9012
baz one 0.6352 1.835 -0.4223 0.9805
two -0.4022 -0.2686 -1.348 1.204

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions