Pycharm linter flags output of concat as "type", not DataFrame
See original GitHub issueMinimum display example
df1 = pd.DataFrame({'A': ['A0']}, index=[0])
df2 = pd.DataFrame({'A': ['A4']}, index=[4])
result = pd.concat([df1, df2])
print(df1.loc[4]) # Pycharm doesn't flag
print(result.loc[4]) # Pycharm flags as "Unresolved attribute reference '.loc' for class 'type'"
# But runs fine anyway
Problem description
Pycharm’s linter believes pd.concat as returning class “type”, when it normally returns a dataframe.
I suspect that this is due to core.reshape.concat._Concatenator.get_result() using core.dtypes.concat._get_frame_result_type which sometimes returns SparseDataFrame (a type) and sometimes an object (normally a Dataframe?).
Extract from pandas.core.dtypes.concat
def _get_frame_result_type(result, objs):
"""
return appropriate class of DataFrame-like concat
if any block is SparseBlock, return SparseDataFrame
otherwise, return 1st obj
"""
if any(b.is_sparse for b in result.blocks):
from pandas.core.sparse.api import SparseDataFrame
return SparseDataFrame
else:
return objs[0]
Expected Output
The MDE should not throw an error in Pycharm.
Output of pd.show_versions()
pandas: 0.20.3 pytest: None pip: 9.0.1 setuptools: 28.8.0 Cython: None numpy: 1.13.1 scipy: None xarray: None IPython: None sphinx: None patsy: None dateutil: 2.6.1 pytz: 2017.2 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: None openpyxl: 2.4.8 xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: 4.6.0 html5lib: None sqlalchemy: None pymysql: None psycopg2: None jinja2: None s3fs: None pandas_gbq: None pandas_datareader: None None
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top GitHub Comments
@gfyoung I’ve been suppressing the warnings in my code using type hints instead.
foo = pd.concat([bar1, bar2]) # type: pd.DataFrame
0.23.0