-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: DataFrame.to_csv formatting parameters for float indexes #11681
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
Merged
Changes from all commits
Commits
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
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
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 |
---|---|---|
|
@@ -2932,6 +2932,52 @@ def test_to_csv_decimal(self): | |
expected_float_format = ';col1;col2;col3\n0;1;a;10,10\n' | ||
self.assertEqual(df.to_csv(decimal=',',sep=';', float_format = '%.2f'), expected_float_format) | ||
|
||
# GH 11553: testing if decimal is taken into account for '0.0' | ||
df = pd.DataFrame({'a': [0, 1.1], 'b': [2.2, 3.3], 'c': 1}) | ||
expected = 'a,b,c\n0^0,2^2,1\n1^1,3^3,1\n' | ||
self.assertEqual( | ||
df.to_csv(index=False, decimal='^'), expected) | ||
|
||
# same but for an index | ||
self.assertEqual( | ||
df.set_index('a').to_csv(decimal='^'), expected) | ||
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. ok, are there tests for |
||
|
||
# same for a multi-index | ||
self.assertEqual( | ||
df.set_index(['a', 'b']).to_csv(decimal="^"), expected) | ||
|
||
def test_to_csv_float_format(self): | ||
# testing if float_format is taken into account for the index | ||
# GH 11553 | ||
df = pd.DataFrame({'a': [0, 1], 'b': [2.2, 3.3], 'c': 1}) | ||
expected = 'a,b,c\n0,2.20,1\n1,3.30,1\n' | ||
self.assertEqual( | ||
df.set_index('a').to_csv(float_format='%.2f'), expected) | ||
|
||
# same for a multi-index | ||
self.assertEqual( | ||
df.set_index(['a', 'b']).to_csv(float_format='%.2f'), expected) | ||
|
||
def test_to_csv_na_rep(self): | ||
# testing if NaN values are correctly represented in the index | ||
# GH 11553 | ||
df = DataFrame({'a': [0, np.NaN], 'b': [0, 1], 'c': [2, 3]}) | ||
expected = "a,b,c\n0.0,0,2\n_,1,3\n" | ||
self.assertEqual(df.set_index('a').to_csv(na_rep='_'), expected) | ||
self.assertEqual(df.set_index(['a', 'b']).to_csv(na_rep='_'), expected) | ||
|
||
# now with an index containing only NaNs | ||
df = DataFrame({'a': np.NaN, 'b': [0, 1], 'c': [2, 3]}) | ||
expected = "a,b,c\n_,0,2\n_,1,3\n" | ||
self.assertEqual(df.set_index('a').to_csv(na_rep='_'), expected) | ||
self.assertEqual(df.set_index(['a', 'b']).to_csv(na_rep='_'), expected) | ||
|
||
# check if na_rep parameter does not break anything when no NaN | ||
df = DataFrame({'a': 0, 'b': [0, 1], 'c': [2, 3]}) | ||
expected = "a,b,c\n0,0,2\n0,1,3\n" | ||
self.assertEqual(df.set_index('a').to_csv(na_rep='_'), expected) | ||
self.assertEqual(df.set_index(['a', 'b']).to_csv(na_rep='_'), expected) | ||
|
||
def test_to_csv_date_format(self): | ||
# GH 10209 | ||
df_sec = DataFrame({'A': pd.date_range('20130101',periods=5,freq='s')}) | ||
|
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.
since this is basically identical to
core/internals/FloatBlock/to_native_types
. let's pull both those out and put it in a function incore/format.py/FloatArrayFormatter
and call it.get_formatted_data()
. See how that works out. These are the routines for screen printing (which are necessarily different from to_csv / index formatting).