Skip to content

BUG: read_clipboard() on excel shifts values to the wrong column when there are nulls in the first column #41108

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
3 tasks done
saucoide opened this issue Apr 22, 2021 · 2 comments · Fixed by #41109
Closed
3 tasks done
Labels
Bug IO Data IO issues that don't fit into a more specific label
Milestone

Comments

@saucoide
Copy link
Contributor

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

copy this text, or data from excel/libreoffice with the same shape

col1	col2
1	red
	blue
2	green
In [3]: pandas.read_clipboard()
Out[3]: 
   col1   col2
0     1    red
1  blue   None
2     2  green

Problem description

Using read_clipboard() on excel data when there are null values on the first few rows of the first columns causes pandas to use the default separator instead of the "\t" used for excel.

The result is the values shift into the wrong column

Expected Output

In [4]: pandas.read_clipboard()
Out[4]: 
	col1	col2
0	1.0	red
1	NaN	blue
2	2.0	green

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2cb9652
python : 3.9.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.11-zen1-1-zen
Version : #1 ZEN SMP PREEMPT Tue, 30 Mar 2021 14:10:21 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.utf8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 20.3.3
setuptools : 49.2.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@saucoide saucoide added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 22, 2021
@asishm-wk
Copy link

asishm-wk commented Apr 23, 2021

pd.read_clipboard uses r'\s+' as the default sep that gets passed in to pd.read_csv

s = 'col1\tcol2\n1\tred\n\tblue\n2\tgreen'

pd.read_csv(StringIO(s), sep=r'\s+')
Out[16]:
   col1   col2
0     1    red
1  blue    NaN
2     2  green

reproduces, and should ideally be fixed there if it is a bug (i'll leave that for others to determine). Specifying sep='\t' provides your output for both read_clipboard and read_csv.

In [17]: pd.read_clipboard(sep='\t')
Out[17]:
   col1   col2
0   1.0    red
1   NaN   blue
2   2.0  green

@saucoide
Copy link
Contributor Author

saucoide commented Apr 23, 2021

yes this only happens if you let pandas infer the format, without specifying a separator, it is this part that does it:

    # Excel copies into clipboard with \t separation
    # inspect no more then the 10 first lines, if they
    # all contain an equal number (>0) of tabs, infer
    # that this came from excel and set 'sep' accordingly
    lines = text[:10000].split("\n")[:-1][:10]

    # Need to remove leading white space, since read_csv
    # accepts:
    #    a  b
    # 0  1  2
    # 1  3  4

    counts = {x.lstrip().count("\t") for x in lines}
    if len(lines) > 1 and len(counts) == 1 and counts.pop() != 0:
        sep = "\t"

I've tried to fix it and added a test here #41109

@jreback jreback added this to the 1.3 milestone Apr 26, 2021
@jreback jreback added IO Data IO issues that don't fit into a more specific label and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants