{"action":"created","issue":{"url":"https://api.github.com/repos/pandas-dev/pandas/issues/19827","repository_url":"https://api.github.com/repos/pandas-dev/pandas","labels_url":"https://api.github.com/repos/pandas-dev/pandas/issues/19827/labels{/name}","comments_url":"https://api.github.com/repos/pandas-dev/pandas/issues/19827/comments","events_url":"https://api.github.com/repos/pandas-dev/pandas/issues/19827/events","html_url":"https://github.com/pandas-dev/pandas/issues/19827","id":299133878,"number":19827,"title":"File mode in `to_csv` is ignored, when passing a file object instead of a path","user":{"login":"colobas","id":12519843,"avatar_url":"https://avatars3.githubusercontent.com/u/12519843?v=4","gravatar_id":"","url":"https://api.github.com/users/colobas","html_url":"https://github.com/colobas","followers_url":"https://api.github.com/users/colobas/followers","following_url":"https://api.github.com/users/colobas/following{/other_user}","gists_url":"https://api.github.com/users/colobas/gists{/gist_id}","starred_url":"https://api.github.com/users/colobas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colobas/subscriptions","organizations_url":"https://api.github.com/users/colobas/orgs","repos_url":"https://api.github.com/users/colobas/repos","events_url":"https://api.github.com/users/colobas/events{/privacy}","received_events_url":"https://api.github.com/users/colobas/received_events","type":"User","site_admin":false},"labels":[{"id":134699,"url":"https://api.github.com/repos/pandas-dev/pandas/labels/Docs","name":"Docs","color":"3465A4","default":false},{"id":47229171,"url":"https://api.github.com/repos/pandas-dev/pandas/labels/IO%20CSV","name":"IO CSV","color":"5319e7","default":false},{"id":717120670,"url":"https://api.github.com/repos/pandas-dev/pandas/labels/good%20first%20issue","name":"good first issue","color":"0e8a16","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/pandas-dev/pandas/milestones/32","html_url":"https://github.com/pandas-dev/pandas/milestone/32","labels_url":"https://api.github.com/repos/pandas-dev/pandas/milestones/32/labels","id":933188,"number":32,"title":"Next Major Release","description":"Changes that would be nice to have in the next release. These issues are not blocking. They will be pushed to the next release if no one has time to fix them.","creator":{"login":"jreback","id":953992,"avatar_url":"https://avatars2.githubusercontent.com/u/953992?v=4","gravatar_id":"","url":"https://api.github.com/users/jreback","html_url":"https://github.com/jreback","followers_url":"https://api.github.com/users/jreback/followers","following_url":"https://api.github.com/users/jreback/following{/other_user}","gists_url":"https://api.github.com/users/jreback/gists{/gist_id}","starred_url":"https://api.github.com/users/jreback/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jreback/subscriptions","organizations_url":"https://api.github.com/users/jreback/orgs","repos_url":"https://api.github.com/users/jreback/repos","events_url":"https://api.github.com/users/jreback/events{/privacy}","received_events_url":"https://api.github.com/users/jreback/received_events","type":"User","site_admin":false},"open_issues":1324,"closed_issues":258,"state":"open","created_at":"2015-01-13T10:53:19Z","updated_at":"2018-02-23T11:47:24Z","due_on":"2018-08-31T07:00:00Z","closed_at":null},"comments":10,"created_at":"2018-02-21T21:24:14Z","updated_at":"2018-02-23T14:55:38Z","closed_at":null,"author_association":"NONE","body":"#### Code Sample, a copy-pastable example if possible\r\n\r\n```\r\n>>> import pandas as pd\r\n>>> df = pd.read_csv(\"example.csv\")\r\n>>> df.head()\r\n   just  a  file\r\n0     1  2     3\r\n1     4  5     6\r\n2     7  8     9\r\n>>> with open(\"someother.csv\", \"wb\") as f:\r\n...     df.to_csv(f, mode=\"wb\")\r\n... \r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 2, in <module>\r\n  File \"/usr/lib/python3.6/site-packages/pandas/core/frame.py\", line 1524, in to_csv\r\n    formatter.save()\r\n  File \"/usr/lib/python3.6/site-packages/pandas/io/formats/format.py\", line 1652, in save\r\n    self._save()\r\n  File \"/usr/lib/python3.6/site-packages/pandas/io/formats/format.py\", line 1740, in _save\r\n    self._save_header()\r\n  File \"/usr/lib/python3.6/site-packages/pandas/io/formats/format.py\", line 1708, in _save_header\r\n    writer.writerow(encoded_labels)\r\nTypeError: a bytes-like object is required, not 'str'\r\n```\r\n#### Problem description\r\n\r\nWhen passing a file opened in binary mode to `df.to_csv` and also passing `mode='wb'`, this mode is ignored. I think it's because of these lines: https://github.com/pandas-dev/pandas/blob/master/pandas/io/common.py#L407-L411 and these ones: https://github.com/pandas-dev/pandas/blob/master/pandas/io/formats/format.py#L1660-L1662\r\n\r\nIt seems that `is_text` isn't passed, and so it assumes the default value of `True`\r\n\r\n#### Expected Output\r\nA file should just be written.\r\n\r\n#### Output of ``pd.show_versions()``\r\n\r\n<details>\r\n\r\nINSTALLED VERSIONS\r\n------------------\r\ncommit: None\r\npython: 3.6.4.final.0\r\npython-bits: 64\r\nOS: Linux\r\nOS-release: 4.14.20-1-lts\r\nmachine: x86_64\r\nprocessor: \r\nbyteorder: little\r\nLC_ALL: None\r\nLANG: en_US.UTF-8\r\nLOCALE: en_US.UTF-8\r\n\r\npandas: 0.22.0\r\npytest: 3.4.0\r\npip: 9.0.1\r\nsetuptools: 38.5.1\r\nCython: 0.27.3\r\nnumpy: 1.14.0\r\nscipy: 1.0.0\r\npyarrow: None\r\nxarray: None\r\nIPython: 6.2.1\r\nsphinx: 1.7.0\r\npatsy: 0.4.1\r\ndateutil: 2.6.1\r\npytz: 2018.3\r\nblosc: None\r\nbottleneck: None\r\ntables: None\r\nnumexpr: None\r\nfeather: None\r\nmatplotlib: 2.1.2\r\nopenpyxl: None\r\nxlrd: None\r\nxlwt: None\r\nxlsxwriter: None\r\nlxml: None\r\nbs4: 4.6.0\r\nhtml5lib: 1.0b10\r\nsqlalchemy: 1.2.3\r\npymysql: None\r\npsycopg2: None\r\njinja2: 2.10\r\ns3fs: 0.1.2\r\nfastparquet: 0.1.3\r\npandas_gbq: None\r\npandas_datareader: None\r\n\r\n</details>\r\n"},"comment":{"url":"https://api.github.com/repos/pandas-dev/pandas/issues/comments/368031514","html_url":"https://github.com/pandas-dev/pandas/issues/19827#issuecomment-368031514","issue_url":"https://api.github.com/repos/pandas-dev/pandas/issues/19827","id":368031514,"user":{"login":"TomAugspurger","id":1312546,"avatar_url":"https://avatars3.githubusercontent.com/u/1312546?v=4","gravatar_id":"","url":"https://api.github.com/users/TomAugspurger","html_url":"https://github.com/TomAugspurger","followers_url":"https://api.github.com/users/TomAugspurger/followers","following_url":"https://api.github.com/users/TomAugspurger/following{/other_user}","gists_url":"https://api.github.com/users/TomAugspurger/gists{/gist_id}","starred_url":"https://api.github.com/users/TomAugspurger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TomAugspurger/subscriptions","organizations_url":"https://api.github.com/users/TomAugspurger/orgs","repos_url":"https://api.github.com/users/TomAugspurger/repos","events_url":"https://api.github.com/users/TomAugspurger/events{/privacy}","received_events_url":"https://api.github.com/users/TomAugspurger/received_events","type":"User","site_admin":false},"created_at":"2018-02-23T14:55:37Z","updated_at":"2018-02-23T14:55:37Z","author_association":"CONTRIBUTOR","body":"I assume you meant `to_csv`. In that case the mode still matters for write\nvs. append vs. exclusive. It's just the text vs. binary part of `mode` that\nwould be \"ignored\".\n\nGiven that the current behavior is so unfriendly (raising a not great error\nmessage), is there a harm in ignoring the text vs. binary part?\n\nOn Fri, Feb 23, 2018 at 8:49 AM, Guilherme Pires <notifications@github.com>\nwrote:\n\n> @TomAugspurger <https://github.com/tomaugspurger> doesn't the mode\n> parameter in read_csv stop having a purpose in that case?\n>\n> —\n> You are receiving this because you were mentioned.\n> Reply to this email directly, view it on GitHub\n> <https://github.com/pandas-dev/pandas/issues/19827#issuecomment-368029599>,\n> or mute the thread\n> <https://github.com/notifications/unsubscribe-auth/ABQHIrphQeD3uruekW9zZpicyMhrfCm2ks5tXs_rgaJpZM4SOWoz>\n> .\n>\n"}}
{"actor":{"display_login":"TomAugspurger"}}