passes columns are not ALL present dataframe
Package:
pandas
30911

Exception Class:
KeyError
Raise code
else:
self.styler = None
self.df = df
if cols is not None:
# all missing, raise
if not len(Index(cols).intersection(df.columns)):
raise KeyError("passes columns are not ALL present dataframe")
if len(Index(cols).intersection(df.columns)) != len(set(cols)):
# Deprecated in GH#17295, enforced in 1.0.0
raise KeyError("Not all names specified in 'columns' are found")
self.df = df.reindex(columns=cols)
Links to the raise (1)
https://github.com/pandas-dev/pandas/blob/b3e335254f46a526ee3ce9bb757eac4011d9d1fe/pandas/io/formats/excel.py#L490Ways to fix
When writing a pandas data frame to excel file make sure the passed columns exist in the dataframe.
Steps to reproduce the error:
- Setup and installation
$ pip install --user pipenv $ mkdir test_folder $ cd test_folder $ pipenv shell $ pipenv install pandas
- Run test code
import pandas as pd
import os
import tempfile
df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],index=['row 1', 'row 2'],columns=['col 1', 'col 2'])
with tempfile.TemporaryDirectory() as tmpdir:
model_path = os.path.join(tmpdir,"output.xlsx")
df1.to_excel(model_path,columns=["col1","col2"])
# Here col1 and col2 doens't exist in df1. df1's columns are "col 1" and "col 2"
Fixed version of the code
import pandas as pd
import os
import tempfile
df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],index=['row 1', 'row 2'],columns=['col 1', 'col 2'])
with tempfile.TemporaryDirectory() as tmpdir:
model_path = os.path.join(tmpdir,"output.xlsx")
df1.to_excel(model_path,columns=["col 1","col 2"])
Add a possible fix
Please authorize to post fix