-
-
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 2 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,17 @@ 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 not float_format and decimal != '.': | ||
float_format = '%f' | ||
if float_format: | ||
imask = (~mask).ravel() | ||
values.flat[imask] = np.array( | ||
[float_format % val for val in values.ravel()[imask]]) | ||
if decimal != '.': | ||
imask = (~mask).ravel() | ||
values.flat[imask] = np.array( | ||
[val.replace('.',',',1) for val in values.ravel()[imask]]) | ||
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 write this in a way that avoids redundant code? e.g., define 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. New proposal submited |
||
|
||
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 | ||
|
||
|
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‘