I now have the simplest test script I can imagine, but it won’t work according to the XlsxWriter instructions:
import pandas as pd
import numpy as np
import xlsxwriter
writer = pd.ExcelWriter("Test.xlsx", engine = "xlsxwriter")
Full error:
Traceback (most recent call last):
File "<ipython-input-310-9c7e424c6d39>", line 1, in <module>
writer = pd.ExcelWriter("Test.xlsx", engine = "xlsxwriter")
File "/Users/ernie/anaconda3/lib/python3.6/site- packages/pandas/io/excel.py", line 1725, in __init__
self.book = xlsxwriter.Workbook(path, **engine_kwargs)
File "/Users/ernie/anaconda3/lib/python3.6/site-packages/xlsxwriter/workbook.py", line 68, in __init__
super(Workbook, self).__init__()
TypeError: super(type, obj): obj must be an instance or subtype of type
This error apparently had something to do with the session where Spyder and Python were running. I exited the session completely, re initiated it, and the error disappeared. Something somehow got corrupted, but very strange.
Related
I want to use objects to handle reading various types of input data. The final implementation will be much more complicated and basically the parent class will define some methods and just use read_func and the subclasses can handle the implementation of read_func, probably pointing to something like pd.read_excel or maybe adding on a few data cleaning steps. But I'm getting this odd error, here's a small reproducible example:
test.py:
import pandas as pd
class test:
read_func = pd.read_excel
print(pd.read_excel("test.xlsx")) # prints the excel fine
print(test().read_func) # prints <bound method read_excel of <__main__.test object at 0x104cebc70>>
print(test().read_func("test.xlsx")) # throws error
The error trace looks like this:
Traceback (most recent call last):
File "my/file/path/test.py", line 6, in <module>
test().read_func("test.xlsx")
File "/opt/homebrew/lib/python3.9/site-packages/pandas/util/_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "/opt/homebrew/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 457, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "/opt/homebrew/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 1376, in __init__
ext = inspect_excel_format(
File "/opt/homebrew/lib/python3.9/site-packages/pandas/io/excel/_base.py", line 1250, in inspect_excel_format
with get_handle(
File "/opt/homebrew/lib/python3.9/site-packages/pandas/io/common.py", line 670, in get_handle
ioargs = _get_filepath_or_buffer(
File "/opt/homebrew/lib/python3.9/site-packages/pandas/io/common.py", line 427, in _get_filepath_or_buffer
raise ValueError(msg)
ValueError: Invalid file path or buffer object type: <class '__main__.test'>
What am I doing wrong here?
Try this:
import pandas as pd
class test:
def __init__(self):
self.read_func = pd.read_excel
print(pd.read_excel("test.xlsx"))
print(test().read_func)
print(test().read_func("test.xlsx"))
I am trying to access an API using the ETL tool Alteryx, but receive the error:
TypeError: __init__() got an unexpected keyword argument 'encoding'
I am able to run the code in Python 2.7.16 just fine, but the version within Alteryx is 3.6.0. I had to change the JSONRPC file where xmlrpclib was to xmlrpc.client. I then got No module named 'httplib'. I had to change httplib to http.client. Next, I got the error Cannot import name 'HTTP'. I saw the line that had from httplib import HTTP, HTTPConnection and I removed HTTP. Lastly, I used a web 2to3 python converter for the jsonrpc file, and added in the line from urllib.parse import (splittype, splithost) in the jsonrpc package. I am now to the final two lines, but running into the encoding argument error.
I have tried to install msgpack, remove the encoding arguments in __init__.py, and change cls = encoding in __init__.py for json in CurrentDirectory\Lib\json.
import pprint
from jsonrpclib import jsonrpc
from datetime import datetime
Transaction_Code = datetime.now().strftime('%Y%m%d%H%M%S')
givex = jsonrpc.ServerProxy("host:port")
response = givex.dc_1026('en', Transaction_Code, 'ID', 'password', 'reportname','','')
pprint.pprint(response)
I expect the JSON output for the data, but I receive the following Traceback:
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\Alteryx\bin\Miniconda3\PythonTool_venv\lib\site-packages\jsonrpclib\jsonrpc.py", line 289, in call
return self.send(self.__name, args)
File "C:\Program Files\Alteryx\bin\Miniconda3\PythonTool_venv\lib\site-packages\jsonrpclib\jsonrpc.py", line 237, in _request
rpcid=rpcid, version=self.__version)
File "C:\Program Files\Alteryx\bin\Miniconda3\PythonTool_venv\lib\site-packages\jsonrpclib\jsonrpc.py", line 532, in dumps
return jdumps(request, encoding=encoding)
File "C:\Program Files\Alteryx\bin\Miniconda3\PythonTool_venv\lib\site-packages\jsonrpclib\jsonrpc.py", line 98, in jdumps
return json.dumps(obj, encoding=encoding)
File "C:\Program Files\Alteryx\bin\Miniconda3\lib\json__init.py", line 238, in dumps
"""
TypeError: init() got an unexpected keyword argument 'encoding'
With the help from #rickdenhaan I had to use jsonrpclib-pelix instead of jsonrpclib in Python 3.6
I am having trouble using pandas-datareader to import data from Quandl. Here is the code that I have tried (with a real API key):
import pandas_datareader.data as pdr
from datetime import date
start=date(1970,1,1)
end=date.today()
ticker='F'
qkey=[My API Key]
pdr.QUANDL_API_KEY=qkey
QUANDL_API_KEY=qkey
pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end)
pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end,api_key=qkey)
I get the following error messages when I run this:
>>> pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python37\lib\site-packages\pandas_datareader\data.py", line 379, in DataReader
session=session, api_key=access_key).read()
File "C:\Python37\lib\site-packages\pandas_datareader\quandl.py", line 54, in __init__
raise ValueError('The Quandl API key must be provided either '
ValueError: The Quandl API key must be provided either through the api_key variable or through the environmental variable QUANDL_API_KEY.
>>> pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end,api_key=qkey)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: DataReader() got an unexpected keyword argument 'api_key'
What am I missing? How do I provide the API key?
The source shows how the DataReader factory function passes it to the Quandl reader:
elif data_source == "quandl":
return QuandlReader(symbols=name, start=start, end=end,
retry_count=retry_count, pause=pause,
session=session, api_key=access_key).read()
So try passing it to DataReader with the access_key argument:
pdrquandl=pdr.DataReader('WIKI/'+ticker,'quandl',start,end,access_key=qkey)
A slightly different approach to quandl
import quandl
quandl.ApiConfig.api_key = "API-KEY"
aapl = quandl.get("WIKI/TICKER", start_date="YEAR-MONTH-DAY", end_date="YEAR-MONTH-DAY")
Configuration
MAC OS
VS Code
Python 3.x
Method
Run this code:
import pandas_datareader.data as pdr
api_key = [YOUR_API_KEY]
pdrquandl = pdr.DataReader("WIKI/TICKER", 'quandl', start_date, end_date, api_key)
I am having issues with getting basic function of the nptdms module working.
First, I am just trying to open a TDMS file and print the contents of specific channels within specific groups.
Using python 2.7 and the nptdms quick start here
Following this, I will be writing these specific pieces of data into a new TDMS file. Then, my ultimate goal is to be able to take a set of source files, open each, and write (append) to a new file. The source data files contain far more information that is needed, so I am breaking out the specifics into their own file.
The problem I have is that I cannot get past a basic error.
When running this code, I get:
Traceback (most recent call last):
File "PullTDMSdataIntoNewFile.py", line 27, in <module>
tdms_file = TdmsFile(r"C:\\Users\daniel.worts\Desktop\this_is_my_tdms_file.tdms","r")
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 94, in __init__
self._read_segments(f)
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 119, in _read_segments
object._initialise_data(memmap_dir=self.memmap_dir)
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 709, in _initialise_data
mode='w+b', prefix="nptdms_", dir=memmap_dir)
File "C:\Anaconda2\lib\tempfile.py", line 475, in NamedTemporaryFile
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
File "C:\Anaconda2\lib\tempfile.py", line 244, in _mkstemp_inner
fd = _os.open(file, flags, 0600)
OSError: [Errno 2] No such file or directory: 'r\\nptdms_yjfyam'
Here is my code:
from nptdms import TdmsFile
import numpy as np
import pandas as pd
#set Tdms file path
tdms_file = TdmsFile(r"C:\\Users\daniel.worts\Desktop\this_is_my_tdms_file.tdms","r")
# set variable for TDMS groups
group_nameone = '101'
group_nametwo = '752'
# set objects for TDMS channels
channel_dataone = tdms_file.object(group_nameone 'Payload_1')
channel_datatwo = tdms_file.object(group_nametwo, 'Payload_2')
# set data from channels
data_dataone = channel_dataone.data
data_datatwo = channel_datatwo.data
print data_dataone
print data_datatwo
Big thanks to anyone who may have encountered this before and can help point to what I am missing.
Best,
- Dan
edit:
Solved the read data issue by removing the 'r' argument from the file path.
Now I am having another error I can't trace when trying to write.
from nptdms import TdmsFile, TdmsWriter, RootObject, GroupObject, ChannelObject
import numpy as np
import pandas as pd
newfilepath = r"C:\\Users\daniel.worts\Desktop\Mined.tdms"
datetimegroup101_channel_object = ChannelObject('101', DateTime, data_datetimegroup101)
with TdmsWriter(newfilepath) as tdms_writer:
tdms_writer.write_segment([datetimegroup101_channel_object])
Returns error:
Traceback (most recent call last):
File "PullTDMSdataIntoNewFile.py", line 82, in <module>
tdms_writer.write_segment([datetimegroup101_channel_object])
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 68, in write_segment
segment = TdmsSegment(objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 88, in __init__
paths = set(obj.path for obj in objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 88, in <genexpr>
paths = set(obj.path for obj in objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 254, in path
self.channel.replace("'", "''"))
AttributeError: 'TdmsObject' object has no attribute 'replace'
I try to import my CA-10-60 file with this code:
import csv
with open('CA-10-60.csv', newline='') as f:
reader = csv.DictReader(f, delimiter='')
for row in reader:
print(row['Contract'], row['Serial'])
But I get this Error:
Traceback (most recent call last):
File "C:/Users/id984876/PycharmProjects/Search Engine SMC/flask/play.py", line 3, in <module>
reader = csv.DictReader(f, delimiter='')
AttributeError: module 'csv' has no attribute 'DictReader'
One issue you will definitely have is your delimiter needs to be a one character string as evidenced by what happens when I try and replicate your error -
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\caleb\AppData\Local\Programs\Python\Python36\lib\csv.py", line 87, in __init__
self.reader = reader(f, dialect, *args, **kwds)
TypeError: "delimiter" must be a 1-character string
Try fixing that and see if anything changes. As far as I can tell, the csv module is present in both 2.7.14 and 3.6.3 so unless you are running an older version I can't imagine any issues with the import.