Skip to content

.add gives incorrect result with MI Dataframe with mix of object and datetimes on index. #26558

Open
@andymcarter

Description

@andymcarter

Code Sample, a copy-pastable example if possible

import pandas as pd
import datetime

df1 = pd.DataFrame(
    data = [10],
    index = pd.MultiIndex.from_tuples([('Z', '2019-05-31')]),
    columns= ['A']
)

df2 = pd.DataFrame(
    data = [20],
    index = pd.MultiIndex.from_tuples([('Z', datetime.datetime(2019,5,31))]),
    columns= ['A']
)

df1.add(df2, fill_value=0)

Problem description

Two multiindexed dataframes, where the inner level is datetime-like string in the first, and actual datetime in the second.

Applying the .add function returns a dataframe with two index lines as expected (one for the string date, and the other for the datetime), but the values in the output do not match the two input frames.

df1.add(df2, fill_value=0)
                 A
Z 2019-05-31  20.0
  2019-05-31  20.0

Expected Output

df1.add(df2, fill_value=0)
                 A
Z 2019-05-31  10.0
  2019-05-31  20.0

OR

df1.add(df2, fill_value=0)
               A
Z 2019-05-31  30

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Darwin
OS-release: 16.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: en_GB.UTF-8

pandas: 0.24.2
pytest: None
pip: 18.1
setuptools: 40.4.3
Cython: None
numpy: 1.16.3
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.5.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2019.1
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.3
openpyxl: 2.6.2
xlrd: 1.2.0
xlwt: None
xlsxwriter: 1.1.8
lxml.etree: 4.3.3
bs4: 4.7.1
html5lib: None
sqlalchemy: 1.3.3
pymysql: 0.9.3
psycopg2: None
jinja2: 2.10.1
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions