-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix default encoding for CSVFormatter.save #17821
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
Merged
TomAugspurger
merged 6 commits into
pandas-dev:master
from
Licht-T:fix-invalid-encoding-to_csv
Oct 11, 2017
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cfb5e16
BUG: Fix default encoding for CSVFormatter.save
Licht-T a632835
TST: Add to_csv defualt encoding test
Licht-T 0cd7bf7
DOC: Add comments on to_csv defualt encoding test
Licht-T 08df290
DOC: added release note
TomAugspurger 039f2cf
DOC: Add the fixing to_csv default encoding to whatsnew note
Licht-T c222ee8
Revert "DOC: Add the fixing to_csv default encoding to whatsnew note"
Licht-T File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from pandas import DataFrame | ||
import numpy as np | ||
import pandas as pd | ||
|
@@ -6,6 +8,21 @@ | |
|
||
class TestToCSV(object): | ||
|
||
def test_to_csv_defualt_encoding(self): | ||
# GH17097 | ||
df = DataFrame({'col': [u"AAAAA", u"ÄÄÄÄÄ", u"ßßßßß", u"聞聞聞聞聞"]}) | ||
|
||
with tm.ensure_clean('test.csv') as path: | ||
# the default to_csv encoding in Python 2 is ascii, and that in | ||
# Python 3 is uft-8. | ||
if pd.compat.PY2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment about why we're raising an error and what should be done to avoid this error (i.e. the |
||
# the encoding argument parameter should be utf-8 | ||
with tm.assert_raises_regex(UnicodeEncodeError, 'ascii'): | ||
df.to_csv(path) | ||
else: | ||
df.to_csv(path) | ||
tm.assert_frame_equal(pd.read_csv(path, index_col=0), df) | ||
|
||
def test_to_csv_quotechar(self): | ||
df = DataFrame({'col': [1, 2]}) | ||
expected = """\ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is overkill, but you can do
encoding = self.encoding or ('ascii' if compat.PY2 else 'utf-8')
to condense this whole block of if-else logic.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, matter of taste here. I prefer the block-logic because its easier to read I find.