Pandas ExcelWriter yields AttributeError - python

While trying to create an xlsx file with pandas, I receive the following error:
Traceback (most recent call last):
File "<ipython-input-24-201aac2da411>", line 1, in <module>
writer = pd.ExcelWriter('test_file2.xlsx', engine='xlsxwriter', options={'constant_memory': True})
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel.py", line 1945, in __init__
self.book = xlsxwriter.Workbook(path, **engine_kwargs)
AttributeError: module 'xlsxwriter' has no attribute 'Workbook'
My code :
import pandas pd
writer = pd.ExcelWriter('test_file.xlsx', engine='xlsxwriter', options={'constant_memory': True})
Versions
Python 3.7.3
Pandas 0.24.2

I resolved the issue. Previously, I had downloaded the xlsxwriter package and added it to my python pathmanager in Spyder. This conflicted with pandas. Deleting the path and xlsxwriter resolved the issue.

Related

Creating and using Excel with multiple sheets in Python

I am creating an Excel file with two sheets from 2 different data frames. Here is a sample code:
import pandas as pd
import openpyxl
import xlsxwriter
with pd.ExcelWriter('output.xlsx',engine='xlsxwriter') as wr:
df1.to_excel(wr, sheet_name='Final',index=False)
df2.to_excel(wr, sheet_name='Tie_Line_Data',index=False)
When I run it in any Python IDE, it runs perfectly. However, when I run it in Terminal, it gives the following error:
Traceback (most recent call last):
File "/home/memiit/public_html/Pooja_MemiIT_Server/weather_demand_data_automated_mail.py", line 382, in <module>
with pd.ExcelWriter('output.xlsx',engine='xlsxwriter') as wr:
File "/home/memiit/public_html/python3/lib/python3.6/site-packages/pandas/io/excel/_xlsxwriter.py", line 187, in __init__
self.book = xlsxwriter.Workbook(path, **engine_kwargs)
AttributeError: module 'xlsxwriter' has no attribute 'Workbook'
How do I solve this, or is there any alternative way of doing it.

Trouble opening old pickle file

I am trying to load an old pickle file containing the airline dataset ( https://arxiv.org/abs/1611.06740 ) . The pickle is very old and I have problems accessing it. If I try:
objects = []
with (open("airline.pickle", "rb")) as openfile:
while True:
try:
objects.append(pickle.load(openfile))
except EOFError:
break
I get the following warning and error:
FutureWarning: pandas.core.index is deprecated and will be removed in a future version. The public classes are available in the top-level namespace.
objects.append(pickle.load(openfile))
Traceback (most recent call last):
File "c:\Users\LocalAdmin\surfdrive\Code\Python\Airline\pickleToCSV.py", line 9, in <module>
objects.append(pickle.load(openfile))
TypeError: _reconstruct: First argument must be a sub-type of ndarray
Trying with pandas does not work:
File "C:\Users\LocalAdmin\surfdrive\Code\Python\Airline\Airline\lib\site-packages\pandas\io\pickle.py", line 203, in read_pickle
return pickle.load(handles.handle) # type: ignore[arg-type]
TypeError: _reconstruct: First argument must be a sub-type of ndarray
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\LocalAdmin\surfdrive\Code\Python\Airline\pickleToCSV.py", line 7, in <module>
df = pd.read_pickle('airline.pickle')
File "C:\Users\LocalAdmin\surfdrive\Code\Python\Airline\Airline\lib\site-packages\pandas\io\pickle.py", line 208, in read_pickle
return pc.load(handles.handle, encoding=None)
File "C:\Users\LocalAdmin\surfdrive\Code\Python\Airline\Airline\lib\site-packages\pandas\compat\pickle_compat.py",
line 249, in load
return up.load()
File "C:\Users\LocalAdmin\AppData\Local\Programs\Python\Python39\lib\pickle.py", line 1212, in load
dispatch[key[0]](self)
File "C:\Users\LocalAdmin\AppData\Local\Programs\Python\Python39\lib\pickle.py", line 1725, in load_build
for k, v in state.items():
AttributeError: 'tuple' object has no attribute 'items'
How can I access the file and save it to csv? I need the data that is contained there. I am using pandas 1.2.4 and python 3.6.
The syntax should be simpler than in your example
with open("airline.pickle", "rb") as f:
objects = pickle.load(f)
If this fails, then I would look at the pickle documentation which covers some of the optional parameters that are useful for decoding pickle files created by python2.
As mentioned in a previous answer, the error TypeError: _reconstruct: First argument must be a sub-type of ndarray is due to a change from pandas version 0.14 to 0.15 (Source). The documentation said that pd.read_pickle would be able to load such old pickle files, but this is not working on recent versions. If you install an older version, I tested 0.17.1 which can be obtained in pypi or conda-forge, it can load that pickle file successfully.
If you are using conda, the following should work:
conda create -n old_pandas -c conda-forge pandas=0.17.* python=3.*
conda activate old_pandas
And then, in a Python prompt,
import pandas as pd
dataset = pd.read_pickle("airline.pickle")

Export Python Dataframe to Excel

I'm trying to export a Python Dataframe to excel using xlsx or csv...
Here is the code I tried to use:
export_word_count = word_count.to_excel (r'C:\Users\OTR\PycharmProjects\MyProjects\word_count.xlsx', index = None, header=True)
I keep getting the following error messages:
Traceback (most recent call last):
File "C:/Users/OTR/PycharmProjects/MyProjects/CAP_Test_MotsCles.py", line 35,
in <module>
export_word_count = word_count.to_excel
(r'C:\Users\OTR\PycharmProjects\MyProjects\word_count_CAP.xlsx', index = None,
header=True)
File "C:\Users\OTR\PycharmProjects\MyProjects\venv\lib\site-
packages\pandas\core\generic.py", line 2127, in to_excel
engine=engine)
File "C:\Users\OTR\PycharmProjects\MyProjects\venv\lib\site-packages\pandas\io\formats\excel.py", line 656, in write
writer = ExcelWriter(_stringify_path(writer), engine=engine)
File "C:\Users\OTR\PycharmProjects\MyProjects\venv\lib\site-packages\pandas\io\excel.py", line 1204, in __init__
from openpyxl.workbook import Workbook
ModuleNotFoundError: No module named 'openpyxl'
Any output on this would be greatly appreciated. I tried to tweek the code, but still wouldn't export. Thank you.
EDIT:
Managed to export, but having issues with full data export
Sample Python Data:
products 58
company 53
cannabis 42
business 39
You dont have python openpyxl module installed.
Install it with:
pip install openpyxl
Your words are your index. Right now you are not exporting the index.
Try changing your code to:
word_count.to_excel (r'C:\Users\OTR\PycharmProjects\MyProjects\word_count.xlsx', index =True, header=True)
‘index=True’ is the default behavior, so not actually necessary.

Workbook object has no attribute 'add_sheet' when using Workbook object from xlsxwriter

Not only am i fairly new to python, but this is my first post on this forum. I am learning how to integrate python and excel. I was able to get the following code:
import numpy as np
import pandas as pd
import xlrd, xlwt
import xlsxwriter
path = "C:/Users/Python/data/"
data = np.arange(1, 101).reshape((10,10))
wb = xlsxwriter.Workbook(path + 'workbook.xlsx')
ws_1 = wb.add_sheet('first_sheet')
ws_2 = wb.add_sheet('second_sheet')
for c in range(data.shape[0]):
for r in range(data.shape[1]):
ws_1.write(r, c, data[c, r])
ws_2.write(r, c, data[c, r])
wb.close()
to work on Jupyter Notebook and through the anaconda python shell, however when i run in Spyder, i get the following error message on the ipython console:
runfile('C:/Users/Python/excel_integration1.py',
wdir='C:/Users/Python') Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/Python/excel_integration1.py', wdir='C:/Users/Python')
File
"C:\Users\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 866, in runfile
execfile(filename, namespace)
File
"C:\Users\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py",
line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/Python/excel_integration1.py", line 7, in
ws_1 = wb.add_sheet('first_sheet')
AttributeError: 'Workbook' object has no attribute 'add_sheet'
I look forward to all your help.
The method name in xlsxwriter, as shown in the xlsxwriter documentation, is add_worksheet. You're using add_sheet. I suspect you may have read examples from xlwt or a different library, because in xlwt you'd have
>>> import xlwt
>>> wb = xlwt.Workbook()
>>> wb.add_sheet("some name")
<xlwt.Worksheet.Worksheet object at 0x7f6633b466d8>
but with xlsxwriter you have
>>> import xlsxwriter
>>> wb = xlsxwriter.Workbook()
>>> wb.add_sheet("won't work")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Workbook' object has no attribute 'add_sheet'
>>> wb.add_worksheet("will work")
<xlsxwriter.worksheet.Worksheet object at 0x7f6632e70320>

splitting pandas dataframe - np.array_split error

I'm trying to split dataframe, this code used to work just fine:
split_dfs = np.array_split(big_df,8)
now it gives me error (i did a system update in between):
Traceback (most recent call last):
File "./prepare_fixations_dataset.py", line 127, in <module>
split_dfs = np.array_split(big_df,8)
File "/usr/lib/python2.7/site-packages/numpy/lib/shape_base.py", line 426, in array_split
if sub_arys[-1].size == 0 and sub_arys[-1].ndim != 1:
File "/usr/lib/python2.7/site-packages/pandas-0.15.1-py2.7-linux-x86_64.egg/pandas/core/generic.py", line 1936, in __getattr__
(type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'size'
if you have any siggestions why it may not work now please let me know.
the error is caused by the bug in numpy 1.9.0 or regression and incompatibility between numpy 1.9.0 and pandas 0.15.1.this bug doesn't happen with numpy 1.8.1 and pandas 0.15.1.
i filed a bug in the pandas github:
https://github.com/pydata/pandas/issues/8846
seems it is already fixed for pandas 0.15.2

Categories

Resources