Skip to content
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

pandas.DataFrame.reindex_like throws if called from a derived class instance #31925

Closed
lguyot opened this issue Feb 12, 2020 · 7 comments · Fixed by #32633
Closed

pandas.DataFrame.reindex_like throws if called from a derived class instance #31925

lguyot opened this issue Feb 12, 2020 · 7 comments · Fixed by #32633
Labels
Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@lguyot
Copy link

lguyot commented Feb 12, 2020

Code Sample, a copy-pastable example if possible

import pandas as pd
import pandas.testing as pdt

class MyDataFrame(pd.DataFrame):
    pass

mdf = MyDataFrame()
df = pd.DataFrame()
# In DataFrame.reindex_like, the derived class is compared to
# the base class. The following line will throw
# 'AssertionError: <class 'pandas.core.frame.DataFrame'>'
mdf.reindex_like(df)

# In pdt.assert_frame_equal,
# check_like=True triggers a call to DataFrame.reindex_like.
# The following line will throw the same
# 'AssertionError: <class 'pandas.core.frame.DataFrame'>'
pdt.assert_frame_equal(mdf, df, check_like=True)

Problem description

The method reindex_like throws an AssertionError when the caller is an instance of a derived class of pandas.DataFrame.

Expected Output

No exception thrown.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-65-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 9.0.1
setuptools : 39.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 0.999999999
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@jorisvandenbossche jorisvandenbossche added the Regression Functionality that used to work in a prior pandas version label Feb 12, 2020
@jorisvandenbossche jorisvandenbossche added this to the 1.0.2 milestone Feb 12, 2020
@jorisvandenbossche
Copy link
Member

@topper-123 another one from the _ensure_type checking

@jbrockmendel
Copy link
Member

@jorisvandenbossche sounds like there has been some discussion about ensure_type already. is someone on top of this?

@TomAugspurger
Copy link
Contributor

sounds like there has been some discussion about ensure_type already

I think Joris was referring to #31785 / #31788.

Don't think anyone is working on this yet (unless @topper-123 has something locally?)

ericchansen added a commit to ericchansen/pandas that referenced this issue Mar 3, 2020
…1925)

Commit pandas-dev/pandas@6fd326d in
pull request pandas-dev#30613 added `_ensure_type`, which
utilizes `isinstance`. However, it is reasonable to assume that someone
may want to create a DataFrame subclass. Therefore, `_ensure_type`
should use `issubclass`.
@ericchansen
Copy link

Isn't this as simple as using issubclass instead of isinstance?

def _ensure_type(self: T, obj) -> T:
"""
Ensure that an object has same type as self.
Used by type checkers.
"""
assert isinstance(obj, type(self)), type(obj)
return obj

@jorisvandenbossche
Copy link
Member

@ericchansen do you want to make a PR with that change? (and a test)

I would personally also be fine with just getting rid of _ensure_type, the type checking should not have runtime consequences

@TomAugspurger
Copy link
Contributor

the type checking should not have runtime consequences

Agreed.

@ericchansen
Copy link

@jorisvandenbossche I'll open the PR today once I'm done with the tests.

@TomAugspurger @jorisvandenbossche It looks as if the type checking has something to do with mypy (as per discussion here #30613 (review) and here #30565). I agree it shouldn't have runtime consequences, but it does look like there was some discussion about why they @topper-123 @jreback did it this way.

ericchansen added a commit to ericchansen/pandas that referenced this issue Mar 3, 2020
ericchansen added a commit to ericchansen/pandas that referenced this issue Mar 3, 2020
…1925)

Commit pandas-dev/pandas@6fd326d in
pull request pandas-dev#30613 added `_ensure_type`, which
utilizes `isinstance`. However, it is reasonable to assume that someone
may want to create a DataFrame subclass. Therefore, `_ensure_type`
should use `issubclass`.
ericchansen added a commit to ericchansen/pandas that referenced this issue Mar 3, 2020
mgeplf pushed a commit to BlueBrain/voxcell that referenced this issue Mar 1, 2022
Several changes are induced by those of
https://pandas.pydata.org/pandas-docs/version/1.0.0/whatsnew/v1.0.0.html#removal-of-prior-version-deprecations-changes

* DataFrame.ix needs to be replaced by DataFrame.iloc for positional indexing, see
    https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike

* Uses of DataFrame.reindex() when indexing with a list
  with missing labels, see
  https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike

* Replaces Series.get_values() by Series.to_numpy(), see
  https://pandas.pydata.org/pandas-docs/version/0.25.3/reference/api/pandas.Series.get_values.html

* In unit tests only: explicit casts of all instances of DataFramProxy
to pandas.DataFrame before comparison in order to circumvent an issue with
pandas.DataFrame.reindex_like method, see pandas-dev/pandas#31925

Change-Id: I110ab055720db91010ed04f991aff0863945fbd9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version
Projects
None yet
5 participants