-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Display precision doesn't affect complex float numbers #25514 #25745
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 all commits
0e1f3b6
34c9d0d
6b1a365
d97bcd7
423eea1
5829a77
7dc8a33
3fd69e9
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 |
---|---|---|
|
@@ -19,9 +19,9 @@ | |
from pandas.compat import lzip | ||
|
||
from pandas.core.dtypes.common import ( | ||
is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype, | ||
is_extension_array_dtype, is_float, is_float_dtype, is_integer, | ||
is_integer_dtype, is_list_like, is_numeric_dtype, is_scalar, | ||
is_categorical_dtype, is_complex_dtype, is_datetime64_dtype, | ||
is_datetime64tz_dtype, is_extension_array_dtype, is_float, is_float_dtype, | ||
is_integer, is_integer_dtype, is_list_like, is_numeric_dtype, is_scalar, | ||
is_timedelta64_dtype) | ||
from pandas.core.dtypes.generic import ( | ||
ABCIndexClass, ABCMultiIndex, ABCSeries, ABCSparseArray) | ||
|
@@ -892,7 +892,7 @@ def format_array(values, formatter, float_format=None, na_rep='NaN', | |
fmt_klass = Timedelta64Formatter | ||
elif is_extension_array_dtype(values.dtype): | ||
fmt_klass = ExtensionArrayFormatter | ||
elif is_float_dtype(values.dtype): | ||
elif is_float_dtype(values.dtype) or is_complex_dtype(values.dtype): | ||
fmt_klass = FloatArrayFormatter | ||
elif is_integer_dtype(values.dtype): | ||
fmt_klass = IntArrayFormatter | ||
|
@@ -1084,6 +1084,7 @@ def format_values_with(float_format): | |
|
||
# separate the wheat from the chaff | ||
values = self.values | ||
is_complex = is_complex_dtype(values) | ||
mask = isna(values) | ||
if hasattr(values, 'to_dense'): # sparse numpy ndarray | ||
values = values.to_dense() | ||
|
@@ -1094,7 +1095,10 @@ def format_values_with(float_format): | |
for val in values.ravel()[imask]]) | ||
|
||
if self.fixed_width: | ||
return _trim_zeros(values, na_rep) | ||
if is_complex: | ||
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 can do the type check right here instead of above 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. The problem with that is at that point the |
||
return _trim_zeros_complex(values, na_rep) | ||
else: | ||
return _trim_zeros_float(values, na_rep) | ||
|
||
return values | ||
|
||
|
@@ -1424,7 +1428,22 @@ def just(x): | |
return result | ||
|
||
|
||
def _trim_zeros(str_floats, na_rep='NaN'): | ||
def _trim_zeros_complex(str_complexes, na_rep='NaN'): | ||
""" | ||
Separates the real and imaginary parts from the complex number, and | ||
executes the _trim_zeros_float method on each of those. | ||
""" | ||
def separate_and_trim(str_complex, na_rep): | ||
num_arr = str_complex.split('+') | ||
return (_trim_zeros_float([num_arr[0]], na_rep) + | ||
['+'] + | ||
_trim_zeros_float([num_arr[1][:-1]], na_rep) + | ||
['j']) | ||
|
||
return [''.join(separate_and_trim(x, na_rep)) for x in str_complexes] | ||
|
||
|
||
def _trim_zeros_float(str_floats, na_rep='NaN'): | ||
""" | ||
Trims zeros, leaving just one before the decimal points if need be. | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1370,6 +1370,19 @@ def test_to_string_float_index(self): | |
'5.0 4') | ||
assert result == expected | ||
|
||
def test_to_string_complex_float_formatting(self): | ||
# GH #25514 | ||
with pd.option_context('display.precision', 5): | ||
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 the gh issue number as a comment |
||
df = DataFrame({'x': [ | ||
(0.4467846931321966 + 0.0715185102060818j), | ||
(0.2739442392974528 + 0.23515228785438969j), | ||
(0.26974928742135185 + 0.3250604054898979j)]}) | ||
result = df.to_string() | ||
expected = (' x\n0 0.44678+0.07152j\n' | ||
'1 0.27394+0.23515j\n' | ||
'2 0.26975+0.32506j') | ||
assert result == expected | ||
|
||
gfyoung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_to_string_ascii_error(self): | ||
data = [('0 ', ' .gitignore ', ' 5 ', | ||
' \xe2\x80\xa2\xe2\x80\xa2\xe2\x80' | ||
|
Uh oh!
There was an error while loading. Please reload this page.