[Tutor] pandas.to_clipboard() copies nothing

Peter Otten __peter__ at web.de
Sat Jun 6 14:39:59 EDT 2020


Jim wrote:

> On 6/6/20 4:25 AM, Peter Otten wrote:
>> Jim wrote:
>> 
>>> Mint18.3, python3.6, pandas 0.24.1
>>>
>>>
>>> et_file = '/home/jfb/Downloads/PortfolioDownload.csv'
>>> df = pd.read_csv(et_file, header=6, usecols=[0, 7], skipfooter=6,
>>> engine='python')
>>> df.to_clipboard(sep=',')
>>> print(df)
>>>
>>> Manual notes the following. I'm not using PyQt4, but xclip is installed.
>>>
>>> Notes: Requirements for your platform.
>>>
>>>       Linux : xclip, or xsel (with PyQt4 modules)
>>>       Windows : none
>>>       OS X : none
>>>
>>> The print command prints what I expect to see.
>>>
>>> I don't use pandas that much so maybe I am missing something.
>> 
>> The pandas side seems OK; maybe you are looking at the wrong selection?
>> I have a slightly different setup, and the following works for me:
>> 
>> $ python3
>> Python 3.4.3 (default, Nov 12 2018, 22:25:49)
>> [GCC 4.8.4] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import pandas
>>>>> df = pandas.DataFrame([[1,2], [3, 4]], columns=["Ham", "Spam"])
>>>>> df.to_clipboard(sep=", ")
>>>>>
>> $ xclip -o -selection clipboard
>>     Ham  Spam
>> 0    1     2
>> 1    3     4$
>> 
> 
> Peter,
> 
> I'm not sure what you mean by the "wrong selection" above. I ran your
> code and got the following results.
> 
> 
> This version (space after ,) worked despite the error msg:
> 
> Python 3.6.10 (default, Dec 19 2019, 23:04:32)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import pandas
>  >>> df = pandas.DataFrame([[1,2,], [3,3]], columns=["Ham", "Spam"])
>  >>> df.to_clipboard(sep=", ")
> QApplication: invalid style override passed, ignoring it.
>      Available styles: Windows, Fusion
> /home/jfb/EVs/env36/lib/python3.6/site-packages/pandas/io/clipboards.py:134:
> UserWarning: to_clipboard in excel mode requires a single character
> separator.
>    warnings.warn('to_clipboard in excel mode requires a single '
> 
> Copied from clipboard:
>     Ham  Spam
> 0    1     2
> 1    3     3
> 
> This version (no space after ,) also worked despite a different error msg:
> 
> (env36) jfb at jims-mint18 ~ $ python
> Python 3.6.10 (default, Dec 19 2019, 23:04:32)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import pandas
>  >>> df = pandas.DataFrame([[1,2], [3,4]], columns=["Ham", "Spam"])
>  >>> df.to_clipboard(sep=",")
> QApplication: invalid style override passed, ignoring it.
>      Available styles: Windows, Fusion
> 
> Copied from clipboard:
> ,Ham,Spam
> 0,1,2
> 1,3,4
> 
> 
> I had a little test file I was using to figure out pandas so I dropped
> your code in it. Nothing was copied to the clipboard and this is the
> error msg:
> 
> (env36) jfb at jims-mint18 ~ $ python /home/jfb/Dev/Python/test_csv_file.py
> QApplication: invalid style override passed, ignoring it.
>      Available styles: Windows, Fusion
> /home/jfb/EVs/env36/lib/python3.6/site-packages/pandas/io/clipboards.py:134:
> UserWarning: to_clipboard in excel mode requires a single character
> separator.
>    warnings.warn('to_clipboard in excel mode requires a single '
> 
> So I added excel=False and dropped the sep= and got this error:
> 
> (env36) jfb at jims-mint18 ~ $ python /home/jfb/Dev/Python/test_csv_file.py
> QApplication: invalid style override passed, ignoring it.
>      Available styles: Windows, Fusion
> 
> I don't understand the QApplication reference. I assume it refers to QT
> I have some QT stuff installed:
> 
> from pip list:
> PyQt5                    5.12
> PyQt5-sip                4.19.14
> qtconsole                4.4.3
> 
> I don't know why QT would come into play here and I don't understand the
> reference to excel when it was not included.
> 
> To sum up. If I run it from the python console it give an error, but
> copies to the clipboard. If I run the python file from the terminal it
> gives an error and does not copy to the clipboard.

Most of your errors seem to be warnings.

Looking into the code writing to the clipboard shows that it requires the 
DISPLAY environment variable to be set, so I'd check that in the terminal.

https://github.com/pandas-dev/pandas/blob/master/pandas/io/clipboard/__init__.py#L539

shows that

    if HAS_DISPLAY:
        if _executable_exists("xsel"):
            return init_xsel_clipboard()
        if _executable_exists("xclip"):
            return init_xclip_clipboard()
        if _executable_exists("klipper") and _executable_exists("qdbus"):
            return init_klipper_clipboard()
        ... [try qt] 

QT is the last option. From that we can conclude that xclip is not found.
Maybe you can fix that by adjusting the path. 
Or you try to set the clipboard mechanism explicitly with

pandas.io.clipboard.set_clipboard("xclip")




More information about the Tutor mailing list