Home > Back-end >  Dataframe printed to terminal without print() command
Dataframe printed to terminal without print() command

Time:11-05

I have written some code to download stockprices using Python.

All Tickers (140 , e.g. BMW.DE) have no problems.

I'm having issues with 'CON.DE'. It won't save to a .csv file and more peculiar it prints the dataframe to the terminal without there being a print() command.

I'm running it using the following versions:

  • yfinance 0.1.77
  • pandas 1.5.0
  • python 3.10.8
import pandas as pd
import yfinance as yf
import os

curdir = os.getcwd()


def getPrice(ticker):
    df = yf.Ticker(ticker).history(period ='1mo')[['Open', 'High', 'Low', 'Close', 'Volume']] #Download pricing
    outputFile = os.path.join(curdir, 'Output', ticker  '.csv') #define path for output file
    df.to_csv(outputFile) #export as .csv

getPrice('CON.DE') #Doesn't save df to .csv and prints df in terminal
getPrice('BMW.DE') #Saves df to .csv without issue

CodePudding user response:

You're actually running into an odd issue with Window's special CON(note it's case insensitive) filename which basically is redirecting to the console. This same code works fine in the WSL(linux) or non-windows.

The issue is in the filename. df.to_csv("con") produces this behavior so as you start stripping away portions of your outputFile when ticket == "CON.DE", you'll notice that breaking up the CON part "fixes" this wonky behavior you're seeing.

So this should fix your issue:

outputFile = os.path.join(curdir, 'Output', ticker.replace(".", "")  '.csv') #define path for output file

^ the above will strip out the . so that the filename here would be "conde.csv" which works fine. You could experiment with some other filenames that fit you better like ticker.replace(".", "-") -> "CON-DE.csv" would work fine as well since Windows interpets . in a filename a little different than most other characters

Some links that may be more helpful to explain CON:

  • Related