Skip to content

Commit 0e1f3b6

Browse files
committed
BUG: Display precision doesn't affect complex float numbers pandas-dev#25514
1 parent bd49d2f commit 0e1f3b6

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

pandas/core/frame.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2498,12 +2498,12 @@ def memory_usage(self, index=True, deep=False):
24982498
... for t in dtypes])
24992499
>>> df = pd.DataFrame(data)
25002500
>>> df.head()
2501-
int64 float64 complex128 object bool
2502-
0 1 1.0 (1+0j) 1 True
2503-
1 1 1.0 (1+0j) 1 True
2504-
2 1 1.0 (1+0j) 1 True
2505-
3 1 1.0 (1+0j) 1 True
2506-
4 1 1.0 (1+0j) 1 True
2501+
int64 float64 complex128 object bool
2502+
0 1 1.0 1.000000+0.000000j 1 True
2503+
1 1 1.0 1.000000+0.000000j 1 True
2504+
2 1 1.0 1.000000+0.000000j 1 True
2505+
3 1 1.0 1.000000+0.000000j 1 True
2506+
4 1 1.0 1.000000+0.000000j 1 True
25072507
25082508
>>> df.memory_usage()
25092509
Index 80

pandas/io/formats/format.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from pandas.compat import StringIO, lzip, map, u, zip
1717

1818
from pandas.core.dtypes.common import (
19-
is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype,
20-
is_extension_array_dtype, is_float, is_float_dtype, is_integer,
21-
is_integer_dtype, is_list_like, is_numeric_dtype, is_scalar,
19+
is_categorical_dtype, is_complex_dtype, is_datetime64_dtype,
20+
is_datetime64tz_dtype, is_extension_array_dtype, is_float, is_float_dtype,
21+
is_integer, is_integer_dtype, is_list_like, is_numeric_dtype, is_scalar,
2222
is_timedelta64_dtype)
2323
from pandas.core.dtypes.generic import (
2424
ABCIndexClass, ABCMultiIndex, ABCSeries, ABCSparseArray)
@@ -885,6 +885,8 @@ def format_array(values, formatter, float_format=None, na_rep='NaN',
885885
fmt_klass = ExtensionArrayFormatter
886886
elif is_float_dtype(values.dtype):
887887
fmt_klass = FloatArrayFormatter
888+
elif is_complex_dtype(values.dtype):
889+
fmt_klass = FloatArrayFormatter
888890
elif is_integer_dtype(values.dtype):
889891
fmt_klass = IntArrayFormatter
890892
else:

pandas/tests/io/formats/test_format.py

+12
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,18 @@ def test_to_string_float_index(self):
14171417
'5.0 4')
14181418
assert result == expected
14191419

1420+
def test_to_string_complex_float_formatting(self):
1421+
fmt.set_option('display.precision', 5)
1422+
df = DataFrame({'x': [
1423+
(0.4467846931321966 + 0.0715185102060818j),
1424+
(0.2739442392974528 + 0.23515228785438969j),
1425+
(0.26974928742135185 + 0.3250604054898979j)]})
1426+
result = df.to_string()
1427+
expected = (' x\n0 0.44678+0.07152j\n'
1428+
'1 0.27394+0.23515j\n'
1429+
'2 0.26975+0.32506j')
1430+
assert result == expected
1431+
14201432
def test_to_string_ascii_error(self):
14211433
data = [('0 ', u(' .gitignore '), u(' 5 '),
14221434
' \xe2\x80\xa2\xe2\x80\xa2\xe2\x80'

0 commit comments

Comments
 (0)