Skip to content

Commit 586ce30

Browse files
committed
changes
1 parent dd19a1d commit 586ce30

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ Missing
909909
- :meth:`DataFrame.interpolate` uses the correct axis convention now. Previously interpolating along columns lead to interpolation along indices and vice versa. Furthermore interpolating with methods ``pad``, ``ffill``, ``bfill`` and ``backfill`` are identical to using these methods with :meth:`fillna` (:issue:`12918`, :issue:`29146`)
910910
- Bug in :meth:`DataFrame.interpolate` when called on a DataFrame with column names of string type was throwing a ValueError. The method is no independing of the type of column names (:issue:`33956`)
911911
- :class:`NA` will now always work when passed into a format string. Previously a ``ValueError`` was raised if any format parameters were supplied to the format string.
912-
For example ``"{:.1f}".format(pd.NA)`` would previously raise a ``ValueError``, but will now return the string ``"<NA>"`` (:issue:`xxxxx`)
912+
For example ``"{:.1f}".format(pd.NA)`` would previously raise a ``ValueError``, but will now return the string ``"<NA>"`` (:issue:`34738`)
913913

914914
MultiIndex
915915
^^^^^^^^^^

pandas/_libs/missing.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ class NAType(C_NAType):
350350
return "<NA>"
351351

352352
def __format__(self, format_spec) -> str:
353+
# accept same format_spec as np.nan
354+
try:
355+
format(np.nan, format_spec)
356+
except ValueError:
357+
raise ValueError("Invalid format specifier")
358+
# if a string format_spec use it, else just return the repr
353359
try:
354360
return self.__repr__().__format__(format_spec)
355361
except ValueError:

pandas/tests/scalar/test_na_scalar.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ def test_repr():
2323

2424

2525
def test_format():
26+
# GH-34738
2627
assert format(NA) == "<NA>"
2728
assert format(NA, ">10") == " <NA>"
28-
assert format(NA, "xxx") == "<NA>" # accept arbitrary format strings
2929

3030
assert "{}".format(NA) == "<NA>"
3131
assert "{:>10}".format(NA) == " <NA>"
32-
assert "{:xxx}".format(NA) == "<NA>" # accept arbitrary format strings
32+
33+
34+
def test_format_raises():
35+
# GH-34738
36+
with pytest.raises(ValueError, match="Invalid format specifier"):
37+
format(NA, "xxx")
38+
39+
with pytest.raises(ValueError, match="Invalid format specifier"):
40+
"{:xxx}".format(NA)
3341

3442

3543
def test_truthiness():

0 commit comments

Comments
 (0)