Skip to content

Commit fc127a1

Browse files
jbrockmendelclaude
andauthored
DEPR: deprecate set_eng_float_format (#64717)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 983a3ed commit fc127a1

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

doc/source/user_guide/options.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,13 @@ Number formatting
307307
------------------
308308

309309
pandas also allows you to set how numbers are displayed in the console.
310-
This option is not set through the ``set_options`` API.
311-
312-
Use the ``set_eng_float_format`` function
313-
to alter the floating-point formatting of pandas objects to produce a particular
314-
format.
310+
Use ``display.precision`` to control the number of decimal places:
315311

316312
.. ipython:: python
317313
318314
import numpy as np
319315
320-
pd.set_eng_float_format(accuracy=3, use_eng_prefix=True)
316+
pd.set_option("display.precision", 2)
321317
s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
322318
s / 1.0e3
323319
s / 1.0e6

doc/source/whatsnew/v3.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Other API changes
9999
Deprecations
100100
~~~~~~~~~~~~
101101
- Deprecated :attr:`Timestamp.dayofweek`, :attr:`Timestamp.dayofyear`, :attr:`Timestamp.daysinmonth` in favor of :attr:`Timestamp.day_of_week`, :attr:`Timestamp.day_of_year`, :attr:`Timestamp.days_in_month`, respectively. The same deprecation applies to the corresponding attributes on :class:`Period`, :class:`DatetimeIndex`, :class:`PeriodIndex`, and :attr:`Series.dt` (:issue:`46768`)
102+
- Deprecated :func:`set_eng_float_format`. Use ``pd.set_option("display.precision", N)`` to control decimal precision, or pass a custom callable to ``pd.set_option("display.float_format", func)`` (:issue:`64460`)
102103
- Deprecated :meth:`.DataFrameGroupBy.agg` and :meth:`.Resampler.agg` unpacking a scalar when the provided ``func`` returns a Series or array of length 1; in the future this will result in the Series or array being in the result. Users should unpack the scalar in ``func`` itself (:issue:`64014`)
103104
- Deprecated :meth:`ExcelFile.parse`, use :func:`read_excel` instead (:issue:`58247`)
104105
- Deprecated ``engine="fastparquet"`` and ``engine="auto"`` in :func:`read_parquet` and :meth:`DataFrame.to_parquet`. The ``fastparquet`` library has been retired; use ``engine="pyarrow"`` or do not pass ``engine`` to use the default. (:issue:`64597`)

pandas/io/formats/format.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
Any,
2626
cast,
2727
)
28+
import warnings
2829

2930
import numpy as np
3031

@@ -41,7 +42,9 @@
4142
Timestamp,
4243
)
4344
from pandas._libs.tslibs.nattype import NaTType
45+
from pandas.errors import Pandas4Warning
4446
from pandas.util._decorators import set_module
47+
from pandas.util._exceptions import find_stack_level
4548

4649
from pandas.core.dtypes.common import (
4750
is_complex_dtype,
@@ -1945,6 +1948,11 @@ def set_eng_float_format(accuracy: int = 3, use_eng_prefix: bool = False) -> Non
19451948
"""
19461949
Format float representation in DataFrame with SI notation.
19471950
1951+
.. deprecated:: 3.1.0
1952+
Use ``pd.set_option("display.precision", N)`` to control decimal
1953+
precision, or pass a custom callable to
1954+
``pd.set_option("display.float_format", func)``.
1955+
19481956
Sets the floating-point display format for ``DataFrame`` objects using engineering
19491957
notation (SI units), allowing easier readability of values across wide ranges.
19501958
@@ -1968,44 +1976,26 @@ def set_eng_float_format(accuracy: int = 3, use_eng_prefix: bool = False) -> Non
19681976
19691977
Examples
19701978
--------
1971-
>>> df = pd.DataFrame([1e-9, 1e-3, 1, 1e3, 1e6])
1972-
>>> df
1973-
0
1974-
0 1.000000e-09
1975-
1 1.000000e-03
1976-
2 1.000000e+00
1977-
3 1.000000e+03
1978-
4 1.000000e+06
1979-
1980-
>>> pd.set_eng_float_format(accuracy=1)
1981-
>>> df
1982-
0
1983-
0 1.0E-09
1984-
1 1.0E-03
1985-
2 1.0E+00
1986-
3 1.0E+03
1987-
4 1.0E+06
1988-
1989-
>>> pd.set_eng_float_format(use_eng_prefix=True)
1990-
>>> df
1991-
0
1992-
0 1.000n
1993-
1 1.000m
1994-
2 1.000
1995-
3 1.000k
1996-
4 1.000M
1997-
1998-
>>> pd.set_eng_float_format(accuracy=1, use_eng_prefix=True)
1999-
>>> df
2000-
0
2001-
0 1.0n
2002-
1 1.0m
2003-
2 1.0
2004-
3 1.0k
2005-
4 1.0M
2006-
2007-
>>> pd.set_option("display.float_format", None) # unset option
1979+
Use ``pd.set_option("display.precision", N)`` to control decimal
1980+
precision instead:
1981+
1982+
>>> with pd.option_context("display.precision", 3):
1983+
... print(pd.DataFrame([1e-9, 1e-3, 1, 1e3, 1e6]))
1984+
0
1985+
0 1.000e-09
1986+
1 1.000e-03
1987+
2 1.000e+00
1988+
3 1.000e+03
1989+
4 1.000e+06
20081990
"""
1991+
warnings.warn(
1992+
"set_eng_float_format is deprecated and will be removed in a future "
1993+
"version. Use pd.set_option('display.precision', N) to control decimal "
1994+
"precision, or pass a custom callable to "
1995+
"pd.set_option('display.float_format', func).",
1996+
Pandas4Warning,
1997+
stacklevel=find_stack_level(),
1998+
)
20091999
set_option("display.float_format", EngFormatter(accuracy, use_eng_prefix))
20102000

20112001

pandas/tests/io/formats/test_eng_formatting.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import numpy as np
22
import pytest
33

4+
from pandas.errors import Pandas4Warning
5+
46
from pandas import (
57
DataFrame,
68
reset_option,
79
set_eng_float_format,
10+
set_option,
811
)
12+
import pandas._testing as tm
913

1014
from pandas.io.formats.format import EngFormatter
1115

@@ -21,19 +25,23 @@ def test_eng_float_formatter2(self, float_frame):
2125
df = float_frame
2226
df.loc[5] = 0
2327

24-
set_eng_float_format()
28+
msg = "set_eng_float_format is deprecated"
29+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
30+
set_eng_float_format()
2531
repr(df)
2632

27-
set_eng_float_format(use_eng_prefix=True)
33+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
34+
set_eng_float_format(use_eng_prefix=True)
2835
repr(df)
2936

30-
set_eng_float_format(accuracy=0)
37+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
38+
set_eng_float_format(accuracy=0)
3139
repr(df)
3240

3341
def test_eng_float_formatter(self):
3442
df = DataFrame({"A": [1.41, 141.0, 14100, 1410000.0]})
3543

36-
set_eng_float_format()
44+
set_option("display.float_format", EngFormatter(accuracy=3))
3745
result = df.to_string()
3846
expected = (
3947
" A\n"
@@ -44,12 +52,15 @@ def test_eng_float_formatter(self):
4452
)
4553
assert result == expected
4654

47-
set_eng_float_format(use_eng_prefix=True)
55+
set_option(
56+
"display.float_format",
57+
EngFormatter(accuracy=3, use_eng_prefix=True),
58+
)
4859
result = df.to_string()
4960
expected = " A\n0 1.410\n1 141.000\n2 14.100k\n3 1.410M"
5061
assert result == expected
5162

52-
set_eng_float_format(accuracy=0)
63+
set_option("display.float_format", EngFormatter(accuracy=0))
5364
result = df.to_string()
5465
expected = " A\n0 1E+00\n1 141E+00\n2 14E+03\n3 1E+06"
5566
assert result == expected
@@ -242,7 +253,7 @@ def test_nan(self):
242253
}
243254
)
244255
pt = df.pivot_table(values="a", index="b", columns="c")
245-
set_eng_float_format(accuracy=1)
256+
set_option("display.float_format", EngFormatter(accuracy=1))
246257
result = pt.to_string()
247258
assert "NaN" in result
248259

0 commit comments

Comments
 (0)