Skip to content

Test Case for to_csv to sys.stdout #21561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hawkeyej opened this issue Jun 20, 2018 · 5 comments · Fixed by #21572
Closed

Test Case for to_csv to sys.stdout #21561

hawkeyej opened this issue Jun 20, 2018 · 5 comments · Fixed by #21572
Labels
good first issue IO Data IO issues that don't fit into a more specific label Testing pandas testing functions or related to the test suite
Milestone

Comments

@hawkeyej
Copy link

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

@r00ta
Copy link
Contributor

r00ta commented Jun 20, 2018

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

@minggli
Copy link
Contributor

minggli commented Jun 20, 2018

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

@hawkeyej
Copy link
Author

hawkeyej commented Jun 20, 2018

@r00ta

I wondered where that '<stdout>' file was coming from.

@WillAyd
Copy link
Member

WillAyd commented Jun 20, 2018

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 WillAyd added Testing pandas testing functions or related to the test suite IO Data IO issues that don't fit into a more specific label good first issue labels Jun 20, 2018
@WillAyd WillAyd changed the title to_csv producing no output to sys.stdout in 0.23.1, 0.23.0 works fine Test Case for to_csv to sys.stdout Jun 21, 2018
@uds5501
Copy link
Contributor

uds5501 commented Jun 21, 2018

@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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue IO Data IO issues that don't fit into a more specific label Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants