-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Add support of 'decimal' option to Series.to_csv and Dataframe.to_csv #8448
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
Changes from 4 commits
0fbb0bf
c0985ec
4261ea5
e410065
f129b0c
f9a3e45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1161,7 +1161,7 @@ def _try_cast(self, element): | |
except: # pragma: no cover | ||
return element | ||
|
||
def to_native_types(self, slicer=None, na_rep='', float_format=None, | ||
def to_native_types(self, slicer=None, na_rep='', float_format=None, decimal='.', | ||
**kwargs): | ||
""" convert to our native types format, slicing if desired """ | ||
|
||
|
@@ -1171,10 +1171,22 @@ def to_native_types(self, slicer=None, na_rep='', float_format=None, | |
values = np.array(values, dtype=object) | ||
mask = isnull(values) | ||
values[mask] = na_rep | ||
if float_format: | ||
|
||
|
||
if float_format and decimal != '.': | ||
formater = lambda v : (float_format % v).replace('.',decimal,1) | ||
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. you actually want to create a new formatter, but NOT in the lambda, e.g.
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. I don't understand you point. If the float_format specified is something like '%.3f' just replacing the '.' by a ',' will lead to an uncorrect formater. 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. if you construt the integer/decimal portion separatly then you can do it (you might have to 'parse' the formatter a bit), somethign like nvm. your original is prob ok (though will be slow) 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. I would suggest to keep this first version as such and I will submit another pull request if I'm able to find a better solution (ideally we should also handle the case where the float_format is a class accepting the '%' operator). The slowness is real but will be close to what people are currently doing in (iterating over the final result to replace all '.' by ',') |
||
elif decimal != '.': | ||
formater = lambda v : ('%g' % v).replace('.',decimal,1) | ||
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. Use |
||
elif float_format: | ||
formater = lambda v : float_format % v | ||
else: | ||
formater = None | ||
|
||
if formater: | ||
imask = (~mask).ravel() | ||
values.flat[imask] = np.array( | ||
[float_format % val for val in values.ravel()[imask]]) | ||
[formater(val) for val in values.ravel()[imask]]) | ||
|
||
return values.tolist() | ||
|
||
def should_store(self, value): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2239,7 +2239,7 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None, | |
def to_csv(self, path, index=True, sep=",", na_rep='', | ||
float_format=None, header=False, | ||
index_label=None, mode='w', nanRep=None, encoding=None, | ||
date_format=None): | ||
date_format=None, decimal='.'): | ||
""" | ||
Write Series to a comma-separated values (csv) file | ||
|
||
|
@@ -2267,14 +2267,16 @@ def to_csv(self, path, index=True, sep=",", na_rep='', | |
non-ascii, for python versions prior to 3 | ||
date_format: string, default None | ||
Format string for datetime objects. | ||
decimal: string, default '.' | ||
Character recognized as decimal separator. E.g. use ‘,’ for European data | ||
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. same remark here |
||
""" | ||
from pandas.core.frame import DataFrame | ||
df = DataFrame(self) | ||
# result is only a string if no path provided, otherwise None | ||
result = df.to_csv(path, index=index, sep=sep, na_rep=na_rep, | ||
float_format=float_format, header=header, | ||
index_label=index_label, mode=mode, nanRep=nanRep, | ||
encoding=encoding, date_format=date_format) | ||
encoding=encoding, date_format=date_format, decimal=decimal) | ||
if path is None: | ||
return result | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2343,7 +2343,15 @@ def test_csv_to_string(self): | |
df = DataFrame({'col' : [1,2]}) | ||
expected = ',col\n0,1\n1,2\n' | ||
self.assertEqual(df.to_csv(), expected) | ||
|
||
|
||
def test_to_csv_decimal(self): | ||
df = DataFrame({'col1' : [1], 'col2' : ['a'], 'col3' : [10.1] }) | ||
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 the issue number as a comment here 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. done 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. I've tried to squash the commits as described but I faced the following error: Any idea ? 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. Try this:
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. There should be something special that I do to create my local version... When I do the command "git checkout master" in my to level directory I get If I do it in the "pandas" directory (the one with the setup.py file) I get: By the way, in my repository, I don't think I've made any branch. I've done On Tue, Mar 3, 2015 at 9:31 AM, Stephan Hoyer [email protected]
Bertrand Haut |
||
|
||
expected_default = ',col1,col2,col3\n0,1,a,10.1\n' | ||
self.assertEqual(df.to_csv(), expected_default) | ||
|
||
expected_european_excel = ';col1;col2;col3\n0;1;a;10,1\n' | ||
self.assertEqual(df.to_csv(decimal=',',sep=';'), expected_european_excel) | ||
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. Can you add a test for the case that also |
||
|
||
class TestSeriesFormatting(tm.TestCase): | ||
_multiprocess_can_split_ = True | ||
|
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.
Can you use standard single quotes here?
'
instead of‘