Pandas: Test Case for to_csv to sys.stdout

Created on 20 Jun 2018  路  5Comments  路  Source: pandas-dev/pandas

Code Sample, a copy-pastable example if possible

#! /usr/bin/env python3

import sys
import pandas as pd
import numpy as np

data = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
                    columns=['a', 'b', 'c', 'd', 'e'])

print('--------- Output ----------')
data.to_csv(sys.stdout, index=False)
print('---------------------------')

Problem description

No output to sys.stdout.

Expected Output

+++++++++ Output ++++++++++
a,b,c,d,e
8,7,1,3,4
6,7,0,1,2
8,0,9,4,1
1,1,9,0,4
8,1,2,9,2
+++++++++++++++++++++++++++

Output with 0.23.1

+++++++++ Output ++++++++++
+++++++++++++++++++++++++++

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-128-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.1
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.5
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.4
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.5
lxml: 4.2.1
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

IO Data Testing good first issue

Most helpful comment

The problem is here https://github.com/pandas-dev/pandas/blame/1a23779f09abc6ebf908d66ee88b973b767e2e3c/pandas/io/formats/csvs.py#L137 and has been introduced with the PR #21300 .

Note that if you execute your code, a file named <stdout>is created in the current directory due to the following portion of code

        elif hasattr(self.path_or_buf, 'name'):
            # path_or_buf is file handle
            path_or_buf = self.path_or_buf.name

Basically sys.stdout.nameis the string<stdout> and at the end the library creates a file with that name and use it, instead of writing the csv on the already opened file

All 5 comments

The problem is here https://github.com/pandas-dev/pandas/blame/1a23779f09abc6ebf908d66ee88b973b767e2e3c/pandas/io/formats/csvs.py#L137 and has been introduced with the PR #21300 .

Note that if you execute your code, a file named <stdout>is created in the current directory due to the following portion of code

        elif hasattr(self.path_or_buf, 'name'):
            # path_or_buf is file handle
            path_or_buf = self.path_or_buf.name

Basically sys.stdout.nameis the string<stdout> and at the end the library creates a file with that name and use it, instead of writing the csv on the already opened file

Hi believe this has been fixed. If you checkout master it should work.

@r00ta
```
I wondered where that '' file was coming from.

This is resolved on master as a result of #21478. However will keep open as I didn't explicitly see a test case for this issue, so PR to add that is certainly welcome

@WillAyd In which file shall this test case be added?
Also, would it be fine if we use a self defined array without using np.random ?

Was this page helpful?
0 / 5 - 0 ratings