2

I am currently trying to set up an RMarkdown document that is almost completely python based. I have several running examples of this set up, but my latest work seems to be impossible to knitt eventhough the code runs smoothly when I run all chunks.

From the error message, I think it has something to do with me importing pandas-datareader, but I would not know why that is, as I do have the latest version installed and I have checked that I am using the right environment. The error seems to be related to the first couple of lines, where I import the package so I will spare you the whole document.

Please see the relevant first few code-chunks below:

---
title: "ABC"
runningheader: "ABC" # only for pdf output
subtitle: "ABC" # only for html output
output: html_document
author: "Jan Felix"
abstract: "An RMarkdown document with all the relevant python packages."
date: "`r format(Sys.time(), '%B %d, %Y')`"
---
library(reticulate)
library(knitr)
knitr::opts_chunk$set(message = FALSE, echo = FALSE, warning = FALSE, error = FALSE)
import numpy as np
import pandas as pd
pd.options.mode.chained_assignment = None 
import matplotlib.pyplot as plt
import pandas_datareader as pdr
import os
import datetime
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Users/janfe/Anaconda3/Library/plugins/platforms'
np.random.seed(seed=1994)
Quitting from lines 18-27 (Pyfi.Rmd) 
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ImportError: cannot import name 'StringIO'

Detailed traceback: 
  File "<string>", line 1, in <module>
  File "C:\Users\janfe\Documents\R\win-library\4.0\reticulate\python\rpytools\loader.py", line 24, in _import_hook
    level=level
  File "C:\Users\janfe\ANACON~1\lib\site-packages\pandas_datareader\__init__.py", line 2, in <module>
    from .data import (DataReader, Options, get_components_yahoo,
  File "C:\Users\janfe\Documents\R\win-library\4.0\reticulate\python\rpytools\loader.py", line 24, in _import_hook
    level=level
  File "C:\Users\janfe\ANACON~1\lib\site-packages\pandas_datareader\data.py", line 7, in <module>
    from pandas_datareader.av.forex import AVForexReader
  File "C:\Users\janfe\Documents\R\win-library\4.0\reticulate\python\rpytools\loader.py", line 24, in _import_hook
    level=level
  File "C:\Users\janfe\ANACON~1\lib\site-packages\pandas_datareader\av\__init__.py", line 3, in <module>
    from pandas_datareader.base import _BaseReader
  File "C:\Us
Calls: <Anonymous> ... py_capture_output -> force -> <Anonymous> -> py_call_impl

Any help greatly appreciated! Let me know if any further info would help.

EDIT: I just confirmed that this error does not happen if I comment out the following lines and load the data as CSV instead, like so:

#import pandas_datareader as pdr 
#fred = pdr.DataReader(symbols, 'fred', start, end)
#fred.to_csv("fred.csv")

fred = pd.read_csv("fred.csv", index_col = "DATE")
fred.index = pd.DatetimeIndex(fred.index)

This is obviously not an answer to my question, but it narrows down the possibilities. Calling pandas-datareader in a document to be knit does not seem to work...

Jan Felix
  • 117
  • 9

0 Answers0